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