Parcourir la source

completed lab Xcuello

Xcuello il y a 6 ans
Parent
révision
043f5ee5b1

+ 128
- 11
src/main/java/com/zipcodewilmington/StringArrayUtils.java Voir le fichier

@@ -1,5 +1,8 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
3 6
 /**
4 7
  * Created by leon on 1/29/18.
5 8
  */
@@ -9,7 +12,9 @@ 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];
17
+
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,9 @@ 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
+
36
+        return array[array.length - 1];
29 37
     }
30 38
 
31 39
     /**
@@ -33,7 +41,8 @@ public class StringArrayUtils {
33 41
      * @return second to last element in specified array
34 42
      */ // TODO
35 43
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
44
+
45
+        return array[array.length - 2];
37 46
     }
38 47
 
39 48
     /**
@@ -42,6 +51,12 @@ 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) {
54
+
55
+        for (String v : array) {
56
+            if (value == v) {
57
+                return true;
58
+            }
59
+        }
45 60
         return false;
46 61
     }
47 62
 
@@ -50,15 +65,33 @@ public class StringArrayUtils {
50 65
      * @return an array with identical contents in reverse order
51 66
      */ // TODO
52 67
     public static String[] reverse(String[] array) {
53
-        return null;
68
+
69
+        String temp = "";
70
+
71
+        for (int i = 0; i < array.length / 2; i++) {
72
+
73
+            temp = array[i];
74
+            array[i] = array[array.length - i - 1];
75
+            array[array.length - i - 1] = temp;
76
+        }
77
+        return array;
54 78
     }
55 79
 
80
+
56 81
     /**
57 82
      * @param array array of String objects
58 83
      * @return true if the order of the array is the same backwards and forwards
59 84
      */ // TODO
60 85
     public static boolean isPalindromic(String[] array) {
61
-        return false;
86
+
87
+        for (int i = 0; i < array.length; i++) {
88
+            if (!array[i].equals(array[array.length - 1 - i])) {
89
+
90
+                return false;
91
+
92
+            }
93
+        }
94
+        return true;
62 95
     }
63 96
 
64 97
     /**
@@ -66,16 +99,50 @@ public class StringArrayUtils {
66 99
      * @return true if each letter in the alphabet has been used in the array
67 100
      */ // TODO
68 101
     public static boolean isPangramic(String[] array) {
69
-        return false;
102
+
103
+        String combo = "";
104
+
105
+        for (int i = 0; i < array.length; i++){
106
+            String temp = array[i].toLowerCase();
107
+            String[] tempArray = temp.split(" ");
108
+            for (int j = 0; j < tempArray.length; j++){
109
+                combo += tempArray[j];
110
+            }
111
+        }
112
+        String[] apart = combo.split("");
113
+
114
+        String[] counter = new String[26];
115
+        for (int i=0; i<apart.length; i++) { //z-z=0 ;  z-a=25 (26-26=0  ; 26-1=25)
116
+            if (counter['z' - apart[i].charAt(0)] == null) {
117
+                counter['z' - apart[i].charAt(0)] = apart[i];
118
+            }
119
+        }
120
+        for (int i=0; i<26; i++){
121
+            if (counter[i]==null){
122
+                return false;
123
+            }
124
+        }
125
+        return true;
70 126
     }
71 127
 
128
+
72 129
     /**
73 130
      * @param array array of String objects
74 131
      * @param value value to check array for
75 132
      * @return number of occurrences the specified `value` has occurred
76 133
      */ // TODO
77 134
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
135
+
136
+        int count = 0;
137
+
138
+        for (int i = 0; i < array.length; i++) {
139
+            if (array[i] == value) {
140
+                count++;
141
+            }
142
+        }
143
+
144
+
145
+        return count;
79 146
     }
80 147
 
81 148
     /**
@@ -84,7 +151,20 @@ public class StringArrayUtils {
84 151
      * @return array with identical contents excluding values of `value`
85 152
      */ // TODO
86 153
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
154
+
155
+        List<String> arrList = new ArrayList<String>();
156
+
157
+        for (int i = 0; i < array.length; i++) {
158
+            if (array[i] != valueToRemove) {
159
+
160
+                arrList.add(array[i]);
161
+
162
+            }
163
+        }
164
+        String[] stringArr = new String[arrList.size()];
165
+        stringArr = arrList.toArray(stringArr);
166
+
167
+        return stringArr;
88 168
     }
89 169
 
90 170
     /**
@@ -92,7 +172,26 @@ public class StringArrayUtils {
92 172
      * @return array of Strings with consecutive duplicates removes
93 173
      */ // TODO
94 174
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
175
+
176
+        ArrayList<String> newArr = new ArrayList<String>();
177
+
178
+        newArr.add(array[0]);
179
+        String last = array[0];
180
+
181
+        for (int i = 1; i < array.length; i++) {
182
+
183
+            String nextWord = array[i];
184
+
185
+            if(nextWord.equals(last)) {
186
+                newArr.set(newArr.size()-1, newArr.get(newArr.size()-1));
187
+
188
+            } else {
189
+                newArr.add(nextWord);
190
+                last = nextWord;
191
+            }
192
+        }
193
+
194
+        return newArr.toArray(new String[newArr.size()-1]);
96 195
     }
97 196
 
98 197
     /**
@@ -100,7 +199,25 @@ public class StringArrayUtils {
100 199
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 200
      */ // TODO
102 201
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
202
+
203
+        ArrayList<String> newArr = new ArrayList<String>();
204
+
205
+        newArr.add(array[0]);
206
+        String last = array[0];
207
+
208
+        for (int i = 1; i < array.length; i++) {
209
+
210
+            String nextWord = array[i];
211
+
212
+            if(nextWord.equals(last)) {
213
+                newArr.set(newArr.size()-1, newArr.get(newArr.size()-1)+nextWord);
214
+
215
+            } else {
216
+                newArr.add(nextWord);
217
+                last = nextWord;
218
+            }
219
+        }
220
+        return newArr.toArray(new String[newArr.size()-1]);
104 221
     }
105 222
 
106 223
 

+ 1
- 1
src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java Voir le fichier

@@ -337,7 +337,7 @@ public class StringArrayUtilsTest {
337 337
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
338 338
         String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
339 339
         String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
340
-        Assert.assertEquals(actual, expected);
340
+        Assert.assertArrayEquals(expected, actual);
341 341
     }
342 342
 
343 343