|
|
@@ -1,5 +1,7 @@
|
|
1
|
1
|
package com.zipcodewilmington;
|
|
2
|
2
|
|
|
|
3
|
+import java.util.Arrays;
|
|
|
4
|
+
|
|
3
|
5
|
/**
|
|
4
|
6
|
* Created by leon on 1/29/18.
|
|
5
|
7
|
*/
|
|
|
@@ -9,7 +11,7 @@ public class StringArrayUtils {
|
|
9
|
11
|
* @return first element of specified array
|
|
10
|
12
|
*/ // TODO
|
|
11
|
13
|
public static String getFirstElement(String[] array) {
|
|
12
|
|
- return null;
|
|
|
14
|
+ return array[0];
|
|
13
|
15
|
}
|
|
14
|
16
|
|
|
15
|
17
|
/**
|
|
|
@@ -17,7 +19,7 @@ public class StringArrayUtils {
|
|
17
|
19
|
* @return second element in specified array
|
|
18
|
20
|
*/
|
|
19
|
21
|
public static String getSecondElement(String[] array) {
|
|
20
|
|
- return null;
|
|
|
22
|
+ return array[1];
|
|
21
|
23
|
}
|
|
22
|
24
|
|
|
23
|
25
|
/**
|
|
|
@@ -25,7 +27,7 @@ public class StringArrayUtils {
|
|
25
|
27
|
* @return last element in specified array
|
|
26
|
28
|
*/ // TODO
|
|
27
|
29
|
public static String getLastElement(String[] array) {
|
|
28
|
|
- return null;
|
|
|
30
|
+ return array[array.length - 1];
|
|
29
|
31
|
}
|
|
30
|
32
|
|
|
31
|
33
|
/**
|
|
|
@@ -33,7 +35,7 @@ public class StringArrayUtils {
|
|
33
|
35
|
* @return second to last element in specified array
|
|
34
|
36
|
*/ // TODO
|
|
35
|
37
|
public static String getSecondToLastElement(String[] array) {
|
|
36
|
|
- return null;
|
|
|
38
|
+ return array[array.length - 2];
|
|
37
|
39
|
}
|
|
38
|
40
|
|
|
39
|
41
|
/**
|
|
|
@@ -42,40 +44,114 @@ public class StringArrayUtils {
|
|
42
|
44
|
* @return true if the array contains the specified `value`
|
|
43
|
45
|
*/ // TODO
|
|
44
|
46
|
public static boolean contains(String[] array, String value) {
|
|
45
|
|
- return false;
|
|
|
47
|
+
|
|
|
48
|
+ boolean result = false;
|
|
|
49
|
+
|
|
|
50
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
51
|
+ if (array[i] == value) {
|
|
|
52
|
+ result = true;
|
|
|
53
|
+ break;
|
|
|
54
|
+ }
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ return result;
|
|
46
|
58
|
}
|
|
47
|
59
|
|
|
48
|
60
|
/**
|
|
49
|
61
|
* @param array of String objects
|
|
50
|
62
|
* @return an array with identical contents in reverse order
|
|
51
|
63
|
*/ // TODO
|
|
|
64
|
+ // Make an empty String array.
|
|
|
65
|
+ // Set the last element of the original array to the first of the empty one.
|
|
|
66
|
+ // Keep looping til the number of iterations reach the end of the array.
|
|
52
|
67
|
public static String[] reverse(String[] array) {
|
|
53
|
|
- return null;
|
|
|
68
|
+
|
|
|
69
|
+ String[] reverseArray = new String[array.length];
|
|
|
70
|
+
|
|
|
71
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
72
|
+ reverseArray[i] = array[array.length - (i + 1)];
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ return reverseArray;
|
|
54
|
76
|
}
|
|
55
|
77
|
|
|
56
|
78
|
/**
|
|
57
|
79
|
* @param array array of String objects
|
|
58
|
80
|
* @return true if the order of the array is the same backwards and forwards
|
|
59
|
81
|
*/ // TODO
|
|
|
82
|
+ // Basically you wanna reverse the array and then see if the first half of the arrays are the same?
|
|
60
|
83
|
public static boolean isPalindromic(String[] array) {
|
|
61
|
|
- return false;
|
|
|
84
|
+
|
|
|
85
|
+ String[] reverseArray = new String[array.length];
|
|
|
86
|
+
|
|
|
87
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
88
|
+ reverseArray[i] = array[array.length - (i + 1)];
|
|
|
89
|
+ }
|
|
|
90
|
+
|
|
|
91
|
+ boolean palindrome = false;
|
|
|
92
|
+
|
|
|
93
|
+ for (int i = 0; i < (array.length / 2); i++)
|
|
|
94
|
+ if (reverseArray[i] == array[i]) {
|
|
|
95
|
+ palindrome = true;
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ return palindrome;
|
|
|
99
|
+
|
|
62
|
100
|
}
|
|
63
|
101
|
|
|
64
|
102
|
/**
|
|
65
|
103
|
* @param array array of String objects
|
|
66
|
104
|
* @return true if each letter in the alphabet has been used in the array
|
|
67
|
105
|
*/ // TODO
|
|
|
106
|
+ // Convert array to string
|
|
|
107
|
+ // BOOLEAN ARRAY?? length of 26 so each index matches the number of an alphbet (0 = a, 1 = b, etc).
|
|
|
108
|
+ // Convert letters to ASCII, then 0-25. Then for each letter of the array,
|
|
|
109
|
+ // log true for the number of the character at the index. If it's already logged
|
|
|
110
|
+ // true just move on. If the full boolean array is true then return true?
|
|
68
|
111
|
public static boolean isPangramic(String[] array) {
|
|
69
|
|
- return false;
|
|
|
112
|
+
|
|
|
113
|
+ boolean[] alphabet = new boolean[26];
|
|
|
114
|
+
|
|
|
115
|
+ String stringArray = Arrays.toString(array).toLowerCase();
|
|
|
116
|
+
|
|
|
117
|
+ int alphaNum;
|
|
|
118
|
+
|
|
|
119
|
+ for (int i = 0; i < stringArray.length(); i++) {
|
|
|
120
|
+ if (stringArray.charAt(i) != ' ' && stringArray.charAt(i) != '[' && stringArray.charAt(i) != ']' && stringArray.charAt(i) != ',') {
|
|
|
121
|
+ alphaNum = ((int) stringArray.charAt(i)) - 97;
|
|
|
122
|
+ alphabet[alphaNum] = true;
|
|
|
123
|
+ }
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+ boolean result = true;
|
|
|
127
|
+
|
|
|
128
|
+ for (int i = 0; i < alphabet.length; i++) {
|
|
|
129
|
+ if (alphabet[i] == false) {
|
|
|
130
|
+ result = false;
|
|
|
131
|
+ break;
|
|
|
132
|
+ }
|
|
|
133
|
+ }
|
|
|
134
|
+ return result;
|
|
70
|
135
|
}
|
|
71
|
136
|
|
|
|
137
|
+
|
|
72
|
138
|
/**
|
|
73
|
139
|
* @param array array of String objects
|
|
74
|
140
|
* @param value value to check array for
|
|
75
|
141
|
* @return number of occurrences the specified `value` has occurred
|
|
76
|
142
|
*/ // TODO
|
|
|
143
|
+ // Log occurrences in int set to 0.
|
|
|
144
|
+ // For each element in the array, if the specified value equals the value of the element, increase 1.
|
|
77
|
145
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
|
78
|
|
- return 0;
|
|
|
146
|
+
|
|
|
147
|
+ int occurrences = 0;
|
|
|
148
|
+
|
|
|
149
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
150
|
+ if (array[i].equalsIgnoreCase(value)) {
|
|
|
151
|
+ occurrences++;
|
|
|
152
|
+ }
|
|
|
153
|
+ }
|
|
|
154
|
+ return occurrences;
|
|
79
|
155
|
}
|
|
80
|
156
|
|
|
81
|
157
|
/**
|
|
|
@@ -83,24 +159,113 @@ public class StringArrayUtils {
|
|
83
|
159
|
* @param valueToRemove value to remove from array
|
|
84
|
160
|
* @return array with identical contents excluding values of `value`
|
|
85
|
161
|
*/ // TODO
|
|
|
162
|
+ // Check if the elements of each array is equal to the value to remove.
|
|
|
163
|
+ // If yes... log occurrences of value, but do nothing with it.
|
|
|
164
|
+ // Create empty string array whose length is the original minus the occurrences.
|
|
|
165
|
+ // If no... assign current operating index of new array as the value.
|
|
|
166
|
+
|
|
86
|
167
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
87
|
|
- return null;
|
|
|
168
|
+
|
|
|
169
|
+ int occurrences = 0;
|
|
|
170
|
+
|
|
|
171
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
172
|
+ if (array[i].equals(valueToRemove)) {
|
|
|
173
|
+ occurrences++;
|
|
|
174
|
+ }
|
|
|
175
|
+ }
|
|
|
176
|
+
|
|
|
177
|
+ String[] newArray = new String[array.length - occurrences];
|
|
|
178
|
+ int j = 0;
|
|
|
179
|
+
|
|
|
180
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
181
|
+ if (!array[i].equals(valueToRemove)) {
|
|
|
182
|
+ newArray[j] = array[i];
|
|
|
183
|
+ j++;
|
|
|
184
|
+ }
|
|
|
185
|
+ }
|
|
|
186
|
+ return newArray;
|
|
88
|
187
|
}
|
|
89
|
188
|
|
|
90
|
189
|
/**
|
|
91
|
190
|
* @param array array of chars
|
|
92
|
191
|
* @return array of Strings with consecutive duplicates removes
|
|
93
|
192
|
*/ // TODO
|
|
|
193
|
+ // Check each element, if array[i] is equal to array[i - 1], log the occurrence.
|
|
|
194
|
+ // The length of that array will be the number of occurrences of duplicates subtracted from original length?
|
|
|
195
|
+ // Then make a "result" array that's the original array length minus occurrences.
|
|
|
196
|
+ // Then basically loop through the original array again, somehow skip the logged duplicates,
|
|
|
197
|
+ // and only put the non-duplicate elements into the result array.
|
|
|
198
|
+
|
|
94
|
199
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
95
|
|
- return null;
|
|
|
200
|
+
|
|
|
201
|
+ int occurrences = 0;
|
|
|
202
|
+
|
|
|
203
|
+ for (int i = 1; i < array.length; i++) {
|
|
|
204
|
+ if (array[i].equals(array[i - 1])) {
|
|
|
205
|
+ occurrences++;
|
|
|
206
|
+ }
|
|
|
207
|
+ }
|
|
|
208
|
+
|
|
|
209
|
+ String[] newArray = new String[array.length - occurrences];
|
|
|
210
|
+
|
|
|
211
|
+ // j is the index of the new array that we're populating, comparison is helping us keep track of which duplicates we've logged.
|
|
|
212
|
+ int j = 0;
|
|
|
213
|
+ String compareDuplicate = "";
|
|
|
214
|
+
|
|
|
215
|
+ // We're checking each element of the array to the one before it, so we start at index 1 so we don't get an out of bounds error.
|
|
|
216
|
+ // So just immediately log the first element in the new array.
|
|
|
217
|
+ newArray[0] = array[0];
|
|
|
218
|
+
|
|
|
219
|
+ // Then, for each element of the array, if it doesn't equal the comparison value, put the value in the new array and move to the next new array index.
|
|
|
220
|
+ for (int i = 1; i < array.length; i++) {
|
|
|
221
|
+ if (!array[i].equals(compareDuplicate)) {
|
|
|
222
|
+ newArray[j] = array[i];
|
|
|
223
|
+ compareDuplicate = newArray[j];
|
|
|
224
|
+ j++;
|
|
|
225
|
+ }
|
|
|
226
|
+ }
|
|
|
227
|
+
|
|
|
228
|
+ return newArray;
|
|
96
|
229
|
}
|
|
97
|
230
|
|
|
98
|
231
|
/**
|
|
99
|
232
|
* @param array array of chars
|
|
100
|
233
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
101
|
234
|
*/ // TODO
|
|
|
235
|
+
|
|
102
|
236
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
103
|
|
- return null;
|
|
|
237
|
+
|
|
|
238
|
+ String[] newArray;
|
|
|
239
|
+
|
|
|
240
|
+ String stringArray = Arrays.toString(array);
|
|
|
241
|
+ StringBuilder lettersOnly = new StringBuilder();
|
|
|
242
|
+
|
|
|
243
|
+ // This loop sorts through the string and only keeps the alphabet characters (so no weird symbols or spaces)
|
|
|
244
|
+ for (int i = 0; i < stringArray.length() - 1; i++) {
|
|
|
245
|
+ if (stringArray.charAt(i) != '[' && stringArray.charAt(i) != ',' && stringArray.charAt(i) != ' ' && stringArray.charAt(i) != ']') {
|
|
|
246
|
+ lettersOnly.append(stringArray.charAt(i));
|
|
|
247
|
+ }
|
|
|
248
|
+ }
|
|
|
249
|
+
|
|
|
250
|
+ StringBuilder groupSameLetters = new StringBuilder();
|
|
|
251
|
+
|
|
|
252
|
+ // Since we're comparing the character to the one before it, we gotta start at index 1 so that we don't get an out of index error.
|
|
|
253
|
+ // So this just appends the first character automatically to the front of the new StringBuilder.
|
|
|
254
|
+ groupSameLetters.append(lettersOnly.charAt(0));
|
|
|
255
|
+
|
|
|
256
|
+ // This loop compares the character to the one before it, and if it's not the same, append an underscore before the character.
|
|
|
257
|
+ for (int i = 1; i < lettersOnly.length(); i++) {
|
|
|
258
|
+ if (lettersOnly.charAt(i) != (lettersOnly.charAt(i - 1))) {
|
|
|
259
|
+ groupSameLetters.append('_');
|
|
|
260
|
+ groupSameLetters.append(lettersOnly.charAt(i));
|
|
|
261
|
+ } else {
|
|
|
262
|
+ groupSameLetters.append(lettersOnly.charAt(i));
|
|
|
263
|
+ }
|
|
|
264
|
+ }
|
|
|
265
|
+
|
|
|
266
|
+ newArray = groupSameLetters.toString().split("_");
|
|
|
267
|
+
|
|
|
268
|
+ return newArray;
|
|
104
|
269
|
}
|
|
105
|
270
|
|
|
106
|
271
|
|