These problems were inspired by the 99 Lisp problems. Lisp is a very different language to Java; its use of lists means it’s not appropriate to emulate all the original problems in Java, however, there is some benefit in practising the use of Java collections.
The introduction of lambda functions in Java8 made me rethink how best to approach these problems. There are three things being practised here: collections, generics and lambdas. I’ve split up the questions so that they give a chance to practice these different skills.
1 Last Item
Find the last item in an List of String objects
1.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "fork", "spoon")); 2: System.out.println(lastItem(myList)); 3: *** Output *** 4: spoon
2 Last Item Generic
Find the last item in a Generic List
2.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "fork", "spoon")); 2: List<Integer> anotherList = new ArrayList<>(asList(1,2,3)); 3: System.out.println(gLastItem(myList)); 4: System.out.println(gLastItem(anotherList)); 5: *** Output *** 6: spoon 7: 3
3 Last But One
Find the last but one item in a List
3.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "fork", "spoon")); 2: System.out.println(lastButOneItem(myList)); 3: *** Output *** 4: fork
4 Iterate through the Elements of a List
Iterate through the Elements of a List, printing them out in turn
4.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "fork", "spoon")); 2: System.out.println(printEach(myList)); 3: *** Output *** 4: knife 5: fork 6: spoon
5 Eliminate consecutive duplicates from a List
Write a method that removes consecutive duplicates from a List. The order of the elements should not be changed.
5.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "knife", "fork", "spoon","spoon")); 2: printEach(removeDuplicates(myList)); 3: *** Output *** 4: knife 5: fork 6: spoon
6 Eliminate consecutive duplicates from a List using HashSet
Remembering that a HashSet cannot contain duplicates, repeat the above question using a HashSet
6.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "knife", "fork", "spoon","spoon")); 2: printEach(removeDuplicates(myList)); 3: *** Output *** 4: knife 5: fork 6: spoon
Remembering that a HashSet cannot contain duplicates
7 RLE
Run-length encoding of a list. Consecutive duplicates of elements are encoded as pairs where N is the number of duplicates of the element E.
Use this Pair class
1: class Pair<T> 2: { 3: int count; 4: T value; 5: 6: Pair(int count, T value) 7: { 8: this.count = count; 9: this.value = value; 10: } 11: 12: void print() 13: { 14: System.out.print("("+ count + ", " + value +") "); 15: } 16: }
7.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "knife", "fork", "spoon","spoon")); 2: List<Pair> rle = RLE(myList); 3: for (Pair r: rle) 4: { 5: r.print(); 6: } 7: System.out.println(""); 8: *** Output *** 9: (2, knife) (1, fork) (2, spoon)
8 Decode a run-length encoded list
Given a run-length code list generated in a previous problem, construct its uncompressed version.
8.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "knife", "fork", "spoon","spoon")); 2: List<Pair> rle = RLE(myList); 3: for (Pair r: rle) 4: { 5: r.print(); 6: } 7: System.out.println(""); 8: 9: List<Object> decoded = decodeRLE(rle); 10: 11: for (Object o: decoded) 12: { 13: System.out.println(o); 14: } 15: *** Output *** 16: (2, knife) (1, fork) (2, spoon) 17: knife 18: knife 19: fork 20: spoon 21: spoon
9 Duplicate the elements of a list
9.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "knife", "fork", "spoon","spoon")); 2: printEach(dupli(myList)); 3: *** Output *** 4: knife 5: knife 6: knife 7: knife 8: fork 9: fork 10: spoon 11: spoon 12: spoon 13: spoon
10 Replicate the elements of a list a given number of times
10.1 Example
1: List<String> myList = new ArrayList<>(asList("knife", "knife", "fork", "spoon","spoon")); 2: printEach(repli(myList,3)); 3: *** Output *** 4: knife 5: knife 6: knife 7: knife 8: knife 9: knife 10: fork 11: fork 12: fork 13: spoon 14: spoon 15: spoon 16: spoon 17: spoon 18: spoon