Просмотр исходного кода

completed StringArrayUtilities

De'Jon Johnson 8 лет назад
Родитель
Сommit
be59365855

+ 105
- 13
src/main/java/com/zipcodewilmington/StringArrayUtils.java Просмотреть файл

@@ -1,5 +1,9 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+
4
+import java.util.*;
5
+
6
+
3 7
 /**
4 8
  * Created by leon on 1/29/18.
5 9
  */
@@ -9,7 +13,7 @@ public class StringArrayUtils {
9 13
      * @return first element of specified array
10 14
      */ // TODO
11 15
     public static String getFirstElement(String[] array) {
12
-        return null;
16
+        return array[0];
13 17
     }
14 18
 
15 19
     /**
@@ -17,7 +21,8 @@ public class StringArrayUtils {
17 21
      * @return second element in specified array
18 22
      */
19 23
     public static String getSecondElement(String[] array) {
20
-        return null;
24
+
25
+        return array[1];
21 26
     }
22 27
 
23 28
     /**
@@ -25,7 +30,8 @@ public class StringArrayUtils {
25 30
      * @return last element in specified array
26 31
      */ // TODO
27 32
     public static String getLastElement(String[] array) {
28
-        return null;
33
+
34
+        return array[array.length - 1];
29 35
     }
30 36
 
31 37
     /**
@@ -33,7 +39,8 @@ public class StringArrayUtils {
33 39
      * @return second to last element in specified array
34 40
      */ // TODO
35 41
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
42
+
43
+        return array[array.length - 2];
37 44
     }
38 45
 
39 46
     /**
@@ -42,7 +49,13 @@ public class StringArrayUtils {
42 49
      * @return true if the array contains the specified `value`
43 50
      */ // TODO
44 51
     public static boolean contains(String[] array, String value) {
45
-        return false;
52
+
53
+
54
+        if (Arrays.asList(array).contains(value)) {
55
+            return true;
56
+        } else {
57
+            return false;
58
+        }
46 59
     }
47 60
 
48 61
     /**
@@ -50,7 +63,16 @@ public class StringArrayUtils {
50 63
      * @return an array with identical contents in reverse order
51 64
      */ // TODO
52 65
     public static String[] reverse(String[] array) {
53
-        return null;
66
+
67
+        String[] newArr = new String[array.length];
68
+        int counter = 0;
69
+        for (int i = array.length - 1; i >= 0; i--) {
70
+            newArr[counter] = array[i];
71
+
72
+            counter++;
73
+            //counter = counter +1;
74
+        }
75
+        return newArr;
54 76
     }
55 77
 
56 78
     /**
@@ -58,7 +80,17 @@ public class StringArrayUtils {
58 80
      * @return true if the order of the array is the same backwards and forwards
59 81
      */ // TODO
60 82
     public static boolean isPalindromic(String[] array) {
61
-        return false;
83
+
84
+        boolean isPal = false;
85
+        for (int i = 0; i < array.length; i++) {
86
+            if (reverse(array)[0].equals(array[i])) {
87
+                isPal = true;
88
+
89
+            } else {
90
+
91
+            }
92
+        }
93
+        return isPal;
62 94
     }
63 95
 
64 96
     /**
@@ -66,7 +98,23 @@ public class StringArrayUtils {
66 98
      * @return true if each letter in the alphabet has been used in the array
67 99
      */ // TODO
68 100
     public static boolean isPangramic(String[] array) {
69
-        return false;
101
+        String character = "abcdefghijklmnopqrstuvwxyz";
102
+        String stringArray = "";
103
+
104
+        for (int i = 0; i < array.length; i++) {
105
+            stringArray = stringArray + array[i];
106
+        }
107
+        stringArray = stringArray.toLowerCase();
108
+
109
+        for (int i = 0; i < stringArray.length(); i++) {
110
+            for (int j = 0; j < character.length(); j++) {
111
+                if (stringArray.indexOf(character.charAt(j)) == -1) {
112
+                    return false;
113
+
114
+                }
115
+            }
116
+        }
117
+        return true;
70 118
     }
71 119
 
72 120
     /**
@@ -75,7 +123,13 @@ public class StringArrayUtils {
75 123
      * @return number of occurrences the specified `value` has occurred
76 124
      */ // TODO
77 125
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
126
+
127
+        int counter = 0;
128
+        for (int i = 0; i < array.length; i++) {
129
+            if (value.equals(array[i]))
130
+                counter++;
131
+        }
132
+        return counter;
79 133
     }
80 134
 
81 135
     /**
@@ -84,7 +138,15 @@ public class StringArrayUtils {
84 138
      * @return array with identical contents excluding values of `value`
85 139
      */ // TODO
86 140
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
141
+
142
+        StringBuilder newString = new StringBuilder();
143
+        for (int i = 0; i < array.length; i++) {
144
+            if (!array[i].equals(valueToRemove)) {
145
+                newString.append(array[i] + " ");
146
+            }
147
+
148
+        }
149
+        return newString.toString().split(" ");
88 150
     }
89 151
 
90 152
     /**
@@ -92,16 +154,46 @@ public class StringArrayUtils {
92 154
      * @return array of Strings with consecutive duplicates removes
93 155
      */ // TODO
94 156
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
157
+
158
+      String[] differentArray = new String[array.length];
159
+      int counter = 0;
160
+      for (int i = 1; i < array.length; i++){
161
+          String newWord = array[i-1];
162
+          if(!newWord.equals(array[i])){
163
+              differentArray[counter] = array[i-1];
164
+              counter++;
165
+          }
166
+      }
167
+      differentArray[counter] = array[array.length -1];
168
+      return Arrays.copyOf(differentArray, counter+1);
96 169
     }
97 170
 
171
+
172
+
98 173
     /**
99 174
      * @param array array of chars
100 175
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 176
      */ // TODO
102 177
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105 178
 
179
+        String [] newArray = new String[ array.length];
180
+        int newIndex = 0;
181
+        String last = array[0];
182
+        String combiner = array[0];
106 183
 
184
+        for (int i = 1; i < array.length; i++){
185
+            if (!last.equals(array[i])){
186
+                last = array[i];
187
+                newArray[newIndex] = combiner;
188
+                combiner = array[i];
189
+                newIndex++;
190
+
191
+            } else{
192
+                combiner+= array[i];
193
+            }
194
+        }
195
+        newArray[newIndex] = combiner;
196
+        return Arrays.copyOf(newArray, newIndex +1);
197
+    }
107 198
 }
199
+

+ 1
- 133
src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java Просмотреть файл

@@ -34,18 +34,6 @@ public class StringArrayUtilsTest {
34 34
     }
35 35
 
36 36
 
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49 37
     @Test
50 38
     public void testGetSecondElement1() {
51 39
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -72,15 +60,6 @@ public class StringArrayUtilsTest {
72 60
     }
73 61
 
74 62
 
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84 63
     @Test
85 64
     public void testGetLastElement1() {
86 65
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -107,25 +86,6 @@ public class StringArrayUtilsTest {
107 86
     }
108 87
 
109 88
 
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129 89
     @Test
130 90
     public void testGetSecondToLastElement1() {
131 91
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -152,21 +112,6 @@ public class StringArrayUtilsTest {
152 112
     }
153 113
 
154 114
 
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170 115
     @Test
171 116
     public void testContains() {
172 117
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -177,17 +122,6 @@ public class StringArrayUtilsTest {
177 122
     }
178 123
 
179 124
 
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191 125
     @Test
192 126
     public void testReverse1() {
193 127
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -199,7 +133,7 @@ public class StringArrayUtilsTest {
199 133
 
200 134
     @Test
201 135
     public void testReverse2() {
202
-        String[] array  = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
136
+        String[] array = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
203 137
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
204 138
         String[] actual = StringArrayUtils.reverse(array);
205 139
         Assert.assertEquals(expected, actual);
@@ -214,14 +148,6 @@ public class StringArrayUtilsTest {
214 148
     }
215 149
 
216 150
 
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225 151
     @Test
226 152
     public void testIsPalindromic1() {
227 153
         String[] array = {"a", "b", "c", "b", "a"};
@@ -230,7 +156,6 @@ public class StringArrayUtilsTest {
230 156
     }
231 157
 
232 158
 
233
-
234 159
     @Test
235 160
     public void testIsPalindromic2() {
236 161
         String[] array = {"Is this a palindrome?", "This is a palindrome", "Is this a palindrome?"};
@@ -247,13 +172,6 @@ public class StringArrayUtilsTest {
247 172
     }
248 173
 
249 174
 
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257 175
     @Test
258 176
     public void testIsPangramic1() {
259 177
         String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
@@ -284,17 +202,6 @@ public class StringArrayUtilsTest {
284 202
     }
285 203
 
286 204
 
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298 205
     @Test
299 206
     public void testGetNumberOfOccurrences1() {
300 207
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -320,18 +227,6 @@ public class StringArrayUtilsTest {
320 227
     }
321 228
 
322 229
 
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335 230
     @Test
336 231
     public void testRemoveConsecutiveDuplicates1() {
337 232
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -341,7 +236,6 @@ public class StringArrayUtilsTest {
341 236
     }
342 237
 
343 238
 
344
-
345 239
     @Test
346 240
     public void testRemoveConsecutiveDuplicates2() {
347 241
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
@@ -360,15 +254,6 @@ public class StringArrayUtilsTest {
360 254
     }
361 255
 
362 256
 
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372 257
     @Test
373 258
     public void testRemovePackDuplicates1() {
374 259
         String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
@@ -387,7 +272,6 @@ public class StringArrayUtilsTest {
387 272
     }
388 273
 
389 274
 
390
-
391 275
     @Test
392 276
     public void testRemovePackDuplicates3() {
393 277
         String[] array = {"m", "o", "o", "n", "m", "a", "n"};
@@ -397,12 +281,6 @@ public class StringArrayUtilsTest {
397 281
     }
398 282
 
399 283
 
400
-
401
-
402
-
403
-
404
-
405
-
406 284
     @Test
407 285
     public void testRemoveValue() {
408 286
         String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -428,14 +306,4 @@ public class StringArrayUtilsTest {
428 306
     }
429 307
 
430 308
 
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441 309
 }