// Program to test solutions to problem #2 on the cse143 midterm, autumn 2017. // Fill in your solution to undouble, then compile and run the // program. import java.util.*; public class Test2 { public static String undouble(String s) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static String undouble2(String s) { if (s.length() < 2) { return s; } else if (s.charAt(0) == s.charAt(1)) { return s.charAt(0) + undouble2(s.substring(2)); } else { return s.charAt(0) + undouble2(s.substring(1)); } } private static int testCount; private static int failCount; public static final int ERRORS_MAX = 25; public static void main(String[] args) { test(""); test("m"); test("nn"); test("pq"); test("sstt"); test("odegaard"); test("bookkeeper"); test("baz"); test("foobar"); test("mississippi"); test("apple"); test("carry"); test("berry"); test("aardvark"); test("glass"); test("committment"); test("juggle"); test("little"); test("cac"); test("theses"); test("ccaatt"); test("aabbccddeeffgg"); test2("", 6); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test2(String s, int len) { if (!s.contains("aaa") && !s.contains("bbb") && !s.contains("ccc") && !s.contains("ddd") && !s.contains("eee")) { test(s); } if (len > 0) { for (char ch = 'a'; ch <= 'e'; ch++) { String s2 = s + ch; test2(s2, len - 1); } } } public static void test(String s) { System.out.println("testing undouble(\"" + s + "\")"); String result1 = undouble2(s); boolean fail = false; try { String result2 = undouble(s); if (!result1.equals(result2)) { fail = true; System.out.println("result should be \"" + result1 + "\", yours = \"" + result2 + "\""); } } catch (RuntimeException e) { int line = e.getStackTrace()[1].getLineNumber(); System.out.println("threw " + e + " at line #" + line); fail = true; } testCount++; if (fail) { System.out.println("failed"); failCount++; if (failCount > ERRORS_MAX) { System.out.println("exceeds " + ERRORS_MAX + " errors"); System.out.println("with " + testCount + " correct"); System.exit(0); } } else { System.out.println("passed"); } System.out.println(); } }