#34 committed

Open
yesoda wants to merge 6 commits from yesoda/Quiz3:master into master

+ 21
- 1
src/main/java/rocks/zipcode/io/quiz3/arrays/SquareArrayAnalyzer.java View File

1
 package rocks.zipcode.io.quiz3.arrays;
1
 package rocks.zipcode.io.quiz3.arrays;
2
 
2
 
3
+import java.util.Arrays;
4
+
3
 /**
5
 /**
4
  * @author leon on 09/12/2018.
6
  * @author leon on 09/12/2018.
7
+ * Given two arrays `a` and `b` write a method `compare(a, b)`
8
+ * that returns true if the two arrays have the "same" elements, with the same multiplicities. "
9
+ * Same" means, here, that the elements
10
+ * in `b` are the elements in `a` squared, regardless of the order.
5
  */
11
  */
6
 public class SquareArrayAnalyzer {
12
 public class SquareArrayAnalyzer {
13
+
14
+
7
     public static Boolean compare(Integer[] input, Integer[] squaredValues) {
15
     public static Boolean compare(Integer[] input, Integer[] squaredValues) {
8
-        return null;
16
+        Arrays.sort(input);
17
+        Arrays.sort(squaredValues );
18
+        boolean flag=false;
19
+        for(int i=0;i<input.length;i++ ){
20
+            if(squaredValues[i]%input[i]==0 ){
21
+                flag=true;
22
+            }
23
+            else
24
+                flag=false;
25
+        }
26
+
27
+
28
+        return flag;
9
     }
29
     }
10
 }
30
 }

+ 65
- 3
src/main/java/rocks/zipcode/io/quiz3/arrays/TicTacToe.java View File

1
 package rocks.zipcode.io.quiz3.arrays;
1
 package rocks.zipcode.io.quiz3.arrays;
2
 
2
 
3
+import java.util.Arrays;
4
+
3
 /**
5
 /**
4
  * @author leon on 09/12/2018.
6
  * @author leon on 09/12/2018.
5
  */
7
  */
6
 public class TicTacToe {
8
 public class TicTacToe {
9
+    String[][] board;
10
+
7
 
11
 
8
     public TicTacToe(String[][] board) {
12
     public TicTacToe(String[][] board) {
13
+
14
+        this.board = board;
9
     }
15
     }
10
 
16
 
11
     public String[] getRow(Integer value) {
17
     public String[] getRow(Integer value) {
18
+
12
         return null;
19
         return null;
13
     }
20
     }
14
 
21
 
15
     public String[] getColumn(Integer value) {
22
     public String[] getColumn(Integer value) {
23
+
16
         return null;
24
         return null;
17
     }
25
     }
18
 
26
 
19
     public Boolean isRowHomogenous(Integer rowIndex) {
27
     public Boolean isRowHomogenous(Integer rowIndex) {
20
-        return null;
28
+        for (int i = 0; i < rowIndex; i++) {
29
+            return true;
30
+
31
+        }
32
+        return false;
21
     }
33
     }
22
 
34
 
23
     public Boolean isColumnHomogeneous(Integer columnIndex) {
35
     public Boolean isColumnHomogeneous(Integer columnIndex) {
24
-        return null;
36
+      for(int i=0;i<columnIndex;i++ ) {
37
+          return true;
38
+      }
39
+
40
+          return false;
25
     }
41
     }
26
 
42
 
27
     public String getWinner() {
43
     public String getWinner() {
28
-        return null;
44
+
45
+
46
+        String[] board = new String[9];
47
+        String turn = "X";
48
+        for (int a = 0; a < 8; a++) {
49
+            String line = null;
50
+            switch (a) {
51
+                case 0:
52
+                    line = board[0] + board[1] + board[2];
53
+                    break;
54
+                case 1:
55
+                    line = board[3] + board[4] + board[5];
56
+                    break;
57
+                case 2:
58
+                    line = board[6] + board[7] + board[8];
59
+                    break;
60
+                case 3:
61
+                    line = board[0] + board[3] + board[6];
62
+                    break;
63
+                case 4:
64
+                    line = board[1] + board[4] + board[7];
65
+                    break;
66
+                case 5:
67
+                    line = board[2] + board[5] + board[8];
68
+                    break;
69
+                case 6:
70
+                    line = board[0] + board[4] + board[8];
71
+                    break;
72
+                case 7:
73
+                    line = board[2] + board[4] + board[6];
74
+                    break;
75
+            }
76
+            if (line.equals("XXX")) {
77
+                return "X";
78
+            } else if (line.equals("OOO")) {
79
+                return "O";
80
+            }
81
+        }
82
+        for (int a = 0; a < 9; a++) {
83
+            if (Arrays.asList(board).contains(String.valueOf(a+1))) {
84
+                break;
85
+            }
86
+            else if (a == 8)
87
+                return turn;
88
+        }
89
+        return turn;
90
+
29
     }
91
     }
30
 }
92
 }

+ 37
- 1
src/main/java/rocks/zipcode/io/quiz3/arrays/WaveGenerator.java View File

1
 package rocks.zipcode.io.quiz3.arrays;
1
 package rocks.zipcode.io.quiz3.arrays;
2
 
2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
3
 /**
8
 /**
4
  * @author leon on 09/12/2018.
9
  * @author leon on 09/12/2018.
5
  */
10
  */
6
 public class WaveGenerator {
11
 public class WaveGenerator {
7
     public static String[] wave(String str) {
12
     public static String[] wave(String str) {
8
-        return null;
13
+//
14
+//        // iterate through string
15
+//        // for every character in string
16
+//        // capitalize that character and return that string
17
+
18
+
19
+          int n = str.length();
20
+          String s = str.toLowerCase();
21
+          ArrayList <String> st = new ArrayList<>();
22
+          StringBuffer temp = new StringBuffer();
23
+
24
+
25
+          String alpha;
26
+          for (int i = 0; i < n; i++) {
27
+
28
+              alpha = s.substring(i, i + 1);
29
+              if (isAlphaString(alpha)) {
30
+                  temp = new StringBuffer();
31
+                  temp.append(s.substring(0, i));
32
+                  temp.append(s.substring(i, i + 1).toUpperCase());
33
+                  temp.append(s.substring(i + 1));
34
+
35
+                  st.add(temp.toString());
36
+              }
37
+          }
38
+        return st.toArray(new String[st.size()]);
39
+    }
40
+
41
+
42
+    public static Boolean isAlphaString(String string) {
43
+
44
+        return string .matches("[a-zA-Z]+");
9
     }
45
     }
10
 }
46
 }

+ 13
- 4
src/main/java/rocks/zipcode/io/quiz3/collections/Lab.java View File

4
  * @author leon on 10/12/2018.
4
  * @author leon on 10/12/2018.
5
  */
5
  */
6
 public class Lab {
6
 public class Lab {
7
+      String labName;
8
+      String labStatus;
9
+
7
     public Lab() {
10
     public Lab() {
8
-        this(null);
11
+        //this(null);
9
     }
12
     }
10
 
13
 
11
     public Lab(String labName) {
14
     public Lab(String labName) {
15
+
16
+        this.labName =labName ;
12
     }
17
     }
13
 
18
 
14
-    public String getLabStatus() {
15
-        return null;
19
+   public String getLabStatus() {
20
+
21
+        return labStatus ;
16
     }
22
     }
17
 
23
 
18
     public void setLabStatus(String labStatus) {
24
     public void setLabStatus(String labStatus) {
25
+
26
+        this.labStatus =labStatus ;
19
     }
27
     }
20
 
28
 
21
     public String getName() {
29
     public String getName() {
22
-        return null;
30
+
31
+        return labName ;
23
     }
32
     }
24
 }
33
 }

+ 20
- 4
src/main/java/rocks/zipcode/io/quiz3/collections/Student.java View File

1
 package rocks.zipcode.io.quiz3.collections;
1
 package rocks.zipcode.io.quiz3.collections;
2
 
2
 
3
+import javafx.scene.Parent;
3
 import rocks.zipcode.io.quiz3.objectorientation.enums.LabStatus;
4
 import rocks.zipcode.io.quiz3.objectorientation.enums.LabStatus;
4
 
5
 
6
+import javax.xml.soap.SOAPPart;
7
+import java.util.HashMap;
5
 import java.util.Map;
8
 import java.util.Map;
6
 
9
 
7
 /**
10
 /**
8
  * @author leon on 10/12/2018.
11
  * @author leon on 10/12/2018.
9
  */
12
  */
10
 public class Student {
13
 public class Student {
14
+
15
+    Map<Lab, LabStatus >hm;
16
+
11
     public Student() {
17
     public Student() {
12
-        this(null);
18
+
19
+        this.hm =new HashMap<>();
20
+        //this(null);
13
     }
21
     }
14
 
22
 
15
     public Student(Map<Lab, LabStatus> map) {
23
     public Student(Map<Lab, LabStatus> map) {
16
-    }
17
 
24
 
18
-    public void setLabStatus(Lab lab, LabStatus labStatus) {
25
+        this.hm=map;
19
     }
26
     }
20
 
27
 
28
+    public void setLabStatus(Lab lab, LabStatus labStatus) {
29
+      hm.put(lab,labStatus );
21
 
30
 
31
+    }
22
     public void forkLab(Lab lab) {
32
     public void forkLab(Lab lab) {
33
+        LabStatus temp = hm.put(lab, LabStatus.PENDING);
34
+        System .out.println(temp);
23
     }
35
     }
24
 
36
 
25
     public LabStatus getLabStatus(String labName) {
37
     public LabStatus getLabStatus(String labName) {
26
-        throw new UnsupportedOperationException("Method not yet implemented");
38
+        try {
39
+            return hm.get(labName);
40
+        } catch (Exception e) {
41
+            throw new UnsupportedOperationException("Method not yet implemented");
42
+        }
27
     }
43
     }
28
 }
44
 }

+ 57
- 1
src/main/java/rocks/zipcode/io/quiz3/fundamentals/PigLatinGenerator.java View File

1
 package rocks.zipcode.io.quiz3.fundamentals;
1
 package rocks.zipcode.io.quiz3.fundamentals;
2
 
2
 
3
+import java.util.regex.Pattern;
4
+
3
 /**
5
 /**
4
  * @author leon on 09/12/2018.
6
  * @author leon on 09/12/2018.
7
+ * Pig Latin is an English language game where the goal is to hide the meaning of a word f
8
+ * rom people not aware of the rules.
9
+ * * The rules themselves are rather easy:
10
+ * * 1) The word starts with a vowel(a,e,i,o,u) -> return the original string plus "way".
11
+ * * 2) The word starts with a consonant -> move consonants from the beginning of the word
12
+ * to the end of the word until the first vowel, then return it plus "ay".
13
+ * * 3) The result must be lowercase, regardless of the case of the input. If the input
14
+ * string has any non-alpha characters, the function must return None, null, Nothing (depending on the language).
15
+ * * 4) The function must also handle simple random strings and not just English words.
16
+ * * 5) The input string has no vowels -> return the original string plus "ay".
17
+ * * For example, the word "spaghetti" becomes "aghettispay" because the first two letters ("sp")
18
+ * are consonants, so they are moved to the end of the string and "ay" is appended.
19
+ *
5
  */
20
  */
6
 public class PigLatinGenerator {
21
 public class PigLatinGenerator {
22
+
7
     public String translate(String str) {
23
     public String translate(String str) {
8
-        return null;
24
+        String s = "";
25
+        char ch[] = str.toCharArray();
26
+
27
+
28
+        String[] words = str.split("\\s+");
29
+        String answer = "";
30
+
31
+        if (str.startsWith("a") || str.startsWith("e") || str.startsWith("i") || str.startsWith("o") || str.startsWith("u")) {
32
+            System.out.print(str + "way");
33
+            return str.concat("way");
34
+        } else {
35
+            answer = str.substring(2, str.length());
36
+            String answer2 = str.substring(1, str.length());
37
+            String answer3 = str.substring(3, str.length());
38
+            String answer4 = str.substring(4, str.length());
39
+            String d = str.substring(0, 4);
40
+            if (!(d.contains("a") || d.contains("e") || d.contains("i") || d.contains("o") || d.contains("u"))) {
41
+               // System.out.print(answer4 + d + "ay");
42
+                return answer4.concat(d).concat("ay");
43
+            } else {
44
+                String c = str.substring(0, 3);
45
+                if (!(c.contains("a") || c.contains("e") || c.contains("i") || c.contains("o") || c.contains("u"))) {
46
+                   // System.out.print(answer3 + c + "ay");
47
+                    return answer3.concat(c).concat("ay");
48
+                } else {
49
+                    String b = str.substring(0, 2);
50
+                    if (!(b.contains("a") || b.contains("e") || b.contains("i") || b.contains("o") || b.contains("u"))) {
51
+                      //  System.out.print(answer + b + "ay");
52
+                        return answer.concat(b).concat("ay");
53
+                    } else {
54
+                        String a = str.substring(0, 1);
55
+                        if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u"))) {
56
+                           // System.out.print(answer2 + a + "ay");
57
+                            return answer2.concat(a).concat("ay");
58
+                        }
59
+                    }
60
+                }
61
+            }
62
+        }
63
+     return null;
9
     }
64
     }
10
 }
65
 }
66
+

+ 16
- 2
src/main/java/rocks/zipcode/io/quiz3/fundamentals/StringUtils.java View File

5
  */
5
  */
6
 public class StringUtils {
6
 public class StringUtils {
7
     public static String capitalizeNthCharacter(String str, Integer indexToCapitalize) {
7
     public static String capitalizeNthCharacter(String str, Integer indexToCapitalize) {
8
-        return null;
8
+        char[] arr2 = str.toCharArray();
9
+
10
+        arr2[indexToCapitalize] = Character.toUpperCase(arr2[indexToCapitalize]);
11
+        String str2 = new String(arr2);
12
+ return str2;
13
+
9
     }
14
     }
10
 
15
 
11
     public static Boolean isCharacterAtIndex(String baseString, Character characterToCheckFor, Integer indexOfString) {
16
     public static Boolean isCharacterAtIndex(String baseString, Character characterToCheckFor, Integer indexOfString) {
12
-        return null;
17
+
18
+
19
+       String s=Character .toString(baseString .charAt(indexOfString ));
20
+       if (s.equals(characterToCheckFor ) ){
21
+           return true;
22
+       }
23
+       else
24
+           return false;
25
+
26
+
13
     }
27
     }
14
 }
28
 }

+ 44
- 3
src/main/java/rocks/zipcode/io/quiz3/fundamentals/VowelUtils.java View File

1
 package rocks.zipcode.io.quiz3.fundamentals;
1
 package rocks.zipcode.io.quiz3.fundamentals;
2
 
2
 
3
+import java.util.regex.Matcher;
4
+import java.util.regex.Pattern;
5
+
3
 /**
6
 /**
4
  * @author leon on 09/12/2018.
7
  * @author leon on 09/12/2018.
5
  */
8
  */
6
 public class VowelUtils {
9
 public class VowelUtils {
7
     public static Boolean hasVowels(String word) {
10
     public static Boolean hasVowels(String word) {
8
-        return null;
11
+
12
+//        String x = word.toLowerCase();
13
+//        if(x.indexOf('e' || 'a' || 'i' || 'o' || 'u')) {
14
+//            return true;
15
+//        } else {
16
+//            return false;
17
+//        }
18
+
19
+        char[] words = word.toCharArray();
20
+        for(int i = 0; i<words.length; i++){
21
+            char z = words[i];
22
+            if (z=='a'||z=='e'||z=='i'||z=='o'||z=='u') return true;
23
+        }
24
+        return false;
9
     }
25
     }
10
 
26
 
11
     public static Integer getIndexOfFirstVowel(String word) {
27
     public static Integer getIndexOfFirstVowel(String word) {
28
+        for (int i = 0; i < word.length(); i++) {
29
+            char c = word.charAt(i);
30
+            if ("AEIOUaeiou".indexOf(c) != -1)
31
+                return word.indexOf(c);
32
+        }
12
         return null;
33
         return null;
13
     }
34
     }
14
 
35
 
15
 
36
 
37
+
38
+
39
+
16
     public static Boolean startsWithVowel(String word) {
40
     public static Boolean startsWithVowel(String word) {
17
-        return null;
41
+        char ch;
42
+
43
+        ch = word.charAt(0);
44
+
45
+        if(ch=='a' || ch=='A' || ch=='e' || ch=='E' ||
46
+                ch=='i' || ch=='I' || ch=='o' || ch=='O' ||
47
+                ch=='u' || ch=='U')
48
+        {
49
+           return true;
50
+        }
51
+        else
52
+        {
53
+           return false;
54
+        }
55
+
56
+
18
     }
57
     }
19
 
58
 
20
     public static Boolean isVowel(Character character) {
59
     public static Boolean isVowel(Character character) {
21
-        return null;
60
+
61
+
62
+       return "AEIOUaeiou".indexOf(character ) != -1;
22
     }
63
     }
23
 }
64
 }

+ 59
- 2
src/main/java/rocks/zipcode/io/quiz3/generics/ArrayUtility.java View File

1
 package rocks.zipcode.io.quiz3.generics;
1
 package rocks.zipcode.io.quiz3.generics;
2
 
2
 
3
+import java.util.*;
3
 import java.util.function.Function;
4
 import java.util.function.Function;
4
 
5
 
5
 /**
6
 /**
9
     private final SomeType[] array;
10
     private final SomeType[] array;
10
 
11
 
11
     public ArrayUtility(SomeType[] array) {
12
     public ArrayUtility(SomeType[] array) {
13
+
12
         this.array = array;
14
         this.array = array;
13
     }
15
     }
14
 
16
 
15
     public SomeType findOddOccurringValue() {
17
     public SomeType findOddOccurringValue() {
16
-        return null;
18
+        if(array.length==1){
19
+            return array[0];
20
+        }
21
+        Arrays.sort(array);
22
+        for(int i=0;i<array.length-1;){
23
+            if(array[i]==array[i+1]){
24
+                i+=2;
25
+            }else{
26
+                return array[i+1];
27
+            }
28
+        }
29
+        return array[array.length-1];
30
+
17
     }
31
     }
18
 
32
 
33
+
34
+
35
+
36
+
37
+
19
     public SomeType findEvenOccurringValue() {
38
     public SomeType findEvenOccurringValue() {
39
+
40
+//        if(array.length==1){
41
+//            return array[0];
42
+//        }
43
+//        Arrays.sort(array);
44
+//        for(int i=0;i<array.length-1;){
45
+//            if(array[i]!==array[i+1]){
46
+//                i+=2;
47
+//            }else{
48
+//                return array[i];
49
+//            }
50
+//        }
51
+//        return array[array.length-1];
52
+
53
+        for (int i = 0; i < array.length; i++) {
54
+            int count = 0;
55
+            for (int j = 0; j < array.length; j++) {
56
+                if (array[i] == array[j]) {
57
+                    count++;
58
+                }
59
+
60
+            }
61
+            if (count % 2 == 0) {
62
+                return array[i];
63
+            }
64
+
65
+        }
20
         return null;
66
         return null;
21
     }
67
     }
22
 
68
 
69
+
23
     public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
70
     public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
24
-        return null;
71
+
72
+        Arrays.sort(array);
73
+        int  nbOccurences = 0;
74
+
75
+        for (int i = 0, length = array.length ; i < length; i++) {
76
+            if (array[i] == valueToEvaluate ) {
77
+                nbOccurences++;
78
+            }
79
+        }
80
+       return nbOccurences;
25
     }
81
     }
26
 
82
 
27
     public SomeType[] filter(Function<SomeType, Boolean> predicate) {
83
     public SomeType[] filter(Function<SomeType, Boolean> predicate) {
84
+
28
         return null;
85
         return null;
29
     }
86
     }
30
 }
87
 }

+ 3
- 1
src/main/java/rocks/zipcode/io/quiz3/objectorientation/enums/LabStatus.java View File

4
  * @author leon on 10/12/2018.
4
  * @author leon on 10/12/2018.
5
  */
5
  */
6
 public enum LabStatus {
6
 public enum LabStatus {
7
-    ADD_ENUMERATIONS_HERE;
7
+    // ADD_ENUMERATIONS_HERE;
8
+    PENDING,COMPLETED,INCOMPLETE,
8
 }
9
 }
10
+

+ 4
- 1
src/main/java/rocks/zipcode/io/quiz3/objectorientation/enums/RockPaperScissorHandSign.java View File

4
  * @author leon on 09/12/2018.
4
  * @author leon on 09/12/2018.
5
  */
5
  */
6
 public enum RockPaperScissorHandSign {
6
 public enum RockPaperScissorHandSign {
7
-    ADD_ENUMERATIONS_HERE;
7
+    //ADD_ENUMERATIONS_HERE;
8
+    ROCK,PAPER,SESSIORS;
9
+
8
 
10
 
9
     public RockPaperScissorHandSign getWinner() {
11
     public RockPaperScissorHandSign getWinner() {
10
         return null;
12
         return null;
11
     }
13
     }
12
 
14
 
15
+
13
     public RockPaperScissorHandSign getLoser() {
16
     public RockPaperScissorHandSign getLoser() {
14
         return null;
17
         return null;
15
     }
18
     }

+ 1
- 1
src/test/java/rocks/zipcode/io/quiz3/arrays/wavegenerator/WaveTest.java View File

144
 
144
 
145
         // when
145
         // when
146
         String[] actual = WaveGenerator.wave(input);
146
         String[] actual = WaveGenerator.wave(input);
147
-
147
+        System.out.println(actual);
148
         // then
148
         // then
149
         TestUtils.assertArrayEquals(expected, actual);
149
         TestUtils.assertArrayEquals(expected, actual);
150
     }
150
     }