|
1 | 1 | package chapter18;
|
2 | 2 |
|
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.LinkedList; |
| 5 | + |
3 | 6 | public class Chapter18 {
|
4 | 7 | public static void main (String [] args){
|
5 |
| - /********/ |
6 |
| - /* 18.1 */ |
7 |
| - /********/ |
| 8 | + test_EighteenPoint1(); |
| 9 | + // 18.2 - Can't test randomness |
| 10 | + // 18.3 - Can't test randomness |
| 11 | + test_EighteenPoint4(); |
| 12 | + test_EighteenPoint5(); |
| 13 | + test_EighteenPoint6(); |
| 14 | + test_EighteenPoint7(); |
| 15 | + // 18.8 - Skip (First learn tries. Then maybe do this suffix tree problem) |
| 16 | + test_EighteenPoint9(); |
| 17 | + test_EighteenPoint10(); |
| 18 | + test_EighteenPoint11(); |
| 19 | + test_EighteenPoint12(); |
| 20 | + // 18.13: Skip (too complicated for an interview) |
| 21 | + } |
| 22 | + |
| 23 | + public static void test_EighteenPoint1(){ |
8 | 24 | System.out.println("*** Test 18.1: Add 2 numbers (without using +)");
|
9 |
| - System.out.println("17 + 34 = " + EighteenPoint1.add(17, 34)); |
10 |
| - System.out.println("-17 + 34 = " + EighteenPoint1.add(-17, 34)); |
11 |
| - System.out.println("17 + -34 = " + EighteenPoint1.add(17, -34)); |
12 |
| - System.out.println("-17 + -34 = " + EighteenPoint1.add(-17, -34)); |
| 25 | + System.out.println("17 + 34 = " + EighteenPoint01.add(17, 34)); |
| 26 | + System.out.println("-17 + 34 = " + EighteenPoint01.add(-17, 34)); |
| 27 | + System.out.println("17 + -34 = " + EighteenPoint01.add(17, -34)); |
| 28 | + System.out.println("-17 + -34 = " + EighteenPoint01.add(-17, -34)); |
13 | 29 | System.out.println();
|
14 |
| - System.out.println("17 + 34 = " + EighteenPoint1.addBook(17, 34)); |
15 |
| - System.out.println("-17 + 34 = " + EighteenPoint1.addBook(-17, 34)); |
16 |
| - System.out.println("17 + -34 = " + EighteenPoint1.addBook(17, -34)); |
17 |
| - System.out.println("-17 + -34 = " + EighteenPoint1.addBook(-17, -34)); |
| 30 | + System.out.println("17 + 34 = " + EighteenPoint01.addBook(17, 34)); |
| 31 | + System.out.println("-17 + 34 = " + EighteenPoint01.addBook(-17, 34)); |
| 32 | + System.out.println("17 + -34 = " + EighteenPoint01.addBook(17, -34)); |
| 33 | + System.out.println("-17 + -34 = " + EighteenPoint01.addBook(-17, -34)); |
18 | 34 | System.out.println();
|
19 |
| - System.out.println("9 + 234 = " + EighteenPoint1.add(9, 234)); |
20 |
| - System.out.println("-9 + 234 = " + EighteenPoint1.add(-9, 234)); |
21 |
| - System.out.println("9 + -234 = " + EighteenPoint1.add(9, -234)); |
22 |
| - System.out.println("-9 + -234 = " + EighteenPoint1.add(-9, -234)); |
| 35 | + System.out.println("9 + 234 = " + EighteenPoint01.add(9, 234)); |
| 36 | + System.out.println("-9 + 234 = " + EighteenPoint01.add(-9, 234)); |
| 37 | + System.out.println("9 + -234 = " + EighteenPoint01.add(9, -234)); |
| 38 | + System.out.println("-9 + -234 = " + EighteenPoint01.add(-9, -234)); |
23 | 39 | System.out.println();
|
24 |
| - System.out.println("9 + 234 = " + EighteenPoint1.addBook(9, 234)); |
25 |
| - System.out.println("-9 + 234 = " + EighteenPoint1.addBook(-9, 234)); |
26 |
| - System.out.println("9 + -234 = " + EighteenPoint1.addBook(9, -234)); |
27 |
| - System.out.println("-9 + -234 = " + EighteenPoint1.addBook(-9, -234)); |
| 40 | + System.out.println("9 + 234 = " + EighteenPoint01.addBook(9, 234)); |
| 41 | + System.out.println("-9 + 234 = " + EighteenPoint01.addBook(-9, 234)); |
| 42 | + System.out.println("9 + -234 = " + EighteenPoint01.addBook(9, -234)); |
| 43 | + System.out.println("-9 + -234 = " + EighteenPoint01.addBook(-9, -234)); |
| 44 | + } |
| 45 | + |
| 46 | + public static void test_EighteenPoint4(){ |
| 47 | + System.out.println("\n\n*** Test 18.4: Count 2s in range 1...n"); |
| 48 | + System.out.println("(from 1 to 99): " + EighteenPoint04.count2s(99)); |
| 49 | + } |
| 50 | + |
| 51 | + public static void test_EighteenPoint5(){ |
| 52 | + System.out.println("\n\n*** Test 18.5: Shortest distance between 2 words in a file"); |
| 53 | + String [] file = {"hey", "there", "hi", "food", "there", "now", "hope", "hi", "food", "now", "gone", "hey"}; |
| 54 | + System.out.println(Arrays.toString(file)); |
28 | 55 |
|
29 |
| - // 18.2 - Can't test true randomness |
30 |
| - // 18.3 - Can't test true randomness |
| 56 | + EighteenPoint05.preProcess(file); |
31 | 57 |
|
32 |
| - /********/ |
33 |
| - /* 18.4 */ |
34 |
| - /********/ |
35 |
| - System.out.println("\n *** Test 18.4: "); |
36 |
| - System.out.println("Solution 1 - Count 2s: " + EighteenPoint4.count2s(99)); |
37 |
| - System.out.println("Solution 2 - Count 2s: " + EighteenPoint4.count2sInRange(99)); |
38 |
| - System.out.println("Solution 1 - Count 2s: " + EighteenPoint4.count2s(23212)); |
39 |
| - System.out.println("Solution 2 - Count 2s: " + EighteenPoint4.count2sInRange(23212)); |
| 58 | + System.out.println("(Solution 1) (there, food) Distance = " + EighteenPoint05.shortest(file, "there", "food")); |
| 59 | + System.out.println("(Solution 2) (there, food) Distance = " + EighteenPoint05.shortest2("there", "food")); |
40 | 60 |
|
41 |
| - //18.5 - Was too lazy to test since Solution 1 matched book/web code, and Solution 1 just has an important algorithm. |
| 61 | + System.out.println("\n(Solution 1) (now, hi) Distance = " + EighteenPoint05.shortest(file, "now", "hi")); |
| 62 | + System.out.println("(Solution 2) (now, hi) Distance = " + EighteenPoint05.shortest2("now", "hi")); |
42 | 63 |
|
43 |
| - /********/ |
44 |
| - /* 18.6 */ |
45 |
| - /********/ |
46 |
| - System.out.println("\n *** Test 18.6: n (well n-1) smallest elements"); |
| 64 | + System.out.println("\n(Solution 1) (gone, hi) Distance = " + EighteenPoint05.shortest(file, "gone", "hi")); |
| 65 | + System.out.println("(Solution 2) (gone, hi) Distance = " + EighteenPoint05.shortest2("gone", "hi")); |
| 66 | + } |
| 67 | + |
| 68 | + public static void test_EighteenPoint6(){ |
| 69 | + System.out.println("\n\n*** Test 18.6: Find n smallest elements in an array"); |
47 | 70 | int [] array = {52, 14, 16, 17, 26, 31, 16, 0, 6, 19, 6, 26, 14, 26, 22, 25};
|
48 |
| - for (int i = 0; i < array.length; i++){ |
49 |
| - System.out.print(array[i] + " "); |
50 |
| - } |
| 71 | + System.out.println(Arrays.toString(array)); |
| 72 | + |
51 | 73 | System.out.print("\n3 Smallest: ");
|
52 |
| - EighteenPoint6.select(array, 3); |
| 74 | + EighteenPoint06.findNthSmallestNums(array, 3); |
53 | 75 | System.out.print("\n8 Smallest: ");
|
54 |
| - EighteenPoint6.select(array, 8); |
| 76 | + EighteenPoint06.findNthSmallestNums(array, 8); |
55 | 77 | System.out.print("\n12 Smallest: ");
|
56 |
| - EighteenPoint6.select(array, 12); |
57 |
| - |
58 |
| - /********/ |
59 |
| - /* 18.7 */ |
60 |
| - /********/ |
61 |
| - System.out.println("\n\n *** Test 18.7: word combinations"); |
62 |
| - String [] array2 = {"cat", "banana", "dog", "nana", "walk", "walker", "dogwalker", "spectaculious"}; |
63 |
| - String [] array3 = {"cat", "banana", "dog", "nana", "walk", "nanadogwalkercat", "walker", "dogwalker"}; |
| 78 | + EighteenPoint06.findNthSmallestNums(array, 12); |
| 79 | + } |
| 80 | + |
| 81 | + public static void test_EighteenPoint7(){ |
| 82 | + System.out.println("\n\n\n*** Test 18.7: Find longest word composed of other words"); |
| 83 | + String [] array1 = {"cat", "banana", "dog", "nana", "walk", "walker", "dogwalker", "spectaculous"}; |
| 84 | + String [] array2 = {"cat", "banana", "dog", "nana", "walk", "nanadogwalkercat", "walker", "dogwalker"}; |
64 | 85 |
|
65 |
| - System.out.print("Array A: "); |
66 |
| - for (int i = 0; i < array2.length; i++){ |
67 |
| - System.out.print(array2[i] + " "); |
68 |
| - } |
69 |
| - System.out.println(); |
70 |
| - System.out.print("Array B: "); |
71 |
| - for (int i = 0; i < array3.length; i++){ |
72 |
| - System.out.print(array3[i] + " "); |
73 |
| - } |
| 86 | + System.out.print(Arrays.toString(array1)); |
| 87 | + System.out.println("\nSolution: " + EighteenPoint07.longestWord(array1)); |
74 | 88 |
|
75 |
| - String longestComboWord = EighteenPoint7.longestWord(array2); |
76 |
| - System.out.println("\n\nFind longest word composed of other words (Array A): " + longestComboWord + ""); |
77 |
| - |
78 |
| - longestComboWord = EighteenPoint7.longestWord(array3); |
79 |
| - System.out.println("Find longest word composed of other words (Array B): " + longestComboWord); |
80 |
| - |
81 |
| - //18.8 - Suffix Tree (Skipped) |
82 |
| - //18.9 - No need to test. Code same as book |
83 |
| - //18.10 - No need to test. Code same as book |
84 |
| - //18.11 - No need to test. Code same as book |
85 |
| - //18.12 - Too lazy to test. Code is from book & website. |
| 89 | + System.out.print("\n" + Arrays.toString(array2)); |
| 90 | + System.out.println("\nSolution: " + EighteenPoint07.longestWord(array2)); |
| 91 | + } |
| 92 | + |
| 93 | + public static void test_EighteenPoint9(){ |
| 94 | + System.out.println("\n\n*** Test 18.9: Keep track of median"); |
| 95 | + for (int i = 0; i < 5; i++) { |
| 96 | + EighteenPoint09.addNum(i); |
| 97 | + System.out.println("Add " + i + " to data structure. Median = " + EighteenPoint09.getMedian()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static void test_EighteenPoint10(){ |
| 102 | + System.out.println("\n\n*** Test 18.10: Transform 1 word into another (changing 1 letter at a time)"); |
| 103 | + EighteenPoint10.setUpDict(); |
| 104 | + LinkedList<String> solution = EighteenPoint10.convert("Damp", "Like"); |
| 105 | + System.out.println(solution); |
| 106 | + } |
| 107 | + |
| 108 | + public static void test_EighteenPoint11(){ |
| 109 | + System.out.println("\n\n*** Test 18.11: Find maximum subsquare with black borders"); |
| 110 | + int[][] matrix = {{1,0,1,1,1}, |
| 111 | + {1,1,1,0,1}, |
| 112 | + {1,1,1,1,1}, |
| 113 | + {1,1,1,1,0}, |
| 114 | + {1,0,1,0,1}}; |
| 115 | + System.out.println(EighteenPoint11.findLargestSubsquare(matrix)); |
| 116 | + System.out.println(EighteenPoint11.findLargestSubsquare2(matrix)); |
| 117 | + } |
| 118 | + |
| 119 | + public static void test_EighteenPoint12(){ |
| 120 | + System.out.println("\n\n*** Test 18.12: Find submatrix with largest possible sum"); |
| 121 | + int[][] matrix = {{ 1,-2, 3, 1}, |
| 122 | + { 1, 5,-4, 1}, |
| 123 | + { 1, 1, 0, 2}, |
| 124 | + {-1, 1, 1,-8}, |
| 125 | + {-8,-9, 9,-3}}; |
| 126 | + System.out.println("(Solution 1): " + EighteenPoint12.findLargestSubmatrix(matrix)); |
| 127 | + System.out.println("(Solution 2): " + EighteenPoint12.findLargestSubmatrix2(matrix)); |
| 128 | + System.out.println("(Solution 3): " + EighteenPoint12.findLargestSubmatrix3(matrix)); |
86 | 129 | }
|
87 | 130 | }
|
0 commit comments