|
@@ -1,5 +1,11 @@
|
1
|
1
|
package com.zipcodewilmington;
|
2
|
2
|
|
|
3
|
+import com.google.common.primitives.Ints;
|
|
4
|
+
|
|
5
|
+import java.util.ArrayList;
|
|
6
|
+import java.util.Arrays;
|
|
7
|
+import java.util.List;
|
|
8
|
+
|
3
|
9
|
/**
|
4
|
10
|
* Created by leon on 1/29/18.
|
5
|
11
|
*/
|
|
@@ -9,7 +15,7 @@ public class StringArrayUtils {
|
9
|
15
|
* @return first element of specified array
|
10
|
16
|
*/ // TODO
|
11
|
17
|
public static String getFirstElement(String[] array) {
|
12
|
|
- return null;
|
|
18
|
+ return array[0];
|
13
|
19
|
}
|
14
|
20
|
|
15
|
21
|
/**
|
|
@@ -17,7 +23,7 @@ public class StringArrayUtils {
|
17
|
23
|
* @return second element in specified array
|
18
|
24
|
*/
|
19
|
25
|
public static String getSecondElement(String[] array) {
|
20
|
|
- return null;
|
|
26
|
+ return array[1];
|
21
|
27
|
}
|
22
|
28
|
|
23
|
29
|
/**
|
|
@@ -25,7 +31,7 @@ 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
|
+ return array[array.length-1];
|
29
|
35
|
}
|
30
|
36
|
|
31
|
37
|
/**
|
|
@@ -33,7 +39,7 @@ public class StringArrayUtils {
|
33
|
39
|
* @return second to last element in specified array
|
34
|
40
|
*/ // TODO
|
35
|
41
|
public static String getSecondToLastElement(String[] array) {
|
36
|
|
- return null;
|
|
42
|
+ return array[array.length-2];
|
37
|
43
|
}
|
38
|
44
|
|
39
|
45
|
/**
|
|
@@ -42,7 +48,10 @@ 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) {
|
45
|
|
- return false;
|
|
51
|
+ boolean doesItContain = false;
|
|
52
|
+ for (String values : array)
|
|
53
|
+ if (values.contains(value)) doesItContain = true;
|
|
54
|
+ return doesItContain;
|
46
|
55
|
}
|
47
|
56
|
|
48
|
57
|
/**
|
|
@@ -50,7 +59,12 @@ public class StringArrayUtils {
|
50
|
59
|
* @return an array with identical contents in reverse order
|
51
|
60
|
*/ // TODO
|
52
|
61
|
public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
62
|
+ for (int i = 0; i < array.length / 2; i++) {
|
|
63
|
+ String temp = array[i];
|
|
64
|
+ array[i] = array[array.length - i - 1];
|
|
65
|
+ array[array.length - i - 1] = temp;
|
|
66
|
+ }
|
|
67
|
+ return array;
|
54
|
68
|
}
|
55
|
69
|
|
56
|
70
|
/**
|
|
@@ -58,7 +72,12 @@ public class StringArrayUtils {
|
58
|
72
|
* @return true if the order of the array is the same backwards and forwards
|
59
|
73
|
*/ // TODO
|
60
|
74
|
public static boolean isPalindromic(String[] array) {
|
61
|
|
- return false;
|
|
75
|
+ for (int i = 0; i < array.length / 2; i++) {
|
|
76
|
+ if (!array[i].equals(array[array.length - i - 1])) {
|
|
77
|
+ return false;
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+ return true;
|
62
|
81
|
}
|
63
|
82
|
|
64
|
83
|
/**
|
|
@@ -66,7 +85,20 @@ public class StringArrayUtils {
|
66
|
85
|
* @return true if each letter in the alphabet has been used in the array
|
67
|
86
|
*/ // TODO
|
68
|
87
|
public static boolean isPangramic(String[] array) {
|
69
|
|
- return false;
|
|
88
|
+ String abcs = "abcdefghijklmnopqrstuvwxyz";
|
|
89
|
+ for (int i = 0; i < abcs.length(); i++) {
|
|
90
|
+ int falseCount = 0;
|
|
91
|
+ for (int j = 0; j < array.length; j++) {
|
|
92
|
+ String lowerCase = array[j].toLowerCase();
|
|
93
|
+ if (lowerCase.indexOf(abcs.charAt(i)) == -1) {
|
|
94
|
+ falseCount++;
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+ if (falseCount == array.length) {
|
|
98
|
+ return false;
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+ return true;
|
70
|
102
|
}
|
71
|
103
|
|
72
|
104
|
/**
|
|
@@ -75,7 +107,11 @@ public class StringArrayUtils {
|
75
|
107
|
* @return number of occurrences the specified `value` has occurred
|
76
|
108
|
*/ // TODO
|
77
|
109
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
110
|
+ int counter = 0;
|
|
111
|
+ for (int i = 0; i < array.length; i++) {
|
|
112
|
+ if (array[i].equals(value)) counter++;
|
|
113
|
+ }
|
|
114
|
+ return counter;
|
79
|
115
|
}
|
80
|
116
|
|
81
|
117
|
/**
|
|
@@ -84,7 +120,14 @@ public class StringArrayUtils {
|
84
|
120
|
* @return array with identical contents excluding values of `value`
|
85
|
121
|
*/ // TODO
|
86
|
122
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
|
123
|
+ ArrayList<String> temp = new ArrayList<String>();
|
|
124
|
+ for (int i = 0; i < array.length; i++) {
|
|
125
|
+ if (!array[i].equals(valueToRemove)) {
|
|
126
|
+ temp.add(array[i]);
|
|
127
|
+ }
|
|
128
|
+ }
|
|
129
|
+ String[] newArray = temp.toArray(new String[temp.size()]);
|
|
130
|
+ return newArray;
|
88
|
131
|
}
|
89
|
132
|
|
90
|
133
|
/**
|
|
@@ -92,15 +135,52 @@ public class StringArrayUtils {
|
92
|
135
|
* @return array of Strings with consecutive duplicates removes
|
93
|
136
|
*/ // TODO
|
94
|
137
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
|
138
|
+ ArrayList<String> temp = new ArrayList<String>();
|
|
139
|
+ temp.add(array[0]);
|
|
140
|
+
|
|
141
|
+ for (int i = 1; i < array.length; i++) {
|
|
142
|
+ if (!array[i].equals(temp.get(temp.size() - 1))) {
|
|
143
|
+ temp.add(array[i]);
|
|
144
|
+ }
|
|
145
|
+ }
|
|
146
|
+ String[] newArray = temp.toArray(new String[temp.size()]);
|
|
147
|
+// for (int i = 0; i < newArray.length; i++) {
|
|
148
|
+// System.out.println(newArray[i]);
|
|
149
|
+// }
|
|
150
|
+ return newArray;
|
96
|
151
|
}
|
97
|
152
|
|
|
153
|
+// public static void main(String[] args) {
|
|
154
|
+// String[] arrayTest = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
155
|
+// removeConsecutiveDuplicates(arrayTest);
|
|
156
|
+// }
|
|
157
|
+
|
98
|
158
|
/**
|
99
|
159
|
* @param array array of chars
|
100
|
160
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
161
|
*/ // TODO
|
102
|
162
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
|
163
|
+
|
|
164
|
+ ArrayList<String> temp = new ArrayList<String>();
|
|
165
|
+ temp.add(array[0]);
|
|
166
|
+ int tempPos = 0;
|
|
167
|
+// String tempString = "";
|
|
168
|
+
|
|
169
|
+ for (int i = 1; i < array.length; i++) {
|
|
170
|
+ if (array[i].charAt(0) == temp.get(tempPos).charAt(0)) {
|
|
171
|
+// temp.set(tempPos, String.format(temp.get(tempPos) += array[i]));
|
|
172
|
+ String s = temp.get(tempPos);
|
|
173
|
+ s = s.concat(array[i]);
|
|
174
|
+ temp.set(tempPos, s);
|
|
175
|
+ } else {
|
|
176
|
+ tempPos++;
|
|
177
|
+ temp.add(tempPos, array[i]);
|
|
178
|
+ }
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ String[] newArray = temp.toArray(new String[temp.size()]);
|
|
182
|
+
|
|
183
|
+ return newArray;
|
104
|
184
|
}
|
105
|
185
|
|
106
|
186
|
|