Yesoda Sanka 6 anos atrás
pai
commit
112d1f5d44

+ 4
- 2
src/main/java/rocks/zipcode/io/quiz4/collections/ComparableTreeSet.java Ver arquivo

@@ -4,6 +4,7 @@ package rocks.zipcode.io.quiz4.collections;
4 4
  * @author leon on 11/12/2018.
5 5
  */
6 6
 public class ComparableTreeSet<_> {
7
+
7 8
     public ComparableTreeSet(_... arr) {
8 9
     }
9 10
 
@@ -11,7 +12,8 @@ public class ComparableTreeSet<_> {
11 12
     public ComparableTreeSet() {
12 13
     }
13 14
 
14
-    public Integer compareTo(ComparableTreeSet<_> o) {
15
-        return null;
15
+    public int compareTo(ComparableTreeSet<_> o) {
16
+
17
+        return 0;
16 18
     }
17 19
 }

+ 4
- 12
src/main/java/rocks/zipcode/io/quiz4/collections/SimpleStringGroup.java Ver arquivo

@@ -40,22 +40,14 @@ public class SimpleStringGroup {
40 40
     }
41 41
 
42 42
     public String fetch(int indexOfValue) {
43
-        String s="";
44
-    for(int i=0;i<list.size() ;i++ ){
45
-        if(list.equals(indexOfValue ) ){
46
-           s= list.get(indexOfValue ) ;
47 43
 
48
-        }
49
-    }
50
-        return s;
44
+
45
+        return list.get(indexOfValue );
51 46
     }
52 47
 
53 48
     public void delete(String valueToInsert) {
54
-        for(int i=0;i<list.size() ;i++ ){
55
-            if(list.equals(valueToInsert ) ){
56
-                list.remove(valueToInsert );
57
-            }
58
-        }
49
+//
50
+     list.remove(valueToInsert );
59 51
     }
60 52
 
61 53
     public void clear() {

+ 9
- 8
src/main/java/rocks/zipcode/io/quiz4/collections/ZipCodeWilmington.java Ver arquivo

@@ -2,31 +2,32 @@ package rocks.zipcode.io.quiz4.collections;
2 2
 
3 3
 import rocks.zipcode.io.quiz4.objectorientation.Student;
4 4
 
5
+import java.util.HashMap;
5 6
 import java.util.Map;
6 7
 
7 8
 /**
8 9
  * @author leon on 11/12/2018.
9 10
  */
10 11
 public class ZipCodeWilmington {
12
+    Map<Student,Double> map=new HashMap<>() ;
11 13
 
12 14
     public void enroll(Student student) {
15
+        map.put(student,0.0 );
13 16
 
14 17
     }
15 18
 
16 19
     public Boolean isEnrolled(Student student) {
17
-        boolean flag=false;
18
-        if (isEnrolled(student)) {
19
-            flag=true;
20
-        }
21
-        else
22
-            flag=false;
23
-        return flag;
20
+        return map.containsKey(student );
24 21
     }
25 22
 
26 23
     public void lecture(double numberOfHours) {
24
+        map.get(numberOfHours );
25
+
26
+
27 27
     }
28 28
 
29 29
     public Map<Student, Double> getStudyMap() {
30
-        return null;
30
+
31
+        return  null ;
31 32
     }
32 33
 }

+ 21
- 22
src/main/java/rocks/zipcode/io/quiz4/fundamentals/PalindromeEvaluator.java Ver arquivo

@@ -10,45 +10,44 @@ import java.util.List;
10 10
 public class PalindromeEvaluator {
11 11
     public static String[] getAllPalindromes(String string) {
12 12
 
13
-       StringBuilder  sb=new StringBuilder();
14
-
15
-        for(int i=0;i<=string.length();i++) {
13
+        List<String> l = new ArrayList<>();
14
+        for (int i = 0; i <= string.length(); i++) {
16 15
             for (int j = i; j < string.length(); j++) {
17
-                if (isPalindrome(string.substring(i, j + 1)))
18
-                    System.out.println(string.substring(i, j + 1));
19
-                sb.append(string.substring(i,j+1));
16
+                if (isPalindrome(string.substring(i, j+1 ))) {
17
+                //System.out.println(string.substring(i, j + 1));
18
+                    l.add(string.substring(i, j +1));
19
+                }
20 20
 
21 21
             }
22 22
         }
23
-        String names[] = new String[sb.length()];
24
-
23
+        String names[] = l.toArray(new String[l.size() ]);
24
+        System.out.println(names);
25 25
         return names;
26 26
     }
27 27
 
28 28
     public static Boolean isPalindrome(String string) {
29 29
 
30
-        StringBuilder sb=new StringBuilder();
31
-        boolean flag=false;
32
-        sb.append(string );
33
-        sb=sb.reverse();
30
+        StringBuilder sb = new StringBuilder();
31
+        boolean flag = false;
32
+        sb.append(string);
33
+        sb = sb.reverse();
34 34
 
35
-        String s=sb.toString();
36
-        if(s.equals(string) ){
37
-            flag=true;
38
-        }
39
-        else
40
-            flag=false;
35
+        String s = sb.toString();
36
+        if (s.equals(string)) {
37
+            flag = true;
38
+        } else
39
+            flag = false;
41 40
 
42 41
         return flag;
43 42
     }
44 43
 
45 44
     public static String reverseString(String string) {
46
-        StringBuilder sb=new StringBuilder();
45
+        StringBuilder sb = new StringBuilder();
47 46
 
48
-        sb.append(string );
49
-        sb=sb.reverse();
47
+        sb.append(string);
48
+        sb = sb.reverse();
50 49
 
51
-        String s=sb.toString();
50
+        String s = sb.toString();
52 51
 
53 52
         return s;
54 53
     }

+ 33
- 39
src/main/java/rocks/zipcode/io/quiz4/fundamentals/StringEvaluator.java Ver arquivo

@@ -1,67 +1,61 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3
-import java.util.List;
3
+import com.sun.deploy.util.StringUtils;
4
+
5
+import java.util.*;
4 6
 
5 7
 /**
6 8
  * @author leon on 11/12/2018.
7 9
  */
8
-public class StringEvaluator {
10
+
11
+    public class StringEvaluator {
12
+
9 13
     public static String[] getAllPrefixes(String string) {
10
-        String[] output = new String[string .length()];
11
-        StringBuilder sb=new StringBuilder();
12
-        for (int index = 0; index < string.length(); index++) {
13
-           output[index]=string.substring(index );
14
-//
14
+
15
+        List<String> list = new ArrayList<>();
16
+        int len = string.length();
17
+        for (int index = 0; index < len; index++) {
18
+            for (int j = string.length(); j > index; j--) {
19
+                if (!list.contains(string.substring(index, j))) {
20
+                    list.add(string.substring(index, j));
21
+                }
22
+            }
23
+
24
+
15 25
         }
26
+        String[] output = list.toArray(new String[list.size()]);
16 27
 
17 28
         return output;
18 29
 
19 30
     }
20 31
 
21 32
     public static String[] getCommonPrefixes(String string1, String string2) {
22
-        String result = "";
33
+        ArrayList<String> str1 = new ArrayList<>(Arrays.asList(getAllPrefixes(string1)));
34
+        ArrayList<String> str2 = new ArrayList<>(Arrays.asList(getAllPrefixes(string2)));
35
+        ArrayList<String> common = new ArrayList<>();
23 36
 
24
-        int n1 = string1.length(), n2 = string2.length();
25
-
26
-        for (int i = 0, j = 0; i <= n1 - 1 && j <= n2 - 1; i++, j++) {
27
-            if (string1.charAt(i) != string2.charAt(j)) {
28
-                break;
37
+        for (String s : str1) {
38
+            if (str2.contains(s) && !common.contains(s)) {
39
+                common.add(s);
29 40
             }
30
-            result += string1.charAt(i);
31
-        }
32
-        String[] array = new String[result .length()];
33
-        for(int i = 0; i < result.length(); i++)
34
-        {
35
-            array[i] = String.valueOf(result.charAt(i));
36 41
         }
37 42
 
38 43
 
39
-        return array;
44
+        return common.toArray(new String[common.size()]);
45
+
40 46
     }
41 47
 
42 48
     public static String getLargestCommonPrefix(String string1, String string2) {
43
-        if (string1 == null || string2 == null){
44
-            throw new IllegalArgumentException();
45
-        }
46
-
47
-        int minLength = 0;
48
-
49
-        if (string1.length() < string2.length()){
50
-            minLength = string1.length();
51
-        }
52
-        else{
53
-            minLength = string2.length();
54
-        }
55 49
 
56
-        for (int i = 0 ; i < minLength ; i++){
57 50
 
58
-            if(string1.charAt(i) == string2.charAt(i)){
59
-                continue;
60
-            }
61
-            else{
62
-                return string1.substring(0,i);
51
+        String s = "";
52
+        String[] prefix = getCommonPrefixes(string1, string2);
53
+        for (String str : prefix) {
54
+            if (str.length() > s.length()) {
55
+                 s=str;
63 56
             }
64 57
         }
65
-        return string1.substring(0,minLength);
58
+        return s;
66 59
     }
67 60
 }
61
+

+ 1
- 0
src/main/java/rocks/zipcode/io/quiz4/generics/GenericUtils.java Ver arquivo

@@ -9,6 +9,7 @@ import java.util.TreeSet;
9 9
  */
10 10
 public class GenericUtils {
11 11
     public static <_ extends Comparable> Iterable<Iterable<_>> powerSet(Set<_> originalSet) {
12
+
12 13
         return null;
13 14
     }
14 15
 

+ 27
- 5
src/main/java/rocks/zipcode/io/quiz4/generics/Group.java Ver arquivo

@@ -1,37 +1,59 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+import java.util.ArrayList;
3 4
 import java.util.Iterator;
5
+import java.util.List;
4 6
 
5 7
 /**
6 8
  * @author leon on 18/12/2018.
7 9
  */
8 10
 public class Group<_> {
11
+
12
+    List<_> list=new ArrayList<_>();
13
+
9 14
     public Group() {
10
-        throw new UnsupportedOperationException("Method not yet implemented");
15
+
16
+        //throw new UnsupportedOperationException("Method not yet implemented");
11 17
     }
12 18
 
13 19
     public Integer count() {
14
-        return null;
20
+
21
+        return list.size() ;
15 22
     }
16 23
 
17 24
     public void insert(_ value) {
25
+        list.add(value);
18 26
     }
19 27
 
20 28
     public Boolean has(_ value) {
21
-        return null;
29
+
30
+        return list.contains(value);
22 31
     }
23 32
 
24 33
     public _ fetch(int indexOfValue) {
25
-        return null;
34
+
35
+        return list.get(indexOfValue );
26 36
     }
27 37
 
28 38
     public void delete(_ value) {
39
+        list.remove(value );
29 40
     }
30 41
 
31 42
     public void clear() {
43
+        list.clear() ;
32 44
     }
33 45
 
34 46
     public Iterator<_> iterator() {
35
-        return null;
47
+
48
+        //StringBuilder sb=new StringBuilder() ;
49
+        List<_ >lit=new ArrayList<>() ;
50
+        Iterator<_> iterator =list.iterator() ;
51
+
52
+        while(iterator.hasNext() ) {
53
+            lit.add (iterator.next()) ;
54
+        }
55
+
56
+
57
+    return  null;
36 58
     }
37 59
 }

+ 32
- 4
src/main/java/rocks/zipcode/io/quiz4/generics/MyStack.java Ver arquivo

@@ -1,25 +1,53 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+import java.util.*;
4
+import java.util.function.Consumer;
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
+
11
+    //List<SomeType > list=new ArrayList<SomeType >();
12
+    Stack<SomeType >stack =new Stack<SomeType >();
7 13
     public MyStack() {
8
-        throw new UnsupportedOperationException("Method not yet implemented");
14
+
15
+        //throw new UnsupportedOperationException("Method not yet implemented");
9 16
     }
10 17
 
11 18
     public Boolean isEmpty() {
12
-        return null;
19
+
20
+        return stack.isEmpty() ;
13 21
     }
14 22
 
15 23
     public void push(SomeType i) {
24
+
25
+        stack.push(i);
16 26
     }
17 27
 
18 28
     public SomeType peek() {
19
-        throw new UnsupportedOperationException("Method not yet implemented");
29
+
30
+        //throw new UnsupportedOperationException("Method not yet implemented");
31
+        return stack.peek() ;
20 32
     }
21 33
 
22 34
     public SomeType pop() {
35
+
36
+        return stack.pop();
37
+    }
38
+
39
+    @Override
40
+    public Iterator iterator() {
41
+        return null;
42
+    }
43
+
44
+    @Override
45
+    public void forEach(Consumer action) {
46
+
47
+    }
48
+
49
+    @Override
50
+    public Spliterator spliterator() {
23 51
         return null;
24 52
     }
25 53
 }

+ 14
- 1
src/main/java/rocks/zipcode/io/quiz4/generics/SortedGroup.java Ver arquivo

@@ -1,18 +1,31 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+
4
+
5
+import java.util.List;
6
+import java.util.*;
7
+
3 8
 /**
4 9
  * @author leon on 18/12/2018.
5 10
  */
6 11
 public class SortedGroup<_> extends Group<_> {
12
+
13
+
7 14
     @Override
8 15
     public void insert(_ value) {
16
+
17
+        list.add(value);
18
+        Collections.sort(list,null);
9 19
     }
10 20
 
11 21
     @Override
12 22
     public void delete(_ value) {
23
+        list.remove(value);
13 24
     }
14 25
 
15 26
     public Integer indexOf(_ firstValue) {
16
-        return null;
27
+        Collections.sort(list,null);
28
+
29
+        return  list.indexOf(firstValue );
17 30
     }
18 31
 }

+ 10
- 3
src/main/java/rocks/zipcode/io/quiz4/objectorientation/StringAssembler.java Ver arquivo

@@ -4,6 +4,7 @@ package rocks.zipcode.io.quiz4.objectorientation;
4 4
  * @author leon on 11/12/2018.
5 5
  */
6 6
 public class StringAssembler {
7
+
7 8
     Character delineter;
8 9
 
9 10
     public StringAssembler(Character delimeter) {
@@ -13,12 +14,18 @@ public class StringAssembler {
13 14
 
14 15
     public StringAssembler append(String str) {
15 16
 
16
-
17
-        return  null;
17
+         StringBuilder sb=new StringBuilder();
18
+         assemble() ;
19
+         sb.append(delineter );
20
+         sb.append(str);
21
+         assemble() ;
22
+        return null ;
18 23
     }
19 24
 
20 25
     public String assemble() {
26
+        String s="";
27
+       return s.concat(delineter .toString() );
21 28
 
22
-        return null;
29
+        //return null;
23 30
     }
24 31
 }

+ 41
- 14
src/main/java/rocks/zipcode/io/quiz4/objectorientation/StringEvaluatorObject.java Ver arquivo

@@ -1,5 +1,9 @@
1 1
 package rocks.zipcode.io.quiz4.objectorientation;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.List;
6
+
3 7
 /**
4 8
  * @author leon on 19/12/2018.
5 9
  */
@@ -11,29 +15,52 @@ public class StringEvaluatorObject {
11 15
 
12 16
     public String[] getAllPrefixes() {
13 17
 
18
+        List<String> list = new ArrayList<>();
19
+        int len = str.length();
20
+        for (int index = 0; index < len; index++) {
21
+            for (int j = str.length(); j > index; j--) {
22
+                if (!list.contains(str.substring(index, j))) {
23
+                    list.add(str.substring(index, j));
24
+                }
25
+            }
26
+
14 27
 
15
-        return null;
28
+        }
29
+        String[] output = list.toArray(new String[list.size()]);
30
+
31
+        return output;
16 32
     }
17 33
 
18 34
     public String[] getCommonPrefixes(String secondInput) {
19 35
 
20
-       // String prefix = "";
21
-        int minLength = Math.min(str.length(), secondInput.length());
22
-        String[] prefix =new String[minLength ];
23
-        for (int i = 0; i < minLength; i++) {
24
-            if (str.charAt(i) != secondInput.charAt(i)) {
25
-                prefix[i] = str.substring(0, i);
26
-                System.out.println(prefix [i]) ;
27
-                break;
28
-            }
29
-        }
36
+//        ArrayList<String> str1 = new ArrayList<>(Arrays.asList(getAllPrefixes(str)));
37
+//        ArrayList<String> str2 = new ArrayList<>(Arrays.asList(getAllPrefixes(secondInput)));
38
+//        ArrayList<String> common = new ArrayList<>();
39
+//
40
+//        for (String s : str1) {
41
+//            if (str2.contains(s) && !common.contains(s)) {
42
+//                common.add(s);
43
+//            }
44
+//        }
30 45
 
31
-        return prefix;
32 46
 
47
+        //return common.toArray(new String[common.size()]);
33 48
 
49
+     return null;
34 50
     }
35 51
 
36 52
     public String getLargestCommonPrefix(String secondInput) {
37
-        return null;
53
+
54
+
55
+        String s = "";
56
+
57
+        String[] prefix = getCommonPrefixes(str, secondInput );
58
+        for (String str : prefix) {
59
+            if (str.length() > s.length()) {
60
+                s=str;
61
+            }
62
+        }
63
+        return s;
64
+    }
38 65
     }
39
-}
66
+

+ 9
- 2
src/main/java/rocks/zipcode/io/quiz4/objectorientation/Student.java Ver arquivo

@@ -5,19 +5,26 @@ package rocks.zipcode.io.quiz4.objectorientation;
5 5
  */
6 6
 public class Student {
7 7
     Integer id;
8
+    Double studytime;
9
+
8 10
     public Student() {
9
-        this(null);
11
+
12
+    this.studytime =0.0;
13
+        // this(null);
10 14
     }
11 15
 
12 16
     public Student(Integer id) {
13 17
         this.id=id;
18
+        this.studytime =0.0;
14 19
     }
15 20
 
16 21
     public void learn(Double amountOfHours) {
22
+        this.studytime +=amountOfHours;
23
+
17 24
     }
18 25
 
19 26
     public Double getTotalStudyTime() {
20 27
 
21
-        return null;
28
+        return this.studytime ;
22 29
     }
23 30
 }

+ 1
- 1
src/test/java/rocks/zipcode/io/quiz4/fundamentals/stringevaluator/GetCommonSuffixesTest.java Ver arquivo

@@ -12,7 +12,7 @@ public class GetCommonSuffixesTest {
12 12
     public void test1() {
13 13
         // given
14 14
         String firstInput = "AAB";
15
-        String secondInput = "AAAB";
15
+        String secondInput = "ABAA";
16 16
         String[] expected = {"A", "AA", "AAB", "AB", "B"};
17 17
 
18 18
         // when