// Program to test solutions to problem #10 on the cse142a final, autumn 2015. // Fill in your solution to isMatch, then compile and run the program. import java.util.*; public class FinalTest10 { public static boolean isMatch(String pattern, String text) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static boolean isMatch2(String pattern, String text) { int j = 0; for (int i = 0; i < pattern.length(); i++) { char ch1 = pattern.charAt(i); if (ch1 == '*') { int diff = text.length() - pattern.length() + 1; if (diff < 0) { return false; } else { j += diff; } } else { if (j >= text.length()) { return false; } char ch2 = text.charAt(j); j++; if (ch1 != '.' && ch1 != ch2) { return false; } } } return j == text.length(); } private static int[][] count = new int[2][2]; private static int[][] failCount = new int[2][2]; private static int overallFailCount = 0; public static void main(String[] args) { test(""); test("."); test("a"); test("x"); test("*"); test("a."); test(".a"); test("x."); test(".x"); test("at"); test("a*"); test("*a"); test("x*"); test("*x"); test(".."); test(".*"); test("*."); test("and"); test(".at"); test(".a."); test("t.."); test("..t"); test("..."); test("t*t"); test("ta*"); test("*at"); test("*th"); test("t*h"); test(".h*"); test("..*"); test("the*"); test("t*en"); test("...."); test("a..."); test(".a.."); test("..a."); test("...a"); test(".a*a"); test("th...n"); test("....th"); test(".....th"); if (overallFailCount == 0) { System.out.println("passed all tests"); } else { String[] group = {"non-star equal length", "non-star unequal length", "star equal length", "star unequal length"}; for (int i = 0; i < count.length; i++) { for (int j = 0; j < count[0].length; j++) { System.out.printf("%-25s failed %d of %d cases\n", group[i * 2 + j] + ":", failCount[i][j], count[i][j]); } } } } public static void test(String pattern) { String[] data = {"", "I", "a", "Ma", "Pa", "ah", "an", "as", "at", "be", "oh", "to", "and", "ant", "but", "cat", "cow", "hat", "sat", "tap", "tar", "ten", "the", "tot", "via", "army", "atom", "aunt", "aura", "bats", "coma", "jump", "lava", "love", "poem", "saga", "scam", "soda", "task", "team", "teen", "that", "then", "this", "trot", "tuna", "yard", "apple", "child", "chime", "hello", "quota", "shine", "spear", "teeth", "there", "today", "token", "trash", "twerp", "wolves", "battle", "cattle", "growth", "health", "nausea", "saliva", "thrown", "zenith", "seventh", "shimmer", "thermal", "thicken", "thistle", "tighten", "tiniest", "untruth", "voucher", "tapestry", "tomorrow", "trespass", "tarantula", "therefore", "thespians", "toothbrushes"}; for (String s : data) { test(pattern, s); } } public static void test(String pattern, String s) { int row; if (pattern.contains("*")) { row = 1; } else { row = 0; } if (pattern.length() == s.length()) { count[row][0]++; } else { count[row][1]++; } boolean result1 = false; Exception problem = null; try { result1 = isMatch(pattern, s); } catch (RuntimeException e) { problem = e; } boolean result2 = isMatch2(pattern, s); boolean fail = result1 != result2; if (fail || problem != null) { System.out.println("failed isMatch(\"" + pattern + "\", \"" + s + "\")"); if (problem != null) { int line = problem.getStackTrace()[1].getLineNumber(); System.out.println("threw " + problem); System.out.println("line # = " + line); } else { System.out.println("correct = " + result2); System.out.println("return = " + result1); } System.out.println(); overallFailCount++; if (pattern.length() == s.length()) { failCount[row][0]++; } else { failCount[row][1]++; } } } }