Key to CSE142 Final, Spring 2017 handout #19 1. 4 [9, 20, 30, 41] 1 [9, 20, 30, 41] 2 0 2. Original List Final List -------------------------------------------------------------- {0, 2, 3} {0, 2, 5} {1, 1, 4, 5} {1, 1, 3, 7} {3, 1, 2, 9, 5} {3, 1, 3, 7, 5} {5, 1, 1, 1, 8} {5, 1, 3, 7, 8} {2, 1, 1, 2, 3, 4} {2, 1, 3, 7, 3, 7} 3. marty marty 1 stuart 2 stuart stuart 1 stuart 2 marty zorah 1 stuart 2 stuart stuart 1 riley 2 4. public static void runningStats(Scanner input) { while (input.hasNext()) { String name = input.next(); int counter = 0; int sum = 0; while (input.hasNextInt()) { int next = input.nextInt(); sum += next; counter++; } System.out.println(name + " ran " + counter + " laps in " + sum + " seconds"); System.out.println("average lap time: " + (double) sum / counter); } } 5. public static void familyData(Scanner input) { int families = 0; while (input.hasNextLine()) { families++; String family = input.nextLine(); int plusIndex = family.indexOf("+"); String parent1 = family.substring(0, plusIndex); String parent2 = family.substring(plusIndex + 1); System.out.println(parent1 + " and " + parent2); String kids = input.nextLine(); Scanner kidScan = new Scanner(kids); System.out.print("offspring: "); int count = 0; while (kidScan.hasNext()) { System.out.print(kidScan.next() + " "); count++; } System.out.println("(" + count + " total)"); } System.out.println("total families: " + families); } 6. public static boolean containsTriple(int[] list) { for (int i = 0; i < list.length - 2; i++) { if (list[i] == list[i + 1] && list[i + 1] == list[i + 2]) { return true; } } return false; } 7. Several Solutions: public static void collapse(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { int sum = list.get(i) + list.get(i + 1); list.set(i, sum); list.remove(i + 1); } } public static void collapse(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { list.add(i, list.remove(i) + list.remove(i)); } } public static void collapse(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { list.set(i, list.remove(i) + list.get(i)); } } 8. public class Vulture extends Critter { private int count; private int max; private boolean infected; public Vulture() { Random r = new Random(); max = r.nextInt(6) + 1; infected = false; count = 0; } public String toString() { return "V"; } public Color getColor() { if (infected) { return Color.BLUE; } else { return Color.BLACK; } } public Action getMove(CritterInfo info) { if (info.getFront() == Neighbor.OTHER) { infected = true; return Action.INFECT; } else { infected = false; if (count < max) { count++; return Action.HOP; } else { count = 0; return Action.LEFT; } } } } 9. public static int[] insertMirror(int[] list1, int[] list2, int index) { int[] result = new int[list1.length + list2.length * 2]; for (int i = 0; i < index; i++) { result[i] = list1[i]; } for (int i = 0; i < list2.length; i++) { result[i + index] = list2[list2.length - 1 - i]; result[i + index + list2.length] = list2[i]; } for (int i = index; i < list1.length; i++) { result[i + list2.length * 2] = list1[i]; } return result; } 10. Several Solutions: public static void moveToBack(int[] list, int n) { for (int i = list.length - 2; i >= 0; i--) { if (list[i] == n) { for (int j = i; j < list.length - 1; j++) { list[j] = list[j + 1]; } list[list.length - 1] = n; } } } public static void moveToBack(int[] list, int n) { int numNonN = 0; for (int i = 0; i < list.length; i++) { if (list[i] != n) { list[numNonN] = list[i]; numNonN++; } } for (int i = numNonN; i < list.length; i++) { list[i] = n; } }
Riley Porter Last modified: Tue Jun 13 2017