Sfoglia il codice sorgente

need to work on my habit to commit regularly

Yauheni Papko 6 anni fa
parent
commit
22d38872b5
3 ha cambiato i file con 139 aggiunte e 16 eliminazioni
  1. 82
    14
      StringArrayUtils.java
  2. 15
    2
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 82
- 14
StringArrayUtils.java Vedi File

@@ -1,4 +1,4 @@
1
- 
1
+import java.util.Arrays;
2 2
 
3 3
 /**
4 4
  * Created by leon on 1/29/18.
@@ -9,7 +9,7 @@ public class StringArrayUtils {
9 9
      * @return first element of specified array
10 10
      */ // TODO
11 11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13 13
     }
14 14
 
15 15
     /**
@@ -17,7 +17,7 @@ public class StringArrayUtils {
17 17
      * @return second element in specified array
18 18
      */
19 19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21 21
     }
22 22
 
23 23
     /**
@@ -25,7 +25,7 @@ public class StringArrayUtils {
25 25
      * @return last element in specified array
26 26
      */ // TODO
27 27
     public static String getLastElement(String[] array) {
28
-        return null;
28
+        return array[array.length-1];
29 29
     }
30 30
 
31 31
     /**
@@ -33,7 +33,7 @@ public class StringArrayUtils {
33 33
      * @return second to last element in specified array
34 34
      */ // TODO
35 35
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
36
+        return array[array.length-2];
37 37
     }
38 38
 
39 39
     /**
@@ -42,7 +42,13 @@ public class StringArrayUtils {
42 42
      * @return true if the array contains the specified `value`
43 43
      */ // TODO
44 44
     public static boolean contains(String[] array, String value) {
45
-        return false;
45
+        boolean output = false;
46
+        for (String element : array) {
47
+            if (element.equals(value)) {
48
+                output = true;
49
+            }
50
+        }
51
+        return output;
46 52
     }
47 53
 
48 54
     /**
@@ -50,7 +56,11 @@ public class StringArrayUtils {
50 56
      * @return an array with identical contents in reverse order
51 57
      */ // TODO
52 58
     public static String[] reverse(String[] array) {
53
-        return null;
59
+        String[] reversedArray = new String[array.length];
60
+        for (int i=array.length-1; i >= 0; i--) {
61
+            reversedArray[array.length-1-i] = array[i];
62
+        }
63
+        return reversedArray;
54 64
     }
55 65
 
56 66
     /**
@@ -58,7 +68,12 @@ public class StringArrayUtils {
58 68
      * @return true if the order of the array is the same backwards and forwards
59 69
      */ // TODO
60 70
     public static boolean isPalindromic(String[] array) {
61
-        return false;
71
+        boolean output = false;
72
+        String[] reversedArray = reverse(array);
73
+        if (Arrays.equals(array, reversedArray)) {
74
+            output = true;
75
+        }
76
+        return output;
62 77
     }
63 78
 
64 79
     /**
@@ -66,7 +81,19 @@ public class StringArrayUtils {
66 81
      * @return true if each letter in the alphabet has been used in the array
67 82
      */ // TODO
68 83
     public static boolean isPangramic(String[] array) {
69
-        return false;
84
+        boolean outcome = true;
85
+        String[] arrayGiven = new String[1];
86
+        arrayGiven[0]="";
87
+        for (int i=0; i < array.length; i++) {
88
+            arrayGiven[0] = arrayGiven[0].concat(array[i]);
89
+        }
90
+        arrayGiven = arrayGiven[0].toLowerCase().split("");
91
+        for (char alph = 'a'; alph <= 'z'; alph++) {
92
+            if (contains(arrayGiven, String.valueOf(alph))==false) {
93
+                outcome = false;
94
+            }
95
+        }
96
+        return outcome;
70 97
     }
71 98
 
72 99
     /**
@@ -75,7 +102,13 @@ public class StringArrayUtils {
75 102
      * @return number of occurrences the specified `value` has occurred
76 103
      */ // TODO
77 104
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
105
+        int counter = 0;
106
+        for (String element : array) {
107
+            if (element.equals(value)) {
108
+                counter++;
109
+            }
110
+        }
111
+        return counter;
79 112
     }
80 113
 
81 114
     /**
@@ -84,7 +117,16 @@ public class StringArrayUtils {
84 117
      * @return array with identical contents excluding values of `value`
85 118
      */ // TODO
86 119
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
120
+        int numberOfElementsToDelete = getNumberOfOccurrences(array, valueToRemove);
121
+        String[] arrayWithRemovedValue = new String[array.length-numberOfElementsToDelete];
122
+        int j=0; //index of arrayWithRemovedValue       
123
+        for (int i=0; i < array.length; i++) {
124
+            if (array[i].compareTo(valueToRemove) != 0) {
125
+                arrayWithRemovedValue[j] = array[i];
126
+                j++;
127
+            }
128
+        }
129
+        return arrayWithRemovedValue;
88 130
     }
89 131
 
90 132
     /**
@@ -92,7 +134,20 @@ public class StringArrayUtils {
92 134
      * @return array of Strings with consecutive duplicates removes
93 135
      */ // TODO
94 136
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
137
+        int numberOfElementsToDelete = 0;
138
+        int j=0; //index of arrayWithRemovedVConsecutiveDuplicates
139
+        String[] arrayWithRemovedConsecutiveDuplicates = new String[array.length];
140
+        for (int i=0; i < array.length-1; i++) {
141
+            if (array[i].compareTo(array[i+1]) != 0) {
142
+                arrayWithRemovedConsecutiveDuplicates[j] = array[i];
143
+                j++;
144
+            } else {
145
+                numberOfElementsToDelete++;
146
+            }
147
+        }
148
+        arrayWithRemovedConsecutiveDuplicates = Arrays.copyOf(arrayWithRemovedConsecutiveDuplicates, array.length-numberOfElementsToDelete);
149
+        arrayWithRemovedConsecutiveDuplicates[arrayWithRemovedConsecutiveDuplicates.length-1] = array[array.length-1];
150
+        return arrayWithRemovedConsecutiveDuplicates;
96 151
     }
97 152
 
98 153
     /**
@@ -100,8 +155,21 @@ public class StringArrayUtils {
100 155
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 156
      */ // TODO
102 157
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
158
+        String[] arrayWithPackedConsecutiveDuplicates = new String[array.length];
159
+        int j=0; //index of arrayWithPackedVConsecutiveDuplicates
160
+        int numberOfElementsToDelete = 0;
161
+        arrayWithPackedConsecutiveDuplicates[0] = array[0];
162
+        for (int i=0; i < array.length-1; i++) {
163
+            if (array[i].compareTo(array[i+1]) == 0) {
164
+                arrayWithPackedConsecutiveDuplicates[j] = arrayWithPackedConsecutiveDuplicates[j] + array[i+1];
165
+                numberOfElementsToDelete++;
166
+            } else {
167
+                arrayWithPackedConsecutiveDuplicates[j+1] = array[i+1];
168
+                j++;
169
+            }
170
+        }
171
+        arrayWithPackedConsecutiveDuplicates = Arrays.copyOf(arrayWithPackedConsecutiveDuplicates, array.length-numberOfElementsToDelete);
172
+        return arrayWithPackedConsecutiveDuplicates;
104 173
     }
105 174
 
106
-
107 175
 }

+ 15
- 2
StringArrayUtilsTest.java Vedi File

@@ -176,7 +176,14 @@ public class StringArrayUtilsTest {
176 176
         }
177 177
     }
178 178
 
179
-
179
+    @Test
180
+    public void testContains1() {
181
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
182
+       
183
+            boolean outcome = StringArrayUtils.contains(array, "fox");
184
+            Assert.assertTrue(outcome);
185
+        
186
+    }
180 187
 
181 188
 
182 189
 
@@ -396,7 +403,13 @@ public class StringArrayUtilsTest {
396 403
         Assert.assertEquals(expected, actual);
397 404
     }
398 405
 
399
-
406
+    @Test
407
+    public void testRemovePackDuplicates4() {
408
+        String[] array = {"m", "m", "o", "o", "n", "m", "a", "n"};
409
+        String[] expected = {"mm", "oo", "n", "m", "a", "n"};
410
+        String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
411
+        Assert.assertEquals(expected, actual);
412
+    }
400 413
 
401 414
 
402 415
 

+ 42
- 0
package.bluej Vedi File

@@ -0,0 +1,42 @@
1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=722
6
+editor.fx.0.width=829
7
+editor.fx.0.x=0
8
+editor.fx.0.y=74
9
+objectbench.height=101
10
+objectbench.width=461
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.8007380073800738
13
+package.editor.height=427
14
+package.editor.width=674
15
+package.editor.x=402
16
+package.editor.y=106
17
+package.frame.height=600
18
+package.frame.width=800
19
+package.numDependencies=1
20
+package.numTargets=2
21
+package.showExtends=true
22
+package.showUses=true
23
+project.charset=UTF-8
24
+readme.height=58
25
+readme.name=@README
26
+readme.width=47
27
+readme.x=10
28
+readme.y=10
29
+target1.height=50
30
+target1.name=StringArrayUtilsTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=150
34
+target1.x=130
35
+target1.y=10
36
+target2.height=50
37
+target2.name=StringArrayUtils
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=120
41
+target2.x=70
42
+target2.y=70