Yesoda Sanka 8 лет назад
Родитель
Сommit
6fb1f06dfa

+ 166
- 40
src/main/java/com/zipcodewilmington/StringArrayUtils.java Просмотреть файл

@@ -1,56 +1,56 @@
1 1
 package com.zipcodewilmington;
2
+import java.util.HashSet;
3
+import java.util.*;
4
+import java.util.List;
5
+//import com.sun.tools.javac.util.ArrayUtils;
6
+import java.util.ArrayList;
7
+import java.lang.CharSequence;
8
+
9
+import java.util.*;
2 10
 
3
-/**
4
- * Created by leon on 1/29/18.
5
- */
6 11
 public class StringArrayUtils {
7
-    /**
8
-     * @param array array of String objects
9
-     * @return first element of specified array
10
-     */ // TODO
12
+
11 13
     public static String getFirstElement(String[] array) {
12
-        return null;
14
+
15
+        return array[0];
13 16
     }
14 17
 
15
-    /**
16
-     * @param array array of String objects
17
-     * @return second element in specified array
18
-     */
19 18
     public static String getSecondElement(String[] array) {
20
-        return null;
19
+
20
+        return array[1];
21 21
     }
22 22
 
23
-    /**
24
-     * @param array array of String objects
25
-     * @return last element in specified array
26
-     */ // TODO
23
+
27 24
     public static String getLastElement(String[] array) {
28
-        return null;
25
+        return array[array.length - 1];
29 26
     }
30 27
 
31
-    /**
32
-     * @param array array of String objects
33
-     * @return second to last element in specified array
34
-     */ // TODO
28
+
35 29
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
30
+
31
+        return array[array.length - 2];
37 32
     }
38 33
 
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
44 34
     public static boolean contains(String[] array, String value) {
35
+
36
+        for (String str : array) {
37
+            if (value.equals(str))
38
+                return true;
39
+        }
45 40
         return false;
46 41
     }
47 42
 
48
-    /**
49
-     * @param array of String objects
50
-     * @return an array with identical contents in reverse order
51
-     */ // TODO
52 43
     public static String[] reverse(String[] array) {
53
-        return null;
44
+
45
+        String[] myStr = new String[array.length];
46
+        int myInt;
47
+        myInt = (array.length) - 1;
48
+        for (String str : array) {
49
+            myStr[myInt--] = str;
50
+            System.out.println(str);
51
+        }
52
+        //  int i = array.length;
53
+        return myStr;
54 54
     }
55 55
 
56 56
     /**
@@ -58,6 +58,17 @@ public class StringArrayUtils {
58 58
      * @return true if the order of the array is the same backwards and forwards
59 59
      */ // TODO
60 60
     public static boolean isPalindromic(String[] array) {
61
+        String s[] = new String[array.length];
62
+        int myInt;
63
+        myInt = (array.length) - 1;
64
+        for (String str : array) {
65
+            s[myInt--] = str;
66
+        }
67
+
68
+        for (int i = 0, j = 0; i < array.length; i++, j++) {
69
+            if (array[i].equals(s[j]))
70
+                return true;
71
+        }
61 72
         return false;
62 73
     }
63 74
 
@@ -66,16 +77,44 @@ public class StringArrayUtils {
66 77
      * @return true if each letter in the alphabet has been used in the array
67 78
      */ // TODO
68 79
     public static boolean isPangramic(String[] array) {
69
-        return false;
80
+        StringBuffer sb = new StringBuffer();
81
+        String alpha = "abcdefghijklmnopqrstuvwxyz";
82
+        int count = 0;
83
+        String str = "";
84
+        // create a string from stringarray
85
+        for (int i = 0; i < array.length; i++) {
86
+            sb.append(array[i]);
87
+        }
88
+        str = sb.toString().toLowerCase();
89
+        //check if all the alphabets are in the input array
90
+        for (int i = 0; i < 26; i++) {
91
+            if (str.contains( String.valueOf(alpha.charAt(i)))) {
92
+                count++;
93
+            }
94
+            System.out.println("Total count value" + count);
95
+        }
96
+
97
+        //check if counter is 26
98
+        if (count == 26)
99
+            return true;
100
+        else
101
+            return false;
70 102
     }
71 103
 
72
-    /**
104
+
105
+
106
+    /*}*
73 107
      * @param array array of String objects
74 108
      * @param value value to check array for
75 109
      * @return number of occurrences the specified `value` has occurred
76 110
      */ // TODO
77 111
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
112
+        int count=0;
113
+        for (String str : array) {
114
+            if (value.equals(str))
115
+                count++;
116
+        }
117
+        return count;
79 118
     }
80 119
 
81 120
     /**
@@ -84,7 +123,18 @@ public class StringArrayUtils {
84 123
      * @return array with identical contents excluding values of `value`
85 124
      */ // TODO
86 125
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
126
+
127
+
128
+        ArrayList<String> nameList = new ArrayList<String>();
129
+        for(String str : array) {
130
+            if (!valueToRemove.equalsIgnoreCase(str)) {
131
+                nameList.add(str);
132
+            }
133
+        }
134
+            String[] names = nameList.toArray(new String[nameList.size()]);
135
+
136
+
137
+        return names;
88 138
     }
89 139
 
90 140
     /**
@@ -92,7 +142,42 @@ public class StringArrayUtils {
92 142
      * @return array of Strings with consecutive duplicates removes
93 143
      */ // TODO
94 144
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
145
+      /*  ArrayList<String> nameList = new ArrayList<String>();
146
+        Collections.addAll(nameList,array) ;
147
+        HashSet<String> hs = new HashSet();
148
+        hs.add(nameList);
149
+        nameList.clear() ;
150
+        nameList.addAll(hs);
151
+        String[] names = nameList.toArray(new String[nameList.size()]);
152
+*/
153
+       ArrayList<String> nameList=new ArrayList<String>();
154
+       int len=array.length;
155
+        for(int i=0;i<len;i++) {
156
+            if (i + 1 < len && (array[i].equals(array[i + 1]))) {
157
+                continue;
158
+            } else {
159
+                nameList.add(array[i]);
160
+            }
161
+        }
162
+            String names[]= nameList.toArray(new String[nameList.size()]);
163
+
164
+      return names;
165
+
166
+     /*  String s[]=new String[array.length];
167
+       int len =array.length;
168
+        String str[]={};
169
+       for(int i=0;i<=len;i++) {
170
+           if (i+1<len && (array[i].equals( array[i + 1]))) {
171
+               continue;
172
+           }
173
+           else
174
+           {
175
+
176
+            System.out.println(array[i]);
177
+
178
+           }
179
+       }*/
180
+
96 181
     }
97 182
 
98 183
     /**
@@ -100,8 +185,49 @@ public class StringArrayUtils {
100 185
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 186
      */ // TODO
102 187
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105 188
 
106 189
 
190
+     ArrayList< String>  str1=new ArrayList<String>();
191
+
192
+      int len=array.length;
193
+
194
+      StringBuffer sbTemp = new StringBuffer("");
195
+      boolean flag = false;
196
+      int i=0;
197
+    while (i < len-1  ) {
198
+
199
+    if (array[i].equalsIgnoreCase(array[i + 1])) {
200
+              flag = true;
201
+              sbTemp.append(array[i]);
202
+          } else {
203
+              if (flag==true) {
204
+                  sbTemp.append(array[i]);
205
+                  str1.add(sbTemp.toString());
206
+                  sbTemp = new StringBuffer("");
207
+              } else {
208
+                  str1.add(array[i]);
209
+
210
+              }
211
+              flag = false;
212
+          }
213
+          i++;
214
+
215
+      }
216
+        if (flag==true) {
217
+            sbTemp.append(array[i]);
218
+            str1.add(sbTemp.toString());
219
+            sbTemp = new StringBuffer("");
220
+        } else {
221
+            str1.add(array[i]);
222
+
223
+        }
224
+
225
+      String names[]= str1.toArray(new String[str1.size()]);
226
+
227
+             System.out.println(names);
228
+
229
+            return names;
230
+
231
+
232
+    }
107 233
 }

+ 12
- 26
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
 
@@ -246,14 +246,6 @@ public class StringArrayUtilsTest {
246 246
         Assert.assertFalse(outcome);
247 247
     }
248 248
 
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257 249
     @Test
258 250
     public void testIsPangramic1() {
259 251
         String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
@@ -324,7 +316,13 @@ public class StringArrayUtilsTest {
324 316
 
325 317
 
326 318
 
327
-
319
+    @Test
320
+    public void testRemoveValue() {
321
+        String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
322
+        String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
323
+        String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
324
+        Assert.assertEquals(actual, expected);
325
+    }
328 326
 
329 327
 
330 328
 
@@ -365,10 +363,6 @@ public class StringArrayUtilsTest {
365 363
 
366 364
 
367 365
 
368
-
369
-
370
-
371
-
372 366
     @Test
373 367
     public void testRemovePackDuplicates1() {
374 368
         String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
@@ -404,19 +398,11 @@ public class StringArrayUtilsTest {
404 398
 
405 399
 
406 400
     @Test
407
-    public void testRemoveValue() {
408
-        String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
409
-        String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
410
-        String[] actual = StringArrayUtils.removeValue(array, "The");
411
-        Assert.assertEquals(expected, actual);
412
-    }
413
-
414
-    @Test
415 401
     public void testRemoveValue1() {
416 402
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
417 403
         String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
418 404
         String[] actual = StringArrayUtils.removeValue(array, "quick");
419
-        Assert.assertEquals(expected, actual);
405
+        Assert.assertArrayEquals(expected, actual) ;
420 406
     }
421 407
 
422 408
     @Test
@@ -424,7 +410,7 @@ public class StringArrayUtilsTest {
424 410
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
425 411
         String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
426 412
         String[] actual = StringArrayUtils.removeValue(array, "brown");
427
-        Assert.assertEquals(expected, actual);
413
+        Assert.assertArrayEquals(expected, actual);
428 414
     }
429 415
 
430 416