|
@@ -1,5 +1,4 @@
|
1
|
|
-
|
2
|
|
-
|
|
1
|
+import java.util.Arrays;
|
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,6 +41,11 @@ 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) {
|
|
44
|
+ for(String answer : array){
|
|
45
|
+ if(answer.equals(value)){
|
|
46
|
+ return true;
|
|
47
|
+ }
|
|
48
|
+ }
|
45
|
49
|
return false;
|
46
|
50
|
}
|
47
|
51
|
|
|
@@ -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[] output = new String[array.length];
|
|
58
|
+ int counter = 0;
|
|
59
|
+ for (int i = array.length -1; i >= 0; i--) {
|
|
60
|
+ output[counter] = array[i];
|
|
61
|
+ counter++;
|
|
62
|
+ }
|
|
63
|
+ return output;
|
54
|
64
|
}
|
55
|
65
|
|
56
|
66
|
/**
|
|
@@ -58,7 +68,14 @@ 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 palindromic = false;
|
|
72
|
+ for (int i = 0; i <= array.length/2; i++) {
|
|
73
|
+ if (array[i] == array[array.length -1 -i]) {
|
|
74
|
+ palindromic = true;
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ return palindromic;
|
62
|
79
|
}
|
63
|
80
|
|
64
|
81
|
/**
|
|
@@ -66,7 +83,25 @@ public class StringArrayUtils {
|
66
|
83
|
* @return true if each letter in the alphabet has been used in the array
|
67
|
84
|
*/ // TODO
|
68
|
85
|
public static boolean isPangramic(String[] array) {
|
69
|
|
- return false;
|
|
86
|
+ String[] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
|
|
87
|
+ "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
|
|
88
|
+ "w", "x", "y", "z" };
|
|
89
|
+
|
|
90
|
+ boolean pan = true;
|
|
91
|
+
|
|
92
|
+ for (String letter : alphabet) {
|
|
93
|
+ boolean letterFound = false;
|
|
94
|
+ for (String letter2 : array) {
|
|
95
|
+ if (letter2.toLowerCase().contains(letter)) {
|
|
96
|
+ letterFound = true;
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+ if (!letterFound) {
|
|
100
|
+ pan = false;
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ return pan;
|
70
|
105
|
}
|
71
|
106
|
|
72
|
107
|
/**
|
|
@@ -75,7 +110,11 @@ public class StringArrayUtils {
|
75
|
110
|
* @return number of occurrences the specified `value` has occurred
|
76
|
111
|
*/ // TODO
|
77
|
112
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
113
|
+ int counter = 0;
|
|
114
|
+ for(String occurence : array){
|
|
115
|
+ if(occurence.equals(value)) counter++;
|
|
116
|
+ }
|
|
117
|
+ return counter;
|
79
|
118
|
}
|
80
|
119
|
|
81
|
120
|
/**
|
|
@@ -84,7 +123,22 @@ public class StringArrayUtils {
|
84
|
123
|
* @return array with identical contents excluding values of `value`
|
85
|
124
|
*/ // TODO
|
86
|
125
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
|
126
|
+ int occurrences = getNumberOfOccurrences(array, valueToRemove);
|
|
127
|
+ String[] valueRemoved;
|
|
128
|
+ if(occurrences == 0){
|
|
129
|
+ valueRemoved = array;
|
|
130
|
+ } else {
|
|
131
|
+ valueRemoved = new String[array.length - occurrences];
|
|
132
|
+ int removedArr = 0;
|
|
133
|
+ for (int i = 0; i < array.length; i++) {
|
|
134
|
+ if (!array[i].equals(valueToRemove)) {
|
|
135
|
+ valueRemoved[removedArr] = array[i];
|
|
136
|
+ removedArr++;
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+ }
|
|
140
|
+ return valueRemoved;
|
|
141
|
+
|
88
|
142
|
}
|
89
|
143
|
|
90
|
144
|
/**
|
|
@@ -92,7 +146,28 @@ public class StringArrayUtils {
|
92
|
146
|
* @return array of Strings with consecutive duplicates removes
|
93
|
147
|
*/ // TODO
|
94
|
148
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
|
149
|
+ String lastNumber = null;
|
|
150
|
+ int index = 0;
|
|
151
|
+ int counter = 0;
|
|
152
|
+
|
|
153
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
154
|
+ if (array[i].equals(array[i + 1])) {
|
|
155
|
+ counter++;
|
|
156
|
+ }
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ String [] arr1 = new String [array.length - counter];
|
|
160
|
+
|
|
161
|
+ for (int i = 0; i < array.length; i++) {
|
|
162
|
+
|
|
163
|
+ if (!array[i].equals(lastNumber)){
|
|
164
|
+ arr1[index] = array[i];
|
|
165
|
+ lastNumber = array[i];
|
|
166
|
+ index++;
|
|
167
|
+ }
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ return arr1;
|
96
|
171
|
}
|
97
|
172
|
|
98
|
173
|
/**
|
|
@@ -100,8 +175,21 @@ public class StringArrayUtils {
|
100
|
175
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
176
|
*/ // TODO
|
102
|
177
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
|
178
|
+ String[] consecDups = new String[array.length];
|
|
179
|
+ int j=0;
|
|
180
|
+ int counter = 0;
|
|
181
|
+ consecDups[0] = array[0];
|
|
182
|
+ for (int i = 0; i < array.length-1; i++) {
|
|
183
|
+ if (array[i].compareTo(array[i+1]) == 0) {
|
|
184
|
+ consecDups[j] = consecDups[j] + array[i+1];
|
|
185
|
+ counter++;
|
|
186
|
+ } else {
|
|
187
|
+ consecDups[j+1] = array[i+1];
|
|
188
|
+ j++;
|
|
189
|
+ }
|
|
190
|
+ }
|
|
191
|
+ consecDups = Arrays.copyOf(consecDups, array.length-counter);
|
|
192
|
+ return consecDups;
|
104
|
193
|
}
|
105
|
194
|
|
106
|
|
-
|
107
|
195
|
}
|