|
|
@@ -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,7 +44,16 @@ 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
|
+
|
|
|
52
|
+ if (array[i] == value) {
|
|
|
53
|
+ result = true;}
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ return result;
|
|
46
|
57
|
}
|
|
47
|
58
|
|
|
48
|
59
|
/**
|
|
|
@@ -50,7 +61,14 @@ public class StringArrayUtils {
|
|
50
|
61
|
* @return an array with identical contents in reverse order
|
|
51
|
62
|
*/ // TODO
|
|
52
|
63
|
public static String[] reverse(String[] array) {
|
|
53
|
|
- return null;
|
|
|
64
|
+ String[] reverse = new String[array.length - 1];
|
|
|
65
|
+ int counter = array.length - 1;
|
|
|
66
|
+
|
|
|
67
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
68
|
+ reverse[i] = array[counter];
|
|
|
69
|
+ counter--;
|
|
|
70
|
+ }
|
|
|
71
|
+ return reverse;
|
|
54
|
72
|
}
|
|
55
|
73
|
|
|
56
|
74
|
/**
|
|
|
@@ -58,7 +76,19 @@ public class StringArrayUtils {
|
|
58
|
76
|
* @return true if the order of the array is the same backwards and forwards
|
|
59
|
77
|
*/ // TODO
|
|
60
|
78
|
public static boolean isPalindromic(String[] array) {
|
|
61
|
|
- return false;
|
|
|
79
|
+ String[] reverse = new String[array.length];
|
|
|
80
|
+ int counter = array.length - 1;
|
|
|
81
|
+ boolean result = false;
|
|
|
82
|
+
|
|
|
83
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
84
|
+ reverse[i] = array[counter];
|
|
|
85
|
+ counter--;
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ if (Arrays.equals(array, reverse)) {
|
|
|
89
|
+ result = true;}
|
|
|
90
|
+
|
|
|
91
|
+ return result;
|
|
62
|
92
|
}
|
|
63
|
93
|
|
|
64
|
94
|
/**
|
|
|
@@ -66,7 +96,32 @@ public class StringArrayUtils {
|
|
66
|
96
|
* @return true if each letter in the alphabet has been used in the array
|
|
67
|
97
|
*/ // TODO
|
|
68
|
98
|
public static boolean isPangramic(String[] array) {
|
|
69
|
|
- return false;
|
|
|
99
|
+ int counter = 0;
|
|
|
100
|
+ char charmatch = 'a';
|
|
|
101
|
+ boolean result = false;
|
|
|
102
|
+ int i = 0;
|
|
|
103
|
+
|
|
|
104
|
+ while (charmatch <= 122) {
|
|
|
105
|
+
|
|
|
106
|
+ while (i < array.length) {
|
|
|
107
|
+ String value = array[i];
|
|
|
108
|
+ value = value.toLowerCase();
|
|
|
109
|
+ String thechar = String.valueOf(charmatch);
|
|
|
110
|
+ if (value.contains(thechar)) {
|
|
|
111
|
+ counter++;
|
|
|
112
|
+ i++;
|
|
|
113
|
+ } else { i++;}
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+ charmatch++;
|
|
|
117
|
+ i = 0;
|
|
|
118
|
+
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ if (counter == 26) {
|
|
|
122
|
+ result = true;}
|
|
|
123
|
+
|
|
|
124
|
+ return result;
|
|
70
|
125
|
}
|
|
71
|
126
|
|
|
72
|
127
|
/**
|
|
|
@@ -75,7 +130,15 @@ public class StringArrayUtils {
|
|
75
|
130
|
* @return number of occurrences the specified `value` has occurred
|
|
76
|
131
|
*/ // TODO
|
|
77
|
132
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
|
78
|
|
- return 0;
|
|
|
133
|
+ int counter = 0;
|
|
|
134
|
+
|
|
|
135
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
136
|
+
|
|
|
137
|
+ if (array[i].equals(value)) {
|
|
|
138
|
+ counter++;
|
|
|
139
|
+ }
|
|
|
140
|
+ }
|
|
|
141
|
+ return counter;
|
|
79
|
142
|
}
|
|
80
|
143
|
|
|
81
|
144
|
/**
|
|
|
@@ -84,7 +147,17 @@ public class StringArrayUtils {
|
|
84
|
147
|
* @return array with identical contents excluding values of `value`
|
|
85
|
148
|
*/ // TODO
|
|
86
|
149
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
87
|
|
- return null;
|
|
|
150
|
+ String[] newArray = new String[array.length - 2];
|
|
|
151
|
+ int counter = 0;
|
|
|
152
|
+
|
|
|
153
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
154
|
+
|
|
|
155
|
+ if (array[i].equals(valueToRemove)) {
|
|
|
156
|
+ counter++;} else {newArray[i] = array[counter];
|
|
|
157
|
+ }
|
|
|
158
|
+ counter++;
|
|
|
159
|
+ }
|
|
|
160
|
+ return newArray;
|
|
88
|
161
|
}
|
|
89
|
162
|
|
|
90
|
163
|
/**
|
|
|
@@ -92,7 +165,26 @@ public class StringArrayUtils {
|
|
92
|
165
|
* @return array of Strings with consecutive duplicates removes
|
|
93
|
166
|
*/ // TODO
|
|
94
|
167
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
95
|
|
- return null;
|
|
|
168
|
+ int counter = 0;
|
|
|
169
|
+
|
|
|
170
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
171
|
+ if (array[i].equals(array[i+1])) {
|
|
|
172
|
+ counter++;
|
|
|
173
|
+ }}
|
|
|
174
|
+
|
|
|
175
|
+ int newArrayLength = (array.length - 1) - counter;
|
|
|
176
|
+ String[] newArray = new String[newArrayLength];
|
|
|
177
|
+
|
|
|
178
|
+ int dupcounter = 0;
|
|
|
179
|
+
|
|
|
180
|
+ for (int i = 0; i < newArrayLength; i++) {
|
|
|
181
|
+ if (array[i].equals(array[i+1])) {
|
|
|
182
|
+ dupcounter++;
|
|
|
183
|
+ } else {newArray[i] = array[dupcounter];}
|
|
|
184
|
+ dupcounter++;
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+ return newArray;
|
|
96
|
188
|
}
|
|
97
|
189
|
|
|
98
|
190
|
/**
|
|
|
@@ -100,8 +192,28 @@ public class StringArrayUtils {
|
|
100
|
192
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
101
|
193
|
*/ // TODO
|
|
102
|
194
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
103
|
|
- return null;
|
|
104
|
|
- }
|
|
|
195
|
+ int counter = 0;
|
|
|
196
|
+
|
|
|
197
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
198
|
+ if (array[i].equals(array[counter])) {
|
|
|
199
|
+ counter++;
|
|
|
200
|
+ }
|
|
|
201
|
+ counter++;
|
|
|
202
|
+ }
|
|
105
|
203
|
|
|
|
204
|
+ int newArrayLength = (array.length - 1) - counter;
|
|
|
205
|
+ String[] newArray = new String[newArrayLength];
|
|
106
|
206
|
|
|
|
207
|
+ int dupcounter = 0;
|
|
|
208
|
+
|
|
|
209
|
+ for (int i = 0; i < newArrayLength; i++) {
|
|
|
210
|
+ if (array[i].equals(array[dupcounter])) {
|
|
|
211
|
+ newArray[i] += array[i];
|
|
|
212
|
+ counter++;
|
|
|
213
|
+ } else {newArray[i] = array[dupcounter];}
|
|
|
214
|
+ counter++;
|
|
|
215
|
+ }
|
|
|
216
|
+
|
|
|
217
|
+ return newArray;
|
|
|
218
|
+}
|
|
107
|
219
|
}
|