|
|
@@ -1,5 +1,5 @@
|
|
1
|
|
-
|
|
2
|
1
|
|
|
|
2
|
+import java.util.*;
|
|
3
|
3
|
/**
|
|
4
|
4
|
* Created by leon on 1/29/18.
|
|
5
|
5
|
*/
|
|
|
@@ -9,7 +9,7 @@ public class StringArrayUtils {
|
|
9
|
9
|
* @return first element of specified array
|
|
10
|
10
|
*/ // TODO
|
|
11
|
11
|
public static String getFirstElement(String[] array) {
|
|
12
|
|
- return null;
|
|
|
12
|
+ return array[0];
|
|
13
|
13
|
}
|
|
14
|
14
|
|
|
15
|
15
|
/**
|
|
|
@@ -17,7 +17,7 @@ public class StringArrayUtils {
|
|
17
|
17
|
* @return second element in specified array
|
|
18
|
18
|
*/
|
|
19
|
19
|
public static String getSecondElement(String[] array) {
|
|
20
|
|
- return null;
|
|
|
20
|
+ return array[1];
|
|
21
|
21
|
}
|
|
22
|
22
|
|
|
23
|
23
|
/**
|
|
|
@@ -25,7 +25,7 @@ public class StringArrayUtils {
|
|
25
|
25
|
* @return last element in specified array
|
|
26
|
26
|
*/ // TODO
|
|
27
|
27
|
public static String getLastElement(String[] array) {
|
|
28
|
|
- return null;
|
|
|
28
|
+ return array[array.length - 1];
|
|
29
|
29
|
}
|
|
30
|
30
|
|
|
31
|
31
|
/**
|
|
|
@@ -33,16 +33,20 @@ public class StringArrayUtils {
|
|
33
|
33
|
* @return second to last element in specified array
|
|
34
|
34
|
*/ // TODO
|
|
35
|
35
|
public static String getSecondToLastElement(String[] array) {
|
|
36
|
|
- return null;
|
|
|
36
|
+ return array[array.length - 2];
|
|
37
|
37
|
}
|
|
38
|
38
|
|
|
39
|
|
- /**
|
|
|
39
|
+ /*
|
|
40
|
40
|
* @param array array of String objects
|
|
41
|
41
|
* @param value value to check array for
|
|
42
|
42
|
* @return true if the array contains the specified `value`
|
|
43
|
43
|
*/ // TODO
|
|
44
|
44
|
public static boolean contains(String[] array, String value) {
|
|
45
|
|
- return false;
|
|
|
45
|
+ boolean result = false;
|
|
|
46
|
+ for (String val : array) {
|
|
|
47
|
+ if (val.equals(value)) result = true;
|
|
|
48
|
+ }
|
|
|
49
|
+ return result;
|
|
46
|
50
|
}
|
|
47
|
51
|
|
|
48
|
52
|
/**
|
|
|
@@ -50,7 +54,13 @@ public class StringArrayUtils {
|
|
50
|
54
|
* @return an array with identical contents in reverse order
|
|
51
|
55
|
*/ // TODO
|
|
52
|
56
|
public static String[] reverse(String[] array) {
|
|
53
|
|
- return null;
|
|
|
57
|
+ String[] reverseArray = new String[array.length];
|
|
|
58
|
+ int j = 0;
|
|
|
59
|
+ for (int i = array.length - 1; i >= 0; i--) {
|
|
|
60
|
+ reverseArray[j] = array[i];
|
|
|
61
|
+ j++;
|
|
|
62
|
+ }
|
|
|
63
|
+ return reverseArray;
|
|
54
|
64
|
}
|
|
55
|
65
|
|
|
56
|
66
|
/**
|
|
|
@@ -58,7 +68,20 @@ public class StringArrayUtils {
|
|
58
|
68
|
* @return true if the order of the array is the same backwards and forwards
|
|
59
|
69
|
*/ // TODO
|
|
60
|
70
|
public static boolean isPalindromic(String[] array) {
|
|
61
|
|
- return false;
|
|
|
71
|
+ boolean result = true;
|
|
|
72
|
+ String[] revArr = new String[array.length];
|
|
|
73
|
+
|
|
|
74
|
+ int j = 0;
|
|
|
75
|
+ for (int i = array.length - 1; i >= 0; i--) {
|
|
|
76
|
+ revArr[j] = array[i];
|
|
|
77
|
+ j++;
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
|
80
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
81
|
+ if (!(array[i].equals(revArr[i]))) result = false;
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ return result;
|
|
62
|
85
|
}
|
|
63
|
86
|
|
|
64
|
87
|
/**
|
|
|
@@ -66,7 +89,20 @@ public class StringArrayUtils {
|
|
66
|
89
|
* @return true if each letter in the alphabet has been used in the array
|
|
67
|
90
|
*/ // TODO
|
|
68
|
91
|
public static boolean isPangramic(String[] array) {
|
|
69
|
|
- return false;
|
|
|
92
|
+ String alpha = "abcdefghijklmnopqrstuvwxyz";
|
|
|
93
|
+
|
|
|
94
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
95
|
+ String word = array[i];
|
|
|
96
|
+ for (int j = 0; j < word.length(); j++) {
|
|
|
97
|
+ String letter = Character.toString(word.charAt(j)).toLowerCase();
|
|
|
98
|
+ if (alpha.indexOf(letter) != -1) {
|
|
|
99
|
+ alpha = alpha.replace(letter, "");
|
|
|
100
|
+ }
|
|
|
101
|
+ }
|
|
|
102
|
+ }
|
|
|
103
|
+
|
|
|
104
|
+ boolean result = alpha.length() <= 0;
|
|
|
105
|
+ return result;
|
|
70
|
106
|
}
|
|
71
|
107
|
|
|
72
|
108
|
/**
|
|
|
@@ -75,7 +111,11 @@ public class StringArrayUtils {
|
|
75
|
111
|
* @return number of occurrences the specified `value` has occurred
|
|
76
|
112
|
*/ // TODO
|
|
77
|
113
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
|
78
|
|
- return 0;
|
|
|
114
|
+ int result = 0;
|
|
|
115
|
+ for (String val : array) {
|
|
|
116
|
+ if (val.equals(value)) result += 1;
|
|
|
117
|
+ }
|
|
|
118
|
+ return result;
|
|
79
|
119
|
}
|
|
80
|
120
|
|
|
81
|
121
|
/**
|
|
|
@@ -84,7 +124,22 @@ public class StringArrayUtils {
|
|
84
|
124
|
* @return array with identical contents excluding values of `value`
|
|
85
|
125
|
*/ // TODO
|
|
86
|
126
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
87
|
|
- return null;
|
|
|
127
|
+ int count = 0;
|
|
|
128
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
129
|
+ if(array[i].equals(valueToRemove)) count++;
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ String[] newArr = new String[array.length - count];
|
|
|
133
|
+
|
|
|
134
|
+ int j = 0;
|
|
|
135
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
136
|
+ if (!array[i].equals(valueToRemove)) {
|
|
|
137
|
+ newArr[j] = array[i];
|
|
|
138
|
+ j++;
|
|
|
139
|
+ }
|
|
|
140
|
+ }
|
|
|
141
|
+
|
|
|
142
|
+ return newArr;
|
|
88
|
143
|
}
|
|
89
|
144
|
|
|
90
|
145
|
/**
|
|
|
@@ -92,7 +147,23 @@ public class StringArrayUtils {
|
|
92
|
147
|
* @return array of Strings with consecutive duplicates removes
|
|
93
|
148
|
*/ // TODO
|
|
94
|
149
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
95
|
|
- return null;
|
|
|
150
|
+ int count = 0;
|
|
|
151
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
152
|
+ if (array[i].equals(array[i + 1])) count++;
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ String[] newArr = new String[array.length - count];
|
|
|
156
|
+ newArr[newArr.length - 1] = array[array.length - 1];
|
|
|
157
|
+ int j = 0;
|
|
|
158
|
+
|
|
|
159
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
160
|
+ if (!array[i].equals(array[i + 1])) {
|
|
|
161
|
+ newArr[j] = array[i];
|
|
|
162
|
+ j++;
|
|
|
163
|
+ }
|
|
|
164
|
+ }
|
|
|
165
|
+
|
|
|
166
|
+ return newArr;
|
|
96
|
167
|
}
|
|
97
|
168
|
|
|
98
|
169
|
/**
|
|
|
@@ -100,8 +171,40 @@ public class StringArrayUtils {
|
|
100
|
171
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
101
|
172
|
*/ // TODO
|
|
102
|
173
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
103
|
|
- return null;
|
|
104
|
|
- }
|
|
|
174
|
+ int count = 1;
|
|
105
|
175
|
|
|
|
176
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
177
|
+ System.out.println(!array[i].equals(array[i + 1]));
|
|
|
178
|
+ if (!array[i].equals(array[i + 1])) count++;
|
|
|
179
|
+ }
|
|
106
|
180
|
|
|
107
|
|
-}
|
|
|
181
|
+ String[] newArr = new String[count];
|
|
|
182
|
+
|
|
|
183
|
+ StringBuilder temp = new StringBuilder();
|
|
|
184
|
+ for (int i = array.length - 1; i >= 0; i--) {
|
|
|
185
|
+ if (array[i].equals(array[i - 1])) {
|
|
|
186
|
+ temp.append(array[i]);
|
|
|
187
|
+ } else {
|
|
|
188
|
+ temp.append(array[i]);
|
|
|
189
|
+ newArr[newArr.length - 1] = temp.toString();
|
|
|
190
|
+ temp = new StringBuilder();
|
|
|
191
|
+ break;
|
|
|
192
|
+ }
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ int j = 0;
|
|
|
196
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
197
|
+ if (array[i].equals(array[i + 1])) {
|
|
|
198
|
+ temp.append(array[i]);
|
|
|
199
|
+ } else {
|
|
|
200
|
+ temp.append(array[i]);
|
|
|
201
|
+ newArr[j] = temp.toString();
|
|
|
202
|
+ temp = new StringBuilder();
|
|
|
203
|
+ j++;
|
|
|
204
|
+ }
|
|
|
205
|
+ }
|
|
|
206
|
+
|
|
|
207
|
+ System.out.println(Arrays.toString(newArr));
|
|
|
208
|
+ return newArr;
|
|
|
209
|
+ }
|
|
|
210
|
+}
|