Browse Source

Curtis Cook's Quiz 4

curtiscook 6 years ago
parent
commit
247f1978a6

+ 38
- 4
src/main/java/rocks/zipcode/io/quiz4/collections/ComparableTreeSet.java View File

@@ -1,17 +1,51 @@
1 1
 package rocks.zipcode.io.quiz4.collections;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.TreeSet;
5
+
3 6
 /**
4 7
  * @author leon on 11/12/2018.
5 8
  */
6
-public class ComparableTreeSet<_> {
9
+public class ComparableTreeSet<_> extends TreeSet implements Comparable {
10
+    _[] array;
11
+
7 12
     public ComparableTreeSet(_... arr) {
13
+//        TreeSet<_> treeSet = new TreeSet<>();
14
+//        for (_ generic : arr) {
15
+//            treeSet.add(generic);
16
+//        }
17
+//
18
+//        for (_ branch : treeSet) {
19
+//            System.out.println(branch);
20
+//        }
21
+
22
+        TreeSet<_> treeSet = new TreeSet<_>(Arrays.asList(arr));
23
+        treeSet.toString();
24
+//        array = (_[]) treeSet.toArray();
8 25
     }
9 26
 
10 27
 
11 28
     public ComparableTreeSet() {
12 29
     }
13 30
 
14
-    public Integer compareTo(ComparableTreeSet<_> o) {
15
-        return null;
31
+//    public Integer compareTo(ComparableTreeSet<_> o) {
32
+//        return null;
33
+//    }
34
+
35
+    @Override
36
+    public int compareTo(Object o) {
37
+        for (int i = 0; i < array.length; i++) {
38
+            if (array[i].hashCode() > o.hashCode()) {
39
+                return 1;
40
+            } else {
41
+                return -1;
42
+            }
43
+        }
44
+        return 0;
45
+    }
46
+
47
+    @Override
48
+    public String toString() {
49
+        return super.toString();
16 50
     }
17
-}
51
+}

+ 25
- 3
src/main/java/rocks/zipcode/io/quiz4/fundamentals/PalindromeEvaluator.java View File

@@ -1,18 +1,40 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3
+import java.util.TreeSet;
4
+
3 5
 /**
4 6
  * @author leon on 18/12/2018.
5 7
  */
6 8
 public class PalindromeEvaluator {
7 9
     public static String[] getAllPalindromes(String string) {
8
-        return null;
10
+        TreeSet<String> treeSet = new TreeSet<>();
11
+        for (int i = 0; i < string.length(); i++) {
12
+            treeSet.add(String.valueOf(string.charAt(i)));
13
+        }
14
+
15
+        for (int i = 0; i < string.length() / 2; i++) {
16
+            if(string.charAt(i) == string.charAt(string.length() - 1 - i)) {
17
+                treeSet.add(string.substring(i, string.length() - i));
18
+            }
19
+        }
20
+        return treeSet.toArray(new String[0]);
9 21
     }
10 22
 
11 23
     public static Boolean isPalindrome(String string) {
12
-        return null;
24
+        for (int i = 0; i < string.length() - 1; i++) {
25
+            if (string.charAt(i) != string.charAt(string.length() - 1 - i)) {
26
+                return false;
27
+            }
28
+        }
29
+        return true;
13 30
     }
14 31
 
15 32
     public static String reverseString(String string) {
16
-        return null;
33
+        String[] reversedString = new String[string.length()];
34
+        StringBuilder sb = new StringBuilder(string.length());
35
+        for (int i = string.length() - 1, j = 0; i >= 0; i--, j++) {
36
+            sb.append(string.charAt(i));
37
+        }
38
+        return sb.toString();
17 39
     }
18 40
 }

+ 32
- 1
src/main/java/rocks/zipcode/io/quiz4/fundamentals/StringEvaluator.java View File

@@ -1,11 +1,42 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3
+import java.util.TreeSet;
4
+
3 5
 /**
4 6
  * @author leon on 11/12/2018.
5 7
  */
6 8
 public class StringEvaluator {
7 9
     public static String[] getAllPrefixes(String string) {
8
-        return null;
10
+        TreeSet<String> treeSet = new TreeSet<>();
11
+        for (int i = 0, j = string.length(); i < string.length(); i++, j--) {
12
+//            treeSet.add(String.valueOf(string.charAt(i)));
13
+//            System.out.println(string.charAt(i));
14
+//            treeSet.add(string.substring(i));
15
+//            System.out.println(string.substring(i));
16
+//            if (i > 1) {
17
+//                treeSet.add(string.substring(0, i));
18
+//                System.out.println(string.substring(0, i));
19
+//            }
20
+//            if (j > i) {
21
+//                treeSet.add(string.substring(i, j));
22
+//                System.out.println(string.substring(i, j));
23
+//            }
24
+            treeSet.add(String.valueOf(string.charAt(i)));
25
+            treeSet.add(string.substring(i));
26
+            if (i > 0 && j > 1) {
27
+                treeSet.add(string.substring(1, j));
28
+            }
29
+            if (j > i) {
30
+                treeSet.add(string.substring(i, j));
31
+                System.out.println(string.substring(i, j));
32
+            }
33
+            if (j > 0) {
34
+                treeSet.add(string.substring(0, j));
35
+                System.out.println(string.substring(0, j));
36
+            }
37
+        }
38
+
39
+        return treeSet.toArray(new String[0]);
9 40
     }
10 41
 
11 42
     public static String[] getCommonPrefixes(String string1, String string2) {

+ 1
- 1
src/main/java/rocks/zipcode/io/quiz4/generics/Group.java View File

@@ -7,7 +7,7 @@ import java.util.Iterator;
7 7
  */
8 8
 public class Group<_> {
9 9
     public Group() {
10
-        throw new UnsupportedOperationException("Method not yet implemented");
10
+//        throw new UnsupportedOperationException("Method not yet implemented");
11 11
     }
12 12
 
13 13
     public Integer count() {

+ 15
- 4
src/main/java/rocks/zipcode/io/quiz4/generics/MyStack.java View File

@@ -1,25 +1,36 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+import java.util.Iterator;
4
+import java.util.Stack;
5
+
3 6
 /**
4 7
  * @author leon on 11/12/2018.
5 8
  */
6
-public class MyStack<SomeType> {
9
+public class MyStack<SomeType> implements Iterable {
10
+    Stack<SomeType> someTypeStack = new Stack<>();
11
+
7 12
     public MyStack() {
8
-        throw new UnsupportedOperationException("Method not yet implemented");
13
+
9 14
     }
10 15
 
11 16
     public Boolean isEmpty() {
12
-        return null;
17
+        return someTypeStack.empty();
13 18
     }
14 19
 
15 20
     public void push(SomeType i) {
21
+        someTypeStack.push(i);
16 22
     }
17 23
 
18 24
     public SomeType peek() {
19
-        throw new UnsupportedOperationException("Method not yet implemented");
25
+        return someTypeStack.peek();
20 26
     }
21 27
 
22 28
     public SomeType pop() {
29
+        return someTypeStack.pop();
30
+    }
31
+
32
+    @Override
33
+    public Iterator iterator() {
23 34
         return null;
24 35
     }
25 36
 }

+ 20
- 5
src/main/java/rocks/zipcode/io/quiz4/generics/SortedGroup.java View File

@@ -3,16 +3,31 @@ package rocks.zipcode.io.quiz4.generics;
3 3
 /**
4 4
  * @author leon on 18/12/2018.
5 5
  */
6
-public class SortedGroup<_> extends Group<_> {
6
+public class SortedGroup<_> extends Group<_> implements GroupInterface {
7
+//    @Override
8
+//    public void insert(_ value) {
9
+//    }
10
+//
11
+//    @Override
12
+//    public void delete(_ value) {
13
+//    }
14
+
15
+    public Integer indexOf(_ firstValue) {
16
+        return null;
17
+    }
18
+
7 19
     @Override
8
-    public void insert(_ value) {
20
+    public Boolean has(Object valueToInsert) {
21
+        return null;
9 22
     }
10 23
 
11 24
     @Override
12
-    public void delete(_ value) {
25
+    public void insert(Object string) {
26
+
13 27
     }
14 28
 
15
-    public Integer indexOf(_ firstValue) {
16
-        return null;
29
+    @Override
30
+    public void delete(Object valueToInsert) {
31
+
17 32
     }
18 33
 }