|
@@ -1,15 +1,15 @@
|
1
|
|
-
|
2
|
1
|
|
3
|
2
|
/**
|
4
|
3
|
* Created by leon on 1/29/18.
|
5
|
4
|
*/
|
|
5
|
+import java.util.Arrays;
|
6
|
6
|
public class StringArrayUtils {
|
7
|
7
|
/**
|
8
|
8
|
* @param array array of String objects
|
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,7 +33,7 @@ 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
|
/**
|
|
@@ -42,7 +42,15 @@ public class StringArrayUtils {
|
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
|
+
|
|
46
|
+ boolean answer = false;
|
|
47
|
+ for(String arr: array) {
|
|
48
|
+ if (arr.equals(value)){
|
|
49
|
+ answer = true;
|
|
50
|
+ break;
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+ return answer;
|
46
|
54
|
}
|
47
|
55
|
|
48
|
56
|
/**
|
|
@@ -50,7 +58,15 @@ public class StringArrayUtils {
|
50
|
58
|
* @return an array with identical contents in reverse order
|
51
|
59
|
*/ // TODO
|
52
|
60
|
public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
61
|
+ String[] answer = new String[array.length];
|
|
62
|
+ int count = 0;
|
|
63
|
+
|
|
64
|
+ for(int i = array.length - 1; i >= 0; i--){
|
|
65
|
+ answer[count] = array[i];
|
|
66
|
+ count++;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ return answer;
|
54
|
70
|
}
|
55
|
71
|
|
56
|
72
|
/**
|
|
@@ -58,7 +74,14 @@ 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 answer = false;
|
|
78
|
+ String[] flipped = reverse(array);
|
|
79
|
+
|
|
80
|
+ if( Arrays.equals(array, flipped)){
|
|
81
|
+
|
|
82
|
+ answer = true;}
|
|
83
|
+
|
|
84
|
+ return answer;
|
62
|
85
|
}
|
63
|
86
|
|
64
|
87
|
/**
|
|
@@ -66,7 +89,28 @@ 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 = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
|
|
93
|
+ String bigString = "";
|
|
94
|
+
|
|
95
|
+ boolean result = true;
|
|
96
|
+ //could be seperate method makeString(String[] arr)
|
|
97
|
+ for(int i = 0; i<array.length;i++){
|
|
98
|
+ bigString = bigString + array[i];
|
|
99
|
+ }
|
|
100
|
+ // could be seperate method backToArray(String string)
|
|
101
|
+ String[] arrBig = new String[bigString.length()];
|
|
102
|
+ for(int j = 0; j < arrBig.length; j++){
|
|
103
|
+ arrBig[j]= (bigString.charAt(j)+"").toLowerCase();
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ for(int i = 0; i < alpha.length; i++){
|
|
107
|
+ if(!contains(arrBig,alpha[i])){
|
|
108
|
+ result = false;
|
|
109
|
+ break;
|
|
110
|
+ }
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ return result;
|
70
|
114
|
}
|
71
|
115
|
|
72
|
116
|
/**
|
|
@@ -75,7 +119,14 @@ public class StringArrayUtils {
|
75
|
119
|
* @return number of occurrences the specified `value` has occurred
|
76
|
120
|
*/ // TODO
|
77
|
121
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
122
|
+ int count = 0;
|
|
123
|
+ for(int i = 0; i < array.length; i++){
|
|
124
|
+ if(array[i].contains(value)){
|
|
125
|
+ count++;
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ return count;
|
79
|
130
|
}
|
80
|
131
|
|
81
|
132
|
/**
|
|
@@ -84,7 +135,21 @@ 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
|
+ //iterate over an array of strings
|
|
139
|
+ // if array[i] !contains valueToRemove, add to new string
|
|
140
|
+ // convert string to array
|
|
141
|
+ // return new array
|
|
142
|
+ String saveMe = "";
|
|
143
|
+
|
|
144
|
+ for(int i = 0; i < array.length; i++){
|
|
145
|
+ if(array[i] != valueToRemove){
|
|
146
|
+ saveMe = saveMe + array[i] + " ";
|
|
147
|
+ }
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ String[] answer = saveMe.split(" ", 0);
|
|
151
|
+
|
|
152
|
+ return answer;
|
88
|
153
|
}
|
89
|
154
|
|
90
|
155
|
/**
|
|
@@ -92,7 +157,23 @@ public class StringArrayUtils {
|
92
|
157
|
* @return array of Strings with consecutive duplicates removes
|
93
|
158
|
*/ // TODO
|
94
|
159
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
|
160
|
+ //create string to save needed values
|
|
161
|
+ int count = 0;
|
|
162
|
+ String saveMe = "";
|
|
163
|
+ //loop array and compare array[i] and array[i+1]
|
|
164
|
+ for(int i = 0; i < array.length; i++){
|
|
165
|
+
|
|
166
|
+ if(count > 1 && array[i] == array[i+1]){
|
|
167
|
+ saveMe = saveMe;
|
|
168
|
+ } else if(count == 0 && array[i] == array[i + 1] && i < array.length - 2){
|
|
169
|
+ count ++;
|
|
170
|
+ saveMe = saveMe + array[i] + " ";
|
|
171
|
+ }else if(array[i] != array[i -1]){
|
|
172
|
+ saveMe = saveMe + array[i] + " ";
|
|
173
|
+ }
|
|
174
|
+ }
|
|
175
|
+ String[] answer = saveMe.split(" ");
|
|
176
|
+ return answer;
|
96
|
177
|
}
|
97
|
178
|
|
98
|
179
|
/**
|
|
@@ -100,8 +181,14 @@ public class StringArrayUtils {
|
100
|
181
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
182
|
*/ // TODO
|
102
|
183
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
104
|
|
- }
|
|
184
|
+ String saveMe = "";
|
105
|
185
|
|
|
186
|
+ for(int i = 0; i < array.length; i++){
|
|
187
|
+ saveMe = saveMe +array[i];
|
|
188
|
+ }
|
|
189
|
+ String[]answer = saveMe.split("(?<=(.))(?!\\1)");
|
|
190
|
+
|
|
191
|
+ return answer;
|
|
192
|
+ }
|
106
|
193
|
|
107
|
194
|
}
|