Zavon Malone пре 7 година
родитељ
комит
07d4b18202

+ 12
- 0
pom.xml Прегледај датотеку

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington.labs</groupId>
8 8
     <artifactId>arrayutils</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>6</source>
17
+                    <target>6</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 68
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java Прегледај датотеку

@@ -1,5 +1,7 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import java.util.*;
4
+
3 5
 /**
4 6
  * Created by leon on 1/29/18.
5 7
  */
@@ -9,7 +11,9 @@ public class StringArrayUtils {
9 11
      * @return first element of specified array
10 12
      */ // TODO
11 13
     public static String getFirstElement(String[] array) {
12
-        return null;
14
+
15
+
16
+        return array[0];
13 17
     }
14 18
 
15 19
     /**
@@ -17,7 +21,7 @@ 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
+        return array[1];
21 25
     }
22 26
 
23 27
     /**
@@ -25,7 +29,9 @@ public class StringArrayUtils {
25 29
      * @return last element in specified array
26 30
      */ // TODO
27 31
     public static String getLastElement(String[] array) {
28
-        return null;
32
+        int lastIndex= array.length-1;
33
+
34
+        return array[lastIndex];
29 35
     }
30 36
 
31 37
     /**
@@ -33,7 +39,9 @@ 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
+        int secondtoLast = array.length-2;
43
+
44
+        return array[secondtoLast];
37 45
     }
38 46
 
39 47
     /**
@@ -42,7 +50,12 @@ public class StringArrayUtils {
42 50
      * @return true if the array contains the specified `value`
43 51
      */ // TODO
44 52
     public static boolean contains(String[] array, String value) {
45
-        return false;
53
+        for(String s : array ){
54
+            if(s.equals(value)){
55
+                return true;
56
+            }
57
+        }
58
+            return false;
46 59
     }
47 60
 
48 61
     /**
@@ -50,7 +63,9 @@ 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
+        List<String> list = Arrays.asList(Arrays.copyOf(array, array.length));
67
+        Collections.reverse(list);
68
+        return list.toArray(new String[array.length]);
54 69
     }
55 70
 
56 71
     /**
@@ -58,7 +73,8 @@ public class StringArrayUtils {
58 73
      * @return true if the order of the array is the same backwards and forwards
59 74
      */ // TODO
60 75
     public static boolean isPalindromic(String[] array) {
61
-        return false;
76
+       return Arrays.equals(array, reverse(array));
77
+
62 78
     }
63 79
 
64 80
     /**
@@ -66,7 +82,20 @@ public class StringArrayUtils {
66 82
      * @return true if each letter in the alphabet has been used in the array
67 83
      */ // TODO
68 84
     public static boolean isPangramic(String[] array) {
69
-        return false;
85
+        String arrayString ="";
86
+        for(String s : array){
87
+            arrayString+= s;
88
+        }
89
+
90
+        String lower = arrayString.toLowerCase();
91
+
92
+
93
+        for(char i = 'a'; i<'z'; i++){
94
+            if(!lower.contains(String.valueOf(i))){
95
+                return false;
96
+            }
97
+        }
98
+        return true;
70 99
     }
71 100
 
72 101
     /**
@@ -75,7 +104,12 @@ public class StringArrayUtils {
75 104
      * @return number of occurrences the specified `value` has occurred
76 105
      */ // TODO
77 106
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
107
+        int count = 0;
108
+        for (int i=0; i<array.length; i++)
109
+            if (value == array[i])
110
+                count++;
111
+        return count;
112
+
79 113
     }
80 114
 
81 115
     /**
@@ -84,7 +118,10 @@ public class StringArrayUtils {
84 118
      * @return array with identical contents excluding values of `value`
85 119
      */ // TODO
86 120
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
121
+        List<String> list = new ArrayList<String>(Arrays.asList(array));
122
+        list.remove(valueToRemove);
123
+        array = list.toArray(new String[0]);
124
+        return array;
88 125
     }
89 126
 
90 127
     /**
@@ -92,7 +129,18 @@ public class StringArrayUtils {
92 129
      * @return array of Strings with consecutive duplicates removes
93 130
      */ // TODO
94 131
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
132
+        int count = 0;
133
+        List<String> list = new ArrayList<String>();
134
+        list.add(array[0]);
135
+        for(int i = 1; i<array.length; i++){
136
+            if(array[i] != array[i-1]){
137
+                list.add(array[i]);
138
+            }
139
+        }
140
+
141
+        array= list.toArray(new String[0]);
142
+        return array;
143
+
96 144
     }
97 145
 
98 146
     /**
@@ -100,7 +148,15 @@ public class StringArrayUtils {
100 148
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 149
      */ // TODO
102 150
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
151
+        StringBuilder sb = new StringBuilder(array[0]);
152
+        for(int i = 1; i<array.length; i++){
153
+            if(array[i]!= array[i-1]){
154
+                sb.append("-");
155
+            }
156
+            sb.append(array[i]);
157
+        }
158
+           String[] s = sb.toString().split("-");
159
+            return s;
104 160
     }
105 161
 
106 162
 

+ 12
- 12
src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java Прегледај датотеку

@@ -193,7 +193,7 @@ public class StringArrayUtilsTest {
193 193
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
194 194
         String[] expected = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
195 195
         String[] actual = StringArrayUtils.reverse(array);
196
-        Assert.assertEquals(expected, actual);
196
+        Assert.assertArrayEquals(expected, actual);
197 197
     }
198 198
 
199 199
 
@@ -202,7 +202,7 @@ public class StringArrayUtilsTest {
202 202
         String[] array  = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
203 203
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
204 204
         String[] actual = StringArrayUtils.reverse(array);
205
-        Assert.assertEquals(expected, actual);
205
+        Assert.assertArrayEquals(expected, actual);
206 206
     }
207 207
 
208 208
 
@@ -210,7 +210,7 @@ public class StringArrayUtilsTest {
210 210
     public void testReverse3() {
211 211
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
212 212
         String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected));
213
-        Assert.assertEquals(expected, actual);
213
+        Assert.assertArrayEquals(expected, actual);
214 214
     }
215 215
 
216 216
 
@@ -337,7 +337,7 @@ public class StringArrayUtilsTest {
337 337
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
338 338
         String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
339 339
         String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
340
-        Assert.assertEquals(actual, expected);
340
+        Assert.assertArrayEquals(expected, actual);
341 341
     }
342 342
 
343 343
 
@@ -347,7 +347,7 @@ public class StringArrayUtilsTest {
347 347
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
348 348
         String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
349 349
         String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "bbb"};
350
-        Assert.assertEquals(actual, expected);
350
+        Assert.assertArrayEquals(expected, actual);
351 351
     }
352 352
 
353 353
 
@@ -356,7 +356,7 @@ public class StringArrayUtilsTest {
356 356
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "aba", "bbb"};
357 357
         String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
358 358
         String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
359
-        Assert.assertEquals(actual, expected);
359
+        Assert.assertArrayEquals(actual, expected);
360 360
     }
361 361
 
362 362
 
@@ -374,7 +374,7 @@ public class StringArrayUtilsTest {
374 374
         String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
375 375
         String[] expected = {"aaa", "b", "cc", "aa", "d"};
376 376
         String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
377
-        Assert.assertEquals(expected, actual);
377
+        Assert.assertArrayEquals(expected, actual);
378 378
     }
379 379
 
380 380
 
@@ -383,7 +383,7 @@ public class StringArrayUtilsTest {
383 383
         String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
384 384
         String[] expected = {"tt", "q", "aaa", "b", "cc", "aa", "d", "eee"};
385 385
         String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
386
-        Assert.assertEquals(expected, actual);
386
+        Assert.assertArrayEquals(expected, actual);
387 387
     }
388 388
 
389 389
 
@@ -393,7 +393,7 @@ public class StringArrayUtilsTest {
393 393
         String[] array = {"m", "o", "o", "n", "m", "a", "n"};
394 394
         String[] expected = {"m", "oo", "n", "m", "a", "n"};
395 395
         String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
396
-        Assert.assertEquals(expected, actual);
396
+        Assert.assertArrayEquals(expected, actual);
397 397
     }
398 398
 
399 399
 
@@ -408,7 +408,7 @@ public class StringArrayUtilsTest {
408 408
         String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
409 409
         String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
410 410
         String[] actual = StringArrayUtils.removeValue(array, "The");
411
-        Assert.assertEquals(expected, actual);
411
+        Assert.assertArrayEquals(expected, actual);
412 412
     }
413 413
 
414 414
     @Test
@@ -416,7 +416,7 @@ public class StringArrayUtilsTest {
416 416
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
417 417
         String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
418 418
         String[] actual = StringArrayUtils.removeValue(array, "quick");
419
-        Assert.assertEquals(expected, actual);
419
+        Assert.assertArrayEquals(expected, actual);
420 420
     }
421 421
 
422 422
     @Test
@@ -424,7 +424,7 @@ public class StringArrayUtilsTest {
424 424
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
425 425
         String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
426 426
         String[] actual = StringArrayUtils.removeValue(array, "brown");
427
-        Assert.assertEquals(expected, actual);
427
+        Assert.assertArrayEquals(expected, actual);
428 428
     }
429 429
 
430 430