// Program to test solutions to problem #6 on the cse143 midterm, spring 2018. // Fill in your solution to mirrorSplit, then compile and run the program. import java.util.*; public class Test6 { public static void mirrorSplit(Stack s) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static void mirrorSplit2(Stack s) { Queue q = new LinkedList(); while (!s.isEmpty()) { q.add(s.pop()); } int oldSize = q.size(); for (int i = 0; i < oldSize; i++) { int n = q.remove(); q.add(n / 2); s.push(n / 2 + n % 2); } while (!s.isEmpty()) { q.add(s.pop()); } for (int i = 0; i < oldSize; i++) { q.add(q.remove()); } while (!q.isEmpty()) { s.push(q.remove()); } } private static int testCount; private static int failCount; public static void main(String[] args) { test(new int[] {}); test(new int[] {2}); test(new int[] {2, 4}); test(new int[] {2, 4, 6}); test(new int[] {2, 4, 6, 8}); test(new int[] {2, 4, 6, 8, 10}); test(new int[] {3}); test(new int[] {3, 5}); test(new int[] {3, 5, 7}); test(new int[] {3, 5, 7, 9}); test(new int[] {14, 7, 0, 13, 28, 42, 19, 108, 317}); test(new int[] {14, 20, 8, 12}); test(new int[] {13, 5, 12}); test(new int[] {2, 4, 6, 8, 10, 12}); test(new int[] {14, 7, 0, 13, 28, 42, 19, 108, 317}); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int[] data) { Stack s = new Stack(); Stack s2 = new Stack(); for (int n : data) { s.push(n); s2.push(n); } System.out.println("original = " + s); mirrorSplit2(s); System.out.println("result = " + s); boolean fail = false; try { mirrorSplit(s2); } catch (RuntimeException e) { int line = e.getStackTrace()[2].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (!fail && !s.equals(s2)) { System.out.println("yours = " + s2); fail = true; } testCount++; if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }