|
@@ -1,5 +1,9 @@
|
1
|
1
|
package com.zipcodewilmington;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+import java.util.Hashtable;
|
|
5
|
+import java.util.Map;
|
|
6
|
+
|
3
|
7
|
/**
|
4
|
8
|
* Created by leon on 1/29/18.
|
5
|
9
|
*/
|
|
@@ -9,7 +13,7 @@ public class StringArrayUtils {
|
9
|
13
|
* @return first element of specified array
|
10
|
14
|
*/ // TODO
|
11
|
15
|
public static String getFirstElement(String[] array) {
|
12
|
|
- return null;
|
|
16
|
+ return array[0];
|
13
|
17
|
}
|
14
|
18
|
|
15
|
19
|
/**
|
|
@@ -17,7 +21,7 @@ public class StringArrayUtils {
|
17
|
21
|
* @return second element in specified array
|
18
|
22
|
*/
|
19
|
23
|
public static String getSecondElement(String[] array) {
|
20
|
|
- return null;
|
|
24
|
+ return array[1];
|
21
|
25
|
}
|
22
|
26
|
|
23
|
27
|
/**
|
|
@@ -25,7 +29,8 @@ public class StringArrayUtils {
|
25
|
29
|
* @return last element in specified array
|
26
|
30
|
*/ // TODO
|
27
|
31
|
public static String getLastElement(String[] array) {
|
28
|
|
- return null;
|
|
32
|
+ String lastElement = array[array.length - 1];
|
|
33
|
+ return lastElement;
|
29
|
34
|
}
|
30
|
35
|
|
31
|
36
|
/**
|
|
@@ -33,7 +38,8 @@ public class StringArrayUtils {
|
33
|
38
|
* @return second to last element in specified array
|
34
|
39
|
*/ // TODO
|
35
|
40
|
public static String getSecondToLastElement(String[] array) {
|
36
|
|
- return null;
|
|
41
|
+ String getSecondToLastElement = array[array.length - 2];
|
|
42
|
+ return getSecondToLastElement;
|
37
|
43
|
}
|
38
|
44
|
|
39
|
45
|
/**
|
|
@@ -42,7 +48,16 @@ public class StringArrayUtils {
|
42
|
48
|
* @return true if the array contains the specified `value`
|
43
|
49
|
*/ // TODO
|
44
|
50
|
public static boolean contains(String[] array, String value) {
|
|
51
|
+ for (String a : array) {
|
|
52
|
+
|
|
53
|
+ if (a == value) {
|
|
54
|
+ return true;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ }
|
|
58
|
+
|
45
|
59
|
return false;
|
|
60
|
+
|
46
|
61
|
}
|
47
|
62
|
|
48
|
63
|
/**
|
|
@@ -50,7 +65,15 @@ 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
|
+ String[] test = new String[array.length];
|
|
69
|
+
|
|
70
|
+ int j = 0;
|
|
71
|
+
|
|
72
|
+ for (int i = array.length - 1; i >= 0; i--) {
|
|
73
|
+ test[j] = array[i];
|
|
74
|
+ j++;
|
|
75
|
+ }
|
|
76
|
+ return test;
|
54
|
77
|
}
|
55
|
78
|
|
56
|
79
|
/**
|
|
@@ -58,6 +81,13 @@ public class StringArrayUtils {
|
58
|
81
|
* @return true if the order of the array is the same backwards and forwards
|
59
|
82
|
*/ // TODO
|
60
|
83
|
public static boolean isPalindromic(String[] array) {
|
|
84
|
+
|
|
85
|
+ for ( int i = 0; i < array.length; i++) {
|
|
86
|
+ if (array[i].equals(array[array.length-1-i])){
|
|
87
|
+ return true;
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ }
|
61
|
91
|
return false;
|
62
|
92
|
}
|
63
|
93
|
|
|
@@ -66,7 +96,26 @@ 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) {
|
|
99
|
+
|
|
100
|
+ int count = 0;
|
|
101
|
+ String str = String.join(" ", array).toLowerCase();
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+ String [] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
|
|
105
|
+
|
|
106
|
+ for (int i = 0; i < alphabet.length; i++) {
|
|
107
|
+ if(str.contains(alphabet[i])){
|
|
108
|
+ count++;
|
|
109
|
+ }
|
|
110
|
+ }
|
|
111
|
+ System.out.println(count);
|
|
112
|
+ if (count >= 26) {
|
|
113
|
+
|
|
114
|
+ return true;
|
|
115
|
+ }
|
69
|
116
|
return false;
|
|
117
|
+
|
|
118
|
+
|
70
|
119
|
}
|
71
|
120
|
|
72
|
121
|
/**
|
|
@@ -75,7 +124,15 @@ public class StringArrayUtils {
|
75
|
124
|
* @return number of occurrences the specified `value` has occurred
|
76
|
125
|
*/ // TODO
|
77
|
126
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
127
|
+ int counter = 0;
|
|
128
|
+
|
|
129
|
+ for (int i = 0; i < array.length; i++) {
|
|
130
|
+
|
|
131
|
+ if (array[i] == value) {
|
|
132
|
+ counter++;
|
|
133
|
+ }
|
|
134
|
+ }
|
|
135
|
+ return counter;
|
79
|
136
|
}
|
80
|
137
|
|
81
|
138
|
/**
|
|
@@ -84,7 +141,23 @@ public class StringArrayUtils {
|
84
|
141
|
* @return array with identical contents excluding values of `value`
|
85
|
142
|
*/ // TODO
|
86
|
143
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
|
144
|
+ int x = getNumberOfOccurrences(array, valueToRemove);
|
|
145
|
+
|
|
146
|
+ String[] array2 = new String[array.length - x];
|
|
147
|
+
|
|
148
|
+ int counter = 0;
|
|
149
|
+
|
|
150
|
+ for (int i = 0; i < array.length; i++) {
|
|
151
|
+ if (array [i] != valueToRemove) {
|
|
152
|
+ array2 [counter] = array [i];
|
|
153
|
+ counter++;
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+ }
|
|
160
|
+ return array2;
|
88
|
161
|
}
|
89
|
162
|
|
90
|
163
|
/**
|
|
@@ -92,7 +165,20 @@ 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
|
+ String [] str1 = new String[array.length];
|
|
169
|
+ String lastValue = "";
|
|
170
|
+ int counter = 0;
|
|
171
|
+
|
|
172
|
+ for(int i = 0; i < array.length; i++) {
|
|
173
|
+
|
|
174
|
+ if (!array[i].equals(lastValue)) {
|
|
175
|
+ lastValue = array[i];
|
|
176
|
+ str1[counter] = array[i];
|
|
177
|
+ counter++;
|
|
178
|
+ }
|
|
179
|
+ }
|
|
180
|
+ String [] str2 = Arrays.copyOf(str1, counter);
|
|
181
|
+ return str2;
|
96
|
182
|
}
|
97
|
183
|
|
98
|
184
|
/**
|
|
@@ -100,7 +186,29 @@ public class StringArrayUtils {
|
100
|
186
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
187
|
*/ // TODO
|
102
|
188
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
|
189
|
+ String [] str1 = new String[array.length];
|
|
190
|
+ String lastVal = array[0];
|
|
191
|
+ String running = array[0];
|
|
192
|
+ int counter = 0;
|
|
193
|
+
|
|
194
|
+ for (int i = 1; i < array.length; i++) {
|
|
195
|
+ if(!array[i].equals(lastVal)) {
|
|
196
|
+ lastVal = array[i];
|
|
197
|
+ str1[counter] = running;
|
|
198
|
+ running = array[i];
|
|
199
|
+ counter++;
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+ }else {
|
|
203
|
+ running = running + array[i];
|
|
204
|
+ }
|
|
205
|
+ }
|
|
206
|
+ str1[counter] = running;
|
|
207
|
+
|
|
208
|
+ String [] str2 = Arrays.copyOf(str1, counter+1 );
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+ return str2;
|
104
|
212
|
}
|
105
|
213
|
|
106
|
214
|
|