NiraParikh 6 年之前
父節點
當前提交
e30013af8f

+ 122
- 24
src/main/java/com/zipcodewilmington/StringArrayUtils.java 查看文件

@@ -1,15 +1,23 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import javax.print.DocFlavor;
4
+import java.lang.reflect.Array;
5
+import java.util.ArrayList;
6
+import java.util.Arrays;
7
+import java.util.Collections;
8
+import java.util.List;
9
+
3 10
 /**
4 11
  * Created by leon on 1/29/18.
5 12
  */
6
-public class StringArrayUtils {
13
+public class StringArrayUtils<start, end> {
7 14
     /**
8 15
      * @param array array of String objects
9 16
      * @return first element of specified array
10 17
      */ // TODO
11 18
     public static String getFirstElement(String[] array) {
12
-        return null;
19
+
20
+        return array[0];
13 21
     }
14 22
 
15 23
     /**
@@ -17,7 +25,8 @@ public class StringArrayUtils {
17 25
      * @return second element in specified array
18 26
      */
19 27
     public static String getSecondElement(String[] array) {
20
-        return null;
28
+
29
+        return array[1];
21 30
     }
22 31
 
23 32
     /**
@@ -25,7 +34,7 @@ public class StringArrayUtils {
25 34
      * @return last element in specified array
26 35
      */ // TODO
27 36
     public static String getLastElement(String[] array) {
28
-        return null;
37
+        return array[array.length - 1];
29 38
     }
30 39
 
31 40
     /**
@@ -33,7 +42,7 @@ public class StringArrayUtils {
33 42
      * @return second to last element in specified array
34 43
      */ // TODO
35 44
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
45
+        return array[array.length - 2];
37 46
     }
38 47
 
39 48
     /**
@@ -42,15 +51,27 @@ public class StringArrayUtils {
42 51
      * @return true if the array contains the specified `value`
43 52
      */ // TODO
44 53
     public static boolean contains(String[] array, String value) {
45
-        return false;
54
+        boolean str = false;
55
+        for (int x = 0; x < array.length; x++) {
56
+            if (value.equals(array[x])) {
57
+                      str = true;
58
+            }
59
+        } return str;
46 60
     }
47 61
 
48 62
     /**
49 63
      * @param array of String objects
50 64
      * @return an array with identical contents in reverse order
51 65
      */ // TODO
52
-    public static String[] reverse(String[] array) {
53
-        return null;
66
+    public static String[] reverse (String[] array) {
67
+
68
+        String[] str = new String[array.length];
69
+
70
+        // for (int i = 0; i < array.length; i++ ){
71
+        for (int i = (array.length - 1); i >= 0; i--) {
72
+            str[i] = array[array.length - (i + 1)];
73
+        }
74
+        return str;
54 75
     }
55 76
 
56 77
     /**
@@ -58,16 +79,38 @@ public class StringArrayUtils {
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
+        for (int i = 0; i < array.length / 2; i++) {
84
+            if (array[i] != array[array.length - 1 - i]) {
85
+                return false;
86
+            }
87
+        }
88
+        return true;
62 89
     }
63 90
 
64
-    /**
65
-     * @param array array of String objects
66
-     * @return true if each letter in the alphabet has been used in the array
91
+
92
+    /** // if the control reaches here, means all the elements were same
93
+     * @parareturn true;m array array of String objects
94
+     * @retu}rn true if each letter in the alphabet has been used in the array
67 95
      */ // TODO
68 96
     public static boolean isPangramic(String[] array) {
69
-        return false;
70
-    }
97
+
98
+              boolean isAPanGramic = true;
99
+              String strLower = "";
100
+
101
+          //Array - toLowerCase - Concat
102
+              for (String var : array) {
103
+                  strLower = strLower.concat(var.toLowerCase());
104
+              }
105
+          //loop through ANSI lower case values a-z
106
+              // IF! contains - boolean set to false
107
+              for(int i = 97; i <= 122; i++){
108
+                  if(!strLower.contains(Character.toString((char)i))){
109
+                      isAPanGramic = false;
110
+                  }
111
+              }
112
+              return isAPanGramic;
113
+          }
71 114
 
72 115
     /**
73 116
      * @param array array of String objects
@@ -75,33 +118,88 @@ public class StringArrayUtils {
75 118
      * @return number of occurrences the specified `value` has occurred
76 119
      */ // TODO
77 120
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
121
+          int count = 0;
122
+
123
+          for (String str: array){
124
+              if(value.equals(str) )
125
+                  count++;
126
+          }
127
+          return count;
79 128
     }
80 129
 
81 130
     /**
82
-     * @param array         array of String objects
131
+     * @param array array of String objects
83 132
      * @param valueToRemove value to remove from array
84 133
      * @return array with identical contents excluding values of `value`
85 134
      */ // TODO
86 135
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
88
-    }
89 136
 
137
+              List<String> list = new ArrayList<String>(Arrays.asList(array));
138
+
139
+              list.remove(valueToRemove);
140
+
141
+              array = list.toArray(new String[0]);
142
+
143
+
144
+              return array;
145
+
146
+   }
90 147
     /**
91 148
      * @param array array of chars
92 149
      * @return array of Strings with consecutive duplicates removes
93 150
      */ // TODO
94 151
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
96
-    }
97 152
 
153
+
154
+        ArrayList<String> rm = new ArrayList<String>();
155
+        rm.add(array[0]);
156
+        for (int i = 1; i < array.length; i++) {
157
+
158
+            if (!array[i - 1].equals(array[i])) {
159
+
160
+                rm.add(array[i]);
161
+            }
162
+        }
163
+
164
+        String[] newArray = new String[rm.size()];
165
+        newArray = rm.toArray(new String[rm.size()]);
166
+
167
+        return newArray;
168
+    }
169
+    
98 170
     /**
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
171
+     * @par  return newList;am array array of chars
172
+     * @ret}urn array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 173
      */ // TODO
102 174
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
175
+
176
+
177
+        ArrayList<String> arrayList = new ArrayList<String>();
178
+        String stri = new String();
179
+
180
+        stri = array[0];
181
+
182
+        arrayList.add(array[0]);
183
+        for (int i = 1; i < array.length; i++) {
184
+
185
+            if (array[i] == stri) {
186
+
187
+                arrayList.set(arrayList.size() - 1, arrayList.get(arrayList.size() - 1) + array[i]);
188
+
189
+            } else {
190
+
191
+                arrayList.add(array[i]);
192
+
193
+                stri = array[i];
194
+
195
+            }
196
+
197
+        }
198
+        String[] newArray = new String[arrayList.size()];
199
+        newArray = arrayList.toArray(new String[arrayList.size()]);
200
+
201
+        return newArray;
104 202
     }
105 203
 
106 204
 
107
-}
205
+    }

+ 0
- 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"};
@@ -176,18 +121,6 @@ public class StringArrayUtilsTest {
176 121
         }
177 122
     }
178 123
 
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191 124
     @Test
192 125
     public void testReverse1() {
193 126
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -213,15 +146,6 @@ public class StringArrayUtilsTest {
213 146
         Assert.assertEquals(expected, actual);
214 147
     }
215 148
 
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225 149
     @Test
226 150
     public void testIsPalindromic1() {
227 151
         String[] array = {"a", "b", "c", "b", "a"};
@@ -248,12 +172,6 @@ public class StringArrayUtilsTest {
248 172
 
249 173
 
250 174
 
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"};
@@ -359,16 +254,6 @@ public class StringArrayUtilsTest {
359 254
         Assert.assertEquals(actual, expected);
360 255
     }
361 256
 
362
-
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"};
@@ -396,13 +281,6 @@ public class StringArrayUtilsTest {
396 281
         Assert.assertEquals(expected, actual);
397 282
     }
398 283
 
399
-
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"};
@@ -427,15 +305,4 @@ public class StringArrayUtilsTest {
427 305
         Assert.assertEquals(expected, actual);
428 306
     }
429 307
 
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441 308
 }