thulasi před 6 roky
rodič
revize
d216c2bbf7

+ 107
- 13
src/main/java/com/zipcodewilmington/StringArrayUtils.java Zobrazit soubor

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

+ 1
- 1
src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java Zobrazit soubor

@@ -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