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