Create a scheme function: diginlist - takes a list and returns the “diggedin” ve
ID: 3703106 • Letter: C
Question
Create a scheme function:
diginlist - takes a list and returns the “diggedin” version of the list. “diggingin” a list ( this is not a recognized function for list ??) means surrounding the rightmost and leftmost elements in parenthesis, and repeating the process inwards until the entire list is “diggedin”. For example:
(diginlist '(4 5 3 2 8)) would return (4 (5 (3) 2) 8).
You can assume that the input list is "flat", meaning that it doesn't contain any lists. If the input list has an odd number of elements, the center element should be in a list by itself, as shown in the example above. If the input list has an even number of elements, then there should be an empty list in the middle. For example:
(diginlist '(4 5 2 8) would return (4 (5 () 2) 8)).
Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Neshted {
public static String neshted_list(List<Integer> list){
String output = "";
float count =0;
for(int i : list){
if(count == list.size()/2){
if(list.size() %2 == 0)
output += "()";
else
output += "(";
}
if(count<list.size()/2){
output += "("+i;
}
else if(count >=list.size()/2) {
output += i+")";
}
count ++;
}
return output;
}
public static void main (String args[]){
Integer[] values = {4,5,3,2,8};
List<Integer> list = Arrays.asList(values);
System.out.println(neshted_list(list));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.