Ver código fonte

updated test cases and readme

Leon 6 anos atrás
pai
commit
28c0c309bb

+ 10
- 10
README.md Ver arquivo

287
 
287
 
288
 
288
 
289
 <br><br><br><br>
289
 <br><br><br><br>
290
-## `getNextToLastElement(array)`
290
+## `getSecondToLastElement(array)`
291
 * **Description**
291
 * **Description**
292
    * Given an array of `String` objects, return the next-to-last element of the array.
292
    * Given an array of `String` objects, return the next-to-last element of the array.
293
 
293
 
299
     String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
299
     String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
300
     
300
     
301
     // : When
301
     // : When
302
-    boolean outcome = StringArrayUtils.getNextToLastElement(array);
302
+    boolean outcome = StringArrayUtils.getSecondToLastElement(array);
303
     
303
     
304
     // : Then
304
     // : Then
305
     System.out.println(outcome);
305
     System.out.println(outcome);
912
 
912
 
913
 
913
 
914
 <br><br><br><br>
914
 <br><br><br><br>
915
-## `packDuplicates(array)`
915
+## `packConsecutiveDuplicates(array)`
916
 * **Description**
916
 * **Description**
917
-    * Given an array of `char` objects, return an array of Strings with conseuctive duplicates placed in an array.
917
+    * Given an array of `char` objects, return an array of Strings with consecutive duplicates placed in an array.
918
         
918
         
919
 
919
 
920
 ### Example 1
920
 ### Example 1
922
 
922
 
923
     ```
923
     ```
924
     // : Given
924
     // : Given
925
-	char[] array = {'a' 'a' 'a' 'a' 'b' 'c' 'c' 'a' 'a' 'd'}
925
+	String[] array = {"a", "a", "a", "a", "b", "c", "c", "a", "a", "d"};
926
 	
926
 	
927
     // : When
927
     // : When
928
-    String[] actual = StringArrayUtils.packDuplicates(array);
928
+    String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
929
     
929
     
930
     // : Then
930
     // : Then
931
     System.out.println(Arrays.toString(actual));
931
     System.out.println(Arrays.toString(actual));
950
 
950
 
951
     ```
951
     ```
952
     // : Given
952
     // : Given
953
-	char[] array = {'t', 't', 'q', 'a' 'a' 'a' 'a' 'b'}
953
+	String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"}; 
954
 	
954
 	
955
     // : When
955
     // : When
956
-    String[] actual = StringArrayUtils.packDuplicates(array);
956
+    String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
957
     
957
     
958
     // : Then
958
     // : Then
959
     System.out.println(Arrays.toString(actual));
959
     System.out.println(Arrays.toString(actual));
976
 
976
 
977
     ```
977
     ```
978
     // : Given
978
     // : Given
979
-	char[] array = {'m', 'o', 'o', 'n' 'm' 'a' 'n'}
979
+	String[] array = {"m", "o", "o", "n", "m", "a", "n"}
980
 	
980
 	
981
     // : When
981
     // : When
982
-    String[] actual = StringArrayUtils.packDuplicates(array);
982
+    String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
983
     
983
     
984
     // : Then
984
     // : Then
985
     System.out.println(Arrays.toString(actual));
985
     System.out.println(Arrays.toString(actual));

+ 84
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java Ver arquivo

5
  */
5
  */
6
 public class StringArrayUtils {
6
 public class StringArrayUtils {
7
     /**
7
     /**
8
-     * @param array - array of String objects
9
-     * @param value - value to check array for
10
-     * @return - number of occurrences the specified `value` has occurred
8
+     * @param array array of String objects
9
+     * @return first element of specified array
11
      */ // TODO
10
      */ // TODO
12
-    public static int getNumberOfOccurrences(String[] array, String value) {
13
-        return 0;
11
+    public static String getFirstElement(String[] array) {
12
+        return null;
14
     }
13
     }
15
 
14
 
16
     /**
15
     /**
17
-     * @param array - array of String objects
18
-     * @param value - value to check array for
19
-     * @return - true if the array contains the specified `value`
16
+     * @param array array of String objects
17
+     * @return second element in specified array
20
      */
18
      */
19
+    public static String getSecondElement(String[] array) {
20
+        return null;
21
+    }
22
+
23
+    /**
24
+     * @param array array of String objects
25
+     * @return last element in specified array
26
+     */ // TODO
27
+    public static String getLastElement(String[] array) {
28
+        return null;
29
+    }
30
+
31
+    /**
32
+     * @param array array of String objects
33
+     * @return second to last element in specified array
34
+     */ // TODO
35
+    public static String getSecondToLastElement(String[] array) {
36
+        return null;
37
+    }
38
+
39
+    /**
40
+     * @param array array of String objects
41
+     * @param value value to check array for
42
+     * @return true if the array contains the specified `value`
43
+     */ // TODO
21
     public static boolean contains(String[] array, String value) {
44
     public static boolean contains(String[] array, String value) {
22
         return false;
45
         return false;
23
     }
46
     }
24
 
47
 
48
+    /**
49
+     * @param array of String objects
50
+     * @return an array with identical contents in reverse order
51
+     */ // TODO
52
+    public static String[] reverse(String[] array) {
53
+        return null;
54
+    }
25
 
55
 
26
     /**
56
     /**
27
-     * @param array - array of String objects
28
-     * @param valueToRemove - value to remove from array
29
-     * @return - array with identical contents excluding values of `value`
30
-     */
57
+     * @param array array of String objects
58
+     * @return true if the order of the array is the same backwards and forwards
59
+     */ // TODO
60
+    public static boolean isPalindromic(String[] array) {
61
+        return false;
62
+    }
31
 
63
 
64
+    /**
65
+     * @param array array of String objects
66
+     * @return true if each letter in the alphabet has been used in the array
67
+     */ // TODO
68
+    public static boolean isPangramic(String[] array) {
69
+        return false;
70
+    }
71
+
72
+    /**
73
+     * @param array array of String objects
74
+     * @param value value to check array for
75
+     * @return number of occurrences the specified `value` has occurred
76
+     */ // TODO
77
+    public static int getNumberOfOccurrences(String[] array, String value) {
78
+        return 0;
79
+    }
80
+
81
+    /**
82
+     * @param array         array of String objects
83
+     * @param valueToRemove value to remove from array
84
+     * @return array with identical contents excluding values of `value`
85
+     */ // TODO
32
     public static String[] removeValue(String[] array, String valueToRemove) {
86
     public static String[] removeValue(String[] array, String valueToRemove) {
33
         return null;
87
         return null;
34
     }
88
     }
89
+
90
+    /**
91
+     * @param array array of chars
92
+     * @return array of Strings with consecutive duplicates removes
93
+     */ // TODO
94
+    public static String[] removeConsecutiveDuplicates(String[] array) {
95
+        return null;
96
+    }
97
+
98
+    /**
99
+     * @param array array of chars
100
+     * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
+     */ // TODO
102
+    public static String[] packConsecutiveDuplicates(String[] array) {
103
+        return null;
104
+    }
105
+
106
+
35
 }
107
 }

+ 397
- 16
src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java Ver arquivo

7
  * Created by leon on 1/29/18.
7
  * Created by leon on 1/29/18.
8
  */
8
  */
9
 public class StringArrayUtilsTest {
9
 public class StringArrayUtilsTest {
10
-    private static final String[] testArray = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
10
+
11
+    @Test
12
+    public void testGetFirstElement1() {
13
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
14
+        String expected = "the";
15
+        String actual = StringArrayUtils.getFirstElement(array);
16
+        Assert.assertEquals(expected, actual);
17
+    }
18
+
19
+    @Test
20
+    public void testGetFirstElement2() {
21
+        String[] array = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
22
+        String expected = "quick";
23
+        String actual = StringArrayUtils.getFirstElement(array);
24
+        Assert.assertEquals(expected, actual);
25
+    }
26
+
27
+
28
+    @Test
29
+    public void testGetFirstElement3() {
30
+        String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
31
+        String expected = "brown";
32
+        String actual = StringArrayUtils.getFirstElement(array);
33
+        Assert.assertEquals(expected, actual);
34
+    }
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+    @Test
50
+    public void testGetSecondElement1() {
51
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
52
+        String expected = "quick";
53
+        String actual = StringArrayUtils.getSecondElement(array);
54
+        Assert.assertEquals(expected, actual);
55
+    }
56
+
57
+    @Test
58
+    public void testGetSecondElement2() {
59
+        String[] array = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
60
+        String expected = "brown";
61
+        String actual = StringArrayUtils.getSecondElement(array);
62
+        Assert.assertEquals(expected, actual);
63
+    }
64
+
65
+
66
+    @Test
67
+    public void testGetSecondElement3() {
68
+        String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
69
+        String expected = "fox";
70
+        String actual = StringArrayUtils.getSecondElement(array);
71
+        Assert.assertEquals(expected, actual);
72
+    }
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+    @Test
85
+    public void testGetLastElement1() {
86
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
87
+        String expected = "dog";
88
+        String actual = StringArrayUtils.getLastElement(array);
89
+        Assert.assertEquals(expected, actual);
90
+    }
91
+
92
+    @Test
93
+    public void testGetLastElement2() {
94
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy"};
95
+        String expected = "lazy";
96
+        String actual = StringArrayUtils.getLastElement(array);
97
+        Assert.assertEquals(expected, actual);
98
+    }
99
+
100
+
101
+    @Test
102
+    public void testGetLastElement3() {
103
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
104
+        String expected = "over";
105
+        String actual = StringArrayUtils.getLastElement(array);
106
+        Assert.assertEquals(expected, actual);
107
+    }
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+    @Test
130
+    public void testGetSecondToLastElement1() {
131
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
132
+        String expected = "lazy";
133
+        String actual = StringArrayUtils.getSecondToLastElement(array);
134
+        Assert.assertEquals(expected, actual);
135
+    }
136
+
137
+    @Test
138
+    public void testGetSecondToLastElement2() {
139
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "lazy"};
140
+        String expected = "over";
141
+        String actual = StringArrayUtils.getSecondToLastElement(array);
142
+        Assert.assertEquals(expected, actual);
143
+    }
144
+
145
+
146
+    @Test
147
+    public void testGetSecondToLastElement3() {
148
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
149
+        String expected = "jumps";
150
+        String actual = StringArrayUtils.getSecondToLastElement(array);
151
+        Assert.assertEquals(expected, actual);
152
+    }
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
11
 
169
 
12
     @Test
170
     @Test
13
     public void testContains() {
171
     public void testContains() {
14
-        for (String s : testArray) {
15
-            boolean outcome = StringArrayUtils.contains(testArray, s);
172
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
173
+        for (String s : array) {
174
+            boolean outcome = StringArrayUtils.contains(array, s);
16
             Assert.assertTrue(outcome);
175
             Assert.assertTrue(outcome);
17
         }
176
         }
18
     }
177
     }
19
 
178
 
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
20
     @Test
191
     @Test
21
-    public void testRemoveValue() {
22
-        String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
23
-        String[] actual = StringArrayUtils.removeValue(expected, "The");
192
+    public void testReverse1() {
193
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
194
+        String[] expected = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
195
+        String[] actual = StringArrayUtils.reverse(array);
24
         Assert.assertEquals(expected, actual);
196
         Assert.assertEquals(expected, actual);
25
     }
197
     }
26
 
198
 
199
+
27
     @Test
200
     @Test
28
-    public void testRemoveValue1() {
29
-        String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
30
-        String[] actual = StringArrayUtils.removeValue(expected, "quick");
201
+    public void testReverse2() {
202
+        String[] array  = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
203
+        String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
204
+        String[] actual = StringArrayUtils.reverse(array);
31
         Assert.assertEquals(expected, actual);
205
         Assert.assertEquals(expected, actual);
32
     }
206
     }
33
 
207
 
208
+
34
     @Test
209
     @Test
35
-    public void testRemoveValue2() {
36
-        String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
37
-        String[] actual = StringArrayUtils.removeValue(expected, "brown");
210
+    public void testReverse3() {
211
+        String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
212
+        String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected));
38
         Assert.assertEquals(expected, actual);
213
         Assert.assertEquals(expected, actual);
39
     }
214
     }
40
 
215
 
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+    @Test
226
+    public void testIsPalindromic1() {
227
+        String[] array = {"a", "b", "c", "b", "a"};
228
+        boolean outcome = StringArrayUtils.isPalindromic(array);
229
+        Assert.assertTrue(outcome);
230
+    }
231
+
232
+
233
+
234
+    @Test
235
+    public void testIsPalindromic2() {
236
+        String[] array = {"Is this a plaindrome?", "This is a plaindrome", "Is this a palindrome?"};
237
+        boolean outcome = StringArrayUtils.isPalindromic(array);
238
+        Assert.assertTrue(outcome);
239
+    }
240
+
241
+
242
+    @Test
243
+    public void testIsPalindromic3() {
244
+        String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
245
+        boolean outcome = StringArrayUtils.isPalindromic(array);
246
+        Assert.assertTrue(outcome);
247
+    }
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+    @Test
258
+    public void testIsPangramic1() {
259
+        String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
260
+        boolean outcome = StringArrayUtils.isPangramic(array);
261
+        Assert.assertTrue(outcome);
262
+    }
263
+
264
+    @Test
265
+    public void testIsPangramic2() {
266
+        String[] array = {"The", "quick", "onyx", "goblin", "jumps", "over", "the", "lazy", "dwarf"};
267
+        boolean outcome = StringArrayUtils.isPangramic(array);
268
+        Assert.assertTrue(outcome);
269
+    }
270
+
271
+    @Test
272
+    public void testIsPangramic3() {
273
+        String[] array = {"Five quacking", "zephyrs", "jolt my", "wax bed"};
274
+        boolean outcome = StringArrayUtils.isPangramic(array);
275
+        Assert.assertTrue(outcome);
276
+    }
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+
41
     @Test
290
     @Test
42
     public void testGetNumberOfOccurrences1() {
291
     public void testGetNumberOfOccurrences1() {
292
+        String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
293
+        int expected = 4;
294
+        int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
295
+        Assert.assertEquals(actual, expected);
296
+    }
297
+
298
+    @Test
299
+    public void testGetNumberOfOccurrences2() {
300
+        String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
43
         int expected = 2;
301
         int expected = 2;
44
-        int actual = StringArrayUtils.getNumberOfOccurrences(testArray, "the");
302
+        int actual = StringArrayUtils.getNumberOfOccurrences(array, "bbb");
303
+        Assert.assertEquals(actual, expected);
304
+    }
305
+
306
+    @Test
307
+    public void testGetNumberOfOccurrences3() {
308
+        String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
309
+        int expected = 4;
310
+        int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
311
+        Assert.assertEquals(actual, expected);
312
+    }
313
+
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+    @Test
328
+    public void testRemoveConsecutiveDuplicates1() {
329
+        String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
330
+        String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
331
+        String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
332
+        Assert.assertEquals(actual, expected);
333
+    }
334
+
335
+
336
+
337
+    @Test
338
+    public void testRemoveConsecutiveDuplicates2() {
339
+        String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
340
+        String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
341
+        String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "bbb"};
342
+        Assert.assertEquals(actual, expected);
343
+    }
344
+
345
+
346
+
347
+    @Test
348
+    public void testRemoveConsecutiveDuplicates3() {
349
+        String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb", "aba", "bbb"};
350
+        String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
351
+        String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
352
+        Assert.assertEquals(actual, expected);
353
+    }
354
+
355
+
356
+
357
+
358
+
359
+
360
+
361
+
362
+
363
+
364
+
365
+    @Test
366
+    public void testRemovePackDuplicates1() {
367
+        String[] array = {"a", "a", "a", "a", "b", "c", "c", "a", "a", "d"};
368
+        String[] expected = {"aaa", "b", "cc", "aa", "d", "eee"};
369
+        String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
45
         Assert.assertEquals(expected, actual);
370
         Assert.assertEquals(expected, actual);
46
     }
371
     }
47
 
372
 
373
+
374
+
48
     @Test
375
     @Test
49
-    public void testGetNumberOfOccurrences2() {
50
-        int expected = 1;
51
-        int actual = StringArrayUtils.getNumberOfOccurrences(testArray, "quick");
376
+    public void testRemovePackDuplicates2() {
377
+        String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
378
+        String[] expected = {"tt", "q", "aaa", "cc", "aa", "d", "eee"};
379
+        String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
380
+        Assert.assertEquals(expected, actual);
381
+    }
382
+
383
+
384
+
385
+    @Test
386
+    public void testRemovePackDuplicates3() {
387
+        String[] array = {"m", "o", "o", "n", "m", "a", "n"};
388
+        String[] expected = {"m", "oo", "n", "m", "a", "n"};
389
+        String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
390
+        Assert.assertEquals(expected, actual);
391
+    }
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+    @Test
401
+    public void testRemoveValue() {
402
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
403
+        String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
404
+        String[] actual = StringArrayUtils.removeValue(array, "The");
405
+        Assert.assertEquals(expected, actual);
406
+    }
407
+
408
+    @Test
409
+    public void testRemoveValue1() {
410
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
411
+        String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
412
+        String[] actual = StringArrayUtils.removeValue(array, "quick");
413
+        Assert.assertEquals(expected, actual);
414
+    }
415
+
416
+    @Test
417
+    public void testRemoveValue2() {
418
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
419
+        String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
420
+        String[] actual = StringArrayUtils.removeValue(array, "brown");
52
         Assert.assertEquals(expected, actual);
421
         Assert.assertEquals(expected, actual);
53
     }
422
     }
423
+
424
+
425
+
426
+
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
54
 }
435
 }