#33 completed array string utilities

Offen
sambhutani möchte 1 Commits von sambhutani/CR-MicroLabs-Arrays-StringArrayUtilities:master nach master zusammenführen

+ 12
- 0
pom.xml Datei anzeigen

@@ -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>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 164
- 17
src/main/java/com/zipcodewilmington/StringArrayUtils.java Datei anzeigen

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

+ 1
- 1
src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java Datei anzeigen

@@ -171,7 +171,7 @@ public class StringArrayUtilsTest {
171 171
     public void testContains() {
172 172
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
173 173
         for (String s : array) {
174
-            boolean outcome = StringArrayUtils.contains(array, s);
174
+            boolean outcome = StringArrayUtils.contains(array,s);
175 175
             Assert.assertTrue(outcome);
176 176
         }
177 177
     }