Bladeren bron

StrinArrayUtilities

Soujanya Buragapu 6 jaren geleden
bovenliggende
commit
c4109b8868

+ 111
- 22
src/main/java/com/zipcodewilmington/StringArrayUtils.java Bestand weergeven

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

+ 0
- 147
src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java Bestand weergeven

@@ -33,19 +33,6 @@ public class StringArrayUtilsTest {
33 33
         Assert.assertEquals(expected, actual);
34 34
     }
35 35
 
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49 36
     @Test
50 37
     public void testGetSecondElement1() {
51 38
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -61,8 +48,6 @@ public class StringArrayUtilsTest {
61 48
         String actual = StringArrayUtils.getSecondElement(array);
62 49
         Assert.assertEquals(expected, actual);
63 50
     }
64
-
65
-
66 51
     @Test
67 52
     public void testGetSecondElement3() {
68 53
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -70,17 +55,6 @@ public class StringArrayUtilsTest {
70 55
         String actual = StringArrayUtils.getSecondElement(array);
71 56
         Assert.assertEquals(expected, actual);
72 57
     }
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84 58
     @Test
85 59
     public void testGetLastElement1() {
86 60
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -88,7 +62,6 @@ public class StringArrayUtilsTest {
88 62
         String actual = StringArrayUtils.getLastElement(array);
89 63
         Assert.assertEquals(expected, actual);
90 64
     }
91
-
92 65
     @Test
93 66
     public void testGetLastElement2() {
94 67
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy"};
@@ -96,8 +69,6 @@ public class StringArrayUtilsTest {
96 69
         String actual = StringArrayUtils.getLastElement(array);
97 70
         Assert.assertEquals(expected, actual);
98 71
     }
99
-
100
-
101 72
     @Test
102 73
     public void testGetLastElement3() {
103 74
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
@@ -105,27 +76,6 @@ public class StringArrayUtilsTest {
105 76
         String actual = StringArrayUtils.getLastElement(array);
106 77
         Assert.assertEquals(expected, actual);
107 78
     }
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129 79
     @Test
130 80
     public void testGetSecondToLastElement1() {
131 81
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -133,7 +83,6 @@ public class StringArrayUtilsTest {
133 83
         String actual = StringArrayUtils.getSecondToLastElement(array);
134 84
         Assert.assertEquals(expected, actual);
135 85
     }
136
-
137 86
     @Test
138 87
     public void testGetSecondToLastElement2() {
139 88
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "lazy"};
@@ -141,8 +90,6 @@ public class StringArrayUtilsTest {
141 90
         String actual = StringArrayUtils.getSecondToLastElement(array);
142 91
         Assert.assertEquals(expected, actual);
143 92
     }
144
-
145
-
146 93
     @Test
147 94
     public void testGetSecondToLastElement3() {
148 95
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
@@ -150,23 +97,6 @@ public class StringArrayUtilsTest {
150 97
         String actual = StringArrayUtils.getSecondToLastElement(array);
151 98
         Assert.assertEquals(expected, actual);
152 99
     }
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170 100
     @Test
171 101
     public void testContains() {
172 102
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -175,19 +105,6 @@ public class StringArrayUtilsTest {
175 105
             Assert.assertTrue(outcome);
176 106
         }
177 107
     }
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191 108
     @Test
192 109
     public void testReverse1() {
193 110
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -195,8 +112,6 @@ public class StringArrayUtilsTest {
195 112
         String[] actual = StringArrayUtils.reverse(array);
196 113
         Assert.assertEquals(expected, actual);
197 114
     }
198
-
199
-
200 115
     @Test
201 116
     public void testReverse2() {
202 117
         String[] array  = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
@@ -204,97 +119,54 @@ public class StringArrayUtilsTest {
204 119
         String[] actual = StringArrayUtils.reverse(array);
205 120
         Assert.assertEquals(expected, actual);
206 121
     }
207
-
208
-
209 122
     @Test
210 123
     public void testReverse3() {
211 124
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
212 125
         String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected));
213 126
         Assert.assertEquals(expected, actual);
214 127
     }
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225 128
     @Test
226 129
     public void testIsPalindromic1() {
227 130
         String[] array = {"a", "b", "c", "b", "a"};
228 131
         boolean outcome = StringArrayUtils.isPalindromic(array);
229 132
         Assert.assertTrue(outcome);
230 133
     }
231
-
232
-
233
-
234 134
     @Test
235 135
     public void testIsPalindromic2() {
236 136
         String[] array = {"Is this a palindrome?", "This is a palindrome", "Is this a palindrome?"};
237 137
         boolean outcome = StringArrayUtils.isPalindromic(array);
238 138
         Assert.assertTrue(outcome);
239 139
     }
240
-
241
-
242 140
     @Test
243 141
     public void testIsPalindromic3() {
244 142
         String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
245 143
         boolean outcome = StringArrayUtils.isPalindromic(array);
246 144
         Assert.assertFalse(outcome);
247 145
     }
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257 146
     @Test
258 147
     public void testIsPangramic1() {
259 148
         String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
260 149
         boolean outcome = StringArrayUtils.isPangramic(array);
261 150
         Assert.assertTrue(outcome);
262 151
     }
263
-
264 152
     @Test
265 153
     public void testIsPangramic2() {
266 154
         String[] array = {"The", "quick", "onyx", "goblin", "jumps", "over", "the", "lazy", "dwarf"};
267 155
         boolean outcome = StringArrayUtils.isPangramic(array);
268 156
         Assert.assertTrue(outcome);
269 157
     }
270
-
271 158
     @Test
272 159
     public void testIsPangramic3() {
273 160
         String[] array = {"Five quacking", "zephyrs", "jolt my", "wax bed"};
274 161
         boolean outcome = StringArrayUtils.isPangramic(array);
275 162
         Assert.assertTrue(outcome);
276 163
     }
277
-
278
-
279 164
     @Test
280 165
     public void testIsPangramic4() {
281 166
         String[] array = {"a", "b", "c", "d"};
282 167
         boolean outcome = StringArrayUtils.isPangramic(array);
283 168
         Assert.assertFalse(outcome);
284 169
     }
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298 170
     @Test
299 171
     public void testGetNumberOfOccurrences1() {
300 172
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -302,7 +174,6 @@ public class StringArrayUtilsTest {
302 174
         int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
303 175
         Assert.assertEquals(actual, expected);
304 176
     }
305
-
306 177
     @Test
307 178
     public void testGetNumberOfOccurrences2() {
308 179
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -310,7 +181,6 @@ public class StringArrayUtilsTest {
310 181
         int actual = StringArrayUtils.getNumberOfOccurrences(array, "bbb");
311 182
         Assert.assertEquals(actual, expected);
312 183
     }
313
-
314 184
     @Test
315 185
     public void testGetNumberOfOccurrences3() {
316 186
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -318,20 +188,6 @@ public class StringArrayUtilsTest {
318 188
         int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
319 189
         Assert.assertEquals(actual, expected);
320 190
     }
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335 191
     @Test
336 192
     public void testRemoveConsecutiveDuplicates1() {
337 193
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -339,9 +195,6 @@ public class StringArrayUtilsTest {
339 195
         String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
340 196
         Assert.assertEquals(actual, expected);
341 197
     }
342
-
343
-
344
-
345 198
     @Test
346 199
     public void testRemoveConsecutiveDuplicates2() {
347 200
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};