// Fill in your solution to rearrange, then compile and run the // program import java.util.*; public class Rearrange { public static void rearrange(ArrayList list) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static void rearrange2(ArrayList list) { int count = 0; for (int i = 1; i < list.size(); i++) { if (i % 2 == 0) { count++; int move = list.remove(i); list.add(count, move); } } } private static int testCount, failCount; public static final int ERRORS_MAX = 25; public static void main(String[] args) { failCount = 0; for (int i = 0; i <= 15; i++) { test(i); } if (failCount == 0) { System.out.println("PASSED all " + testCount + " tests"); } } public static void test(int i) { int[] data = new int[i]; test(data, 0); } public static void test(int[] data, int index) { if (index == data.length) { testCount++; ArrayList data1 = new ArrayList(); ArrayList data2 = new ArrayList(); for (int n : data) { data1.add(n); data2.add(n); } boolean fail = false; RuntimeException except = null; try { rearrange(data1); } catch (RuntimeException e) { fail = true; except = e; } rearrange2(data2); fail = fail || !data1.equals(data2); if (fail) { if (except != null) { StackTraceElement[] trace = except.getStackTrace(); int line = trace[0].getLineNumber(); System.out.println("FAIL for " + Arrays.toString(data) + " with exception at line#" + line); } else { System.out.println("FAIL for " + Arrays.toString(data)); } System.out.println("correct: " + data2); System.out.println("yours : " + data1); System.out.println(); failCount++; if (failCount > ERRORS_MAX) { System.out.println("exceeds " + ERRORS_MAX + " errors"); System.out.println("with " + testCount + " correct"); System.exit(0); } } } else { data[index] = 10 + index; test(data, index + 1); data[index] = 11 + index; test(data, index + 1); } } }