Skip to content

Commit 4a6e4c0

Browse files
committed
Refactored Chapters 1-3
1 parent 640387d commit 4a6e4c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+656
-699
lines changed

src/ThreePoint7/Animal.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/ThreePoint7/Cat.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/ThreePoint7/Dog.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/ThreePoint7/Shelter.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/ThreePoint7/ThreePoint7.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/chapter1/IntroArrayList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44

5+
/* ArrayList practice */
56
public class IntroArrayList {
67
public static void main(String [] args){
78
ArrayList<String> strings = new ArrayList<String>();
@@ -15,7 +16,7 @@ public static void main(String [] args){
1516
}
1617
}
1718

18-
/* Merge 2 arrays into an ArrayList */
19+
/* Merges 2 arrays into an ArrayList */
1920
public static ArrayList<String> merge(String [] words, String [] moreWords){
2021
ArrayList<String> result = new ArrayList<String>();
2122
for (String word : words){

src/chapter1/IntroHashMap.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package chapter1;
2+
23
import java.util.HashMap;
34

5+
/* HashMap practice */
46
public class IntroHashMap {
57
public static void main (String [] args){
68
String [] strings = {"Bob", "Jane", "Alex"};
@@ -10,7 +12,7 @@ public static void main (String [] args){
1012
}
1113
}
1214

13-
/* Build HashMap from Array */
15+
/* Builds HashMap from Array */
1416
public static HashMap<Integer, String> buildMap(String [] strings){
1517
int i = 0;
1618
HashMap<Integer, String> map = new HashMap<Integer, String>();

src/chapter1/IntroStringBuffer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package chapter1;
22

3+
/* StringBuffer practice */
34
public class IntroStringBuffer {
45
public static void main(String [] args){
5-
String [] myStrings = {"Hi ", "there ", "Rob"};
6+
String [] myStrings = {"Hi ", "there ", "Rob. ", "How ", "are ", "you?"};
67
System.out.println(joinWords(myStrings));
78
}
89

10+
/* Joins an an array of strings into 1 string. Uses StringBuffer for efficiency */
911
public static String joinWords(String [] strings){
1012
StringBuffer sentence = new StringBuffer();
1113
for (String string : strings){

src/chapter1/OnePoint1.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package chapter1;
2+
23
import java.util.HashSet;
34
import java.util.Arrays;
45

@@ -9,27 +10,13 @@
910
* 4) Using Sorting O(n log n) runtime
1011
* 5) Naive Brute Force O(n^2) runtime
1112
*/
13+
14+
/* Determines if a String has all unique characters */
1215
public class OnePoint1 {
1316
public static void main (String [] args){
14-
System.out.println("Bob " + uniqueCharacters_1("Bobby"));
15-
System.out.println("Ben " + uniqueCharacters_1("Ben"));
16-
System.out.println("Alex " + uniqueCharacters_1("Alex"));
17-
System.out.println();
18-
System.out.println("Bob " + uniqueCharacters_2("Bobby"));
19-
System.out.println("Ben " + uniqueCharacters_2("Ben"));
20-
System.out.println("Alex " + uniqueCharacters_2("Alex"));
21-
System.out.println();
22-
System.out.println("Bob " + uniqueCharacters_3("Bobby"));
23-
System.out.println("Ben " + uniqueCharacters_3("Ben"));
24-
System.out.println("Alex " + uniqueCharacters_3("Alex"));
25-
System.out.println();
26-
System.out.println("Bob " + uniqueCharacters_4("Bobby"));
27-
System.out.println("Ben " + uniqueCharacters_4("Ben"));
28-
System.out.println("Alex " + uniqueCharacters_4("Alex"));
29-
System.out.println();
30-
System.out.println("Bob " + uniqueCharacters_5("Bobby"));
31-
System.out.println("Ben " + uniqueCharacters_5("Ben"));
32-
System.out.println("Alex " + uniqueCharacters_5("Alex"));
17+
test("Benny");
18+
test("Ben");
19+
test("Alex");
3320
}
3421

3522
/* O(n) runtime, O(n) space: using HashMap (my favorite solution) */
@@ -97,5 +84,16 @@ public static boolean uniqueCharacters_5(String string){
9784
}
9885
return true;
9986
}
87+
88+
/* Tests code */
89+
private static void test(String s){
90+
System.out.println("*** " + s);
91+
System.out.println("Solution 1: " + uniqueCharacters_1(s));
92+
System.out.println("Solution 2: " + uniqueCharacters_2(s));
93+
System.out.println("Solution 3: " + uniqueCharacters_3(s));
94+
System.out.println("Solution 4: " + uniqueCharacters_4(s));
95+
System.out.println("Solution 5: " + uniqueCharacters_5(s));
96+
System.out.println();
97+
}
10098
}
10199

src/chapter1/OnePoint3.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
package chapter1;
2+
23
import java.util.Arrays;
34

5+
/* Determines if 2 Strings are permutations of each other */
46
public class OnePoint3 {
57
public static void main (String [] args){
6-
if (isPermutation_1("hello", "olhel"))
7-
System.out.println("yes");
8-
else
9-
System.out.println("no");
10-
11-
if (isPermutation_1("james", "beans"))
12-
System.out.println("yes");
13-
else
14-
System.out.println("no");
15-
System.out.println();
16-
17-
/* Solution 2 */
18-
if (isPermutation_2("hello", "olhel"))
19-
System.out.println("yes");
20-
else
21-
System.out.println("no");
22-
23-
if (isPermutation_2("james", "beans"))
24-
System.out.println("yes");
25-
else
26-
System.out.println("no");
8+
test("hello", "olhel");
9+
test("james", "bean");
2710
}
2811

2912
/* Solution 1 - Sort the 2 Strings (by converting to char[]), then compare them: O(n log n) */
@@ -40,6 +23,7 @@ public static boolean isPermutation_1(String s1, String s2){
4023
return s1.equals(s2);
4124
}
4225

26+
/* Sorts the characters in a String, returning a new String */
4327
private static String sort(String s){
4428
char [] charArray = s.toCharArray();
4529
Arrays.sort(charArray);
@@ -71,4 +55,12 @@ public static boolean isPermutation_2(String s1, String s2){
7155

7256
return true;
7357
}
58+
59+
/* Tests code */
60+
private static void test(String s1, String s2){
61+
System.out.println(s1 + " " + s2);
62+
System.out.println("Solution 1: " + isPermutation_1(s1, s2));
63+
System.out.println("Solution 2: " + isPermutation_2(s1, s2));
64+
System.out.println();
65+
}
7466
}

src/chapter1/OnePoint4.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package chapter1;
22

33
public class OnePoint4 {
4+
/* Important: To simplify finding "true length", we only use Strings with 2 spaces to replace,
5+
* and 4 blank spaces at end */
46
public static void main (String [] args){
5-
char [] sentence = {'D', 'a', 'd', ' ', 'i', 's', ' ', 's', 'm', 'a', 'r', 't', '\0', '\0', '\0', '\0'};
6-
System.out.println(sentence);
7-
replaceWhitespace(sentence, sentence.length - 4);
8-
System.out.println(sentence);
7+
test("Dad is smart ");
98
}
109

10+
/* Replaces whitespace in char[] with %20 */
1111
public static void replaceWhitespace(char [] sentence, int trueLength){
1212
/* Strings are immutable in JAVA. That's why the book had us use a character array */
1313
int numberOfSpaces = 0;
@@ -30,4 +30,13 @@ public static void replaceWhitespace(char [] sentence, int trueLength){
3030
}
3131
}
3232
}
33+
34+
/* Tests code */
35+
private static void test(String s){
36+
char [] sentence = s.toCharArray();
37+
System.out.println(sentence);
38+
replaceWhitespace(sentence, sentence.length - 4); // -4 is hard-coded.
39+
System.out.println(sentence);
40+
System.out.println();
41+
}
3342
}

src/chapter1/OnePoint5.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,8 @@
22

33
public class OnePoint5 {
44
public static void main (String [] args){
5-
/* Test #1 */
6-
String original = "aabcccccaaa";
7-
System.out.println("Original string = " + original);
8-
String compressed = basicCompression_1(original);
9-
System.out.println(compressed);
10-
String compressed_2 = basicCompression_2(original);
11-
System.out.println(compressed_2);
12-
13-
System.out.println();
14-
15-
/* Test #2 */
16-
original = "abababa";
17-
System.out.println("Original string = " + original);
18-
compressed = basicCompression_1(original);
19-
System.out.println(compressed);
20-
compressed_2 = basicCompression_2(original);
21-
System.out.println(compressed_2);
5+
test("aabcccccaaa");
6+
test("abababa");
227
}
238

249
/******************************************************************************/
@@ -128,4 +113,14 @@ private static int writeToArray(char [] array, int index, char ch, int num){
128113
}
129114
return index;
130115
}
116+
117+
/* Tests code */
118+
private static void test(String original){
119+
System.out.println("Original string = " + original);
120+
String compressed = basicCompression_1(original);
121+
System.out.println("Solution 1: " + compressed);
122+
String compressed_2 = basicCompression_2(original);
123+
System.out.println("Solution 2: " + compressed_2);
124+
System.out.println();
125+
}
131126
}

0 commit comments

Comments
 (0)