|
@@ -1,4 +1,4 @@
|
1
|
|
-
|
|
1
|
+import java.util.Arrays;
|
2
|
2
|
|
3
|
3
|
/**
|
4
|
4
|
* Created by leon on 1/29/18.
|
|
@@ -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,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,13 @@ 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
|
+ boolean output = false;
|
|
46
|
+ for (String element : array) {
|
|
47
|
+ if (element.equals(value)) {
|
|
48
|
+ output = true;
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+ return output;
|
46
|
52
|
}
|
47
|
53
|
|
48
|
54
|
/**
|
|
@@ -50,7 +56,11 @@ public class StringArrayUtils {
|
50
|
56
|
* @return an array with identical contents in reverse order
|
51
|
57
|
*/ // TODO
|
52
|
58
|
public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
59
|
+ String[] reversedArray = new String[array.length];
|
|
60
|
+ for (int i=array.length-1; i >= 0; i--) {
|
|
61
|
+ reversedArray[array.length-1-i] = array[i];
|
|
62
|
+ }
|
|
63
|
+ return reversedArray;
|
54
|
64
|
}
|
55
|
65
|
|
56
|
66
|
/**
|
|
@@ -58,7 +68,12 @@ 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 output = false;
|
|
72
|
+ String[] reversedArray = reverse(array);
|
|
73
|
+ if (Arrays.equals(array, reversedArray)) {
|
|
74
|
+ output = true;
|
|
75
|
+ }
|
|
76
|
+ return output;
|
62
|
77
|
}
|
63
|
78
|
|
64
|
79
|
/**
|
|
@@ -66,7 +81,19 @@ public class StringArrayUtils {
|
66
|
81
|
* @return true if each letter in the alphabet has been used in the array
|
67
|
82
|
*/ // TODO
|
68
|
83
|
public static boolean isPangramic(String[] array) {
|
69
|
|
- return false;
|
|
84
|
+ boolean outcome = true;
|
|
85
|
+ String[] arrayGiven = new String[1];
|
|
86
|
+ arrayGiven[0]="";
|
|
87
|
+ for (int i=0; i < array.length; i++) {
|
|
88
|
+ arrayGiven[0] = arrayGiven[0].concat(array[i]);
|
|
89
|
+ }
|
|
90
|
+ arrayGiven = arrayGiven[0].toLowerCase().split("");
|
|
91
|
+ for (char alph = 'a'; alph <= 'z'; alph++) {
|
|
92
|
+ if (contains(arrayGiven, String.valueOf(alph))==false) {
|
|
93
|
+ outcome = false;
|
|
94
|
+ }
|
|
95
|
+ }
|
|
96
|
+ return outcome;
|
70
|
97
|
}
|
71
|
98
|
|
72
|
99
|
/**
|
|
@@ -75,7 +102,13 @@ public class StringArrayUtils {
|
75
|
102
|
* @return number of occurrences the specified `value` has occurred
|
76
|
103
|
*/ // TODO
|
77
|
104
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
105
|
+ int counter = 0;
|
|
106
|
+ for (String element : array) {
|
|
107
|
+ if (element.equals(value)) {
|
|
108
|
+ counter++;
|
|
109
|
+ }
|
|
110
|
+ }
|
|
111
|
+ return counter;
|
79
|
112
|
}
|
80
|
113
|
|
81
|
114
|
/**
|
|
@@ -84,7 +117,16 @@ public class StringArrayUtils {
|
84
|
117
|
* @return array with identical contents excluding values of `value`
|
85
|
118
|
*/ // TODO
|
86
|
119
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
|
120
|
+ int numberOfElementsToDelete = getNumberOfOccurrences(array, valueToRemove);
|
|
121
|
+ String[] arrayWithRemovedValue = new String[array.length-numberOfElementsToDelete];
|
|
122
|
+ int j=0; //index of arrayWithRemovedValue
|
|
123
|
+ for (int i=0; i < array.length; i++) {
|
|
124
|
+ if (array[i].compareTo(valueToRemove) != 0) {
|
|
125
|
+ arrayWithRemovedValue[j] = array[i];
|
|
126
|
+ j++;
|
|
127
|
+ }
|
|
128
|
+ }
|
|
129
|
+ return arrayWithRemovedValue;
|
88
|
130
|
}
|
89
|
131
|
|
90
|
132
|
/**
|
|
@@ -92,7 +134,20 @@ public class StringArrayUtils {
|
92
|
134
|
* @return array of Strings with consecutive duplicates removes
|
93
|
135
|
*/ // TODO
|
94
|
136
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
|
137
|
+ int numberOfElementsToDelete = 0;
|
|
138
|
+ int j=0; //index of arrayWithRemovedVConsecutiveDuplicates
|
|
139
|
+ String[] arrayWithRemovedConsecutiveDuplicates = new String[array.length];
|
|
140
|
+ for (int i=0; i < array.length-1; i++) {
|
|
141
|
+ if (array[i].compareTo(array[i+1]) != 0) {
|
|
142
|
+ arrayWithRemovedConsecutiveDuplicates[j] = array[i];
|
|
143
|
+ j++;
|
|
144
|
+ } else {
|
|
145
|
+ numberOfElementsToDelete++;
|
|
146
|
+ }
|
|
147
|
+ }
|
|
148
|
+ arrayWithRemovedConsecutiveDuplicates = Arrays.copyOf(arrayWithRemovedConsecutiveDuplicates, array.length-numberOfElementsToDelete);
|
|
149
|
+ arrayWithRemovedConsecutiveDuplicates[arrayWithRemovedConsecutiveDuplicates.length-1] = array[array.length-1];
|
|
150
|
+ return arrayWithRemovedConsecutiveDuplicates;
|
96
|
151
|
}
|
97
|
152
|
|
98
|
153
|
/**
|
|
@@ -100,8 +155,21 @@ public class StringArrayUtils {
|
100
|
155
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
156
|
*/ // TODO
|
102
|
157
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
|
158
|
+ String[] arrayWithPackedConsecutiveDuplicates = new String[array.length];
|
|
159
|
+ int j=0; //index of arrayWithPackedVConsecutiveDuplicates
|
|
160
|
+ int numberOfElementsToDelete = 0;
|
|
161
|
+ arrayWithPackedConsecutiveDuplicates[0] = array[0];
|
|
162
|
+ for (int i=0; i < array.length-1; i++) {
|
|
163
|
+ if (array[i].compareTo(array[i+1]) == 0) {
|
|
164
|
+ arrayWithPackedConsecutiveDuplicates[j] = arrayWithPackedConsecutiveDuplicates[j] + array[i+1];
|
|
165
|
+ numberOfElementsToDelete++;
|
|
166
|
+ } else {
|
|
167
|
+ arrayWithPackedConsecutiveDuplicates[j+1] = array[i+1];
|
|
168
|
+ j++;
|
|
169
|
+ }
|
|
170
|
+ }
|
|
171
|
+ arrayWithPackedConsecutiveDuplicates = Arrays.copyOf(arrayWithPackedConsecutiveDuplicates, array.length-numberOfElementsToDelete);
|
|
172
|
+ return arrayWithPackedConsecutiveDuplicates;
|
104
|
173
|
}
|
105
|
174
|
|
106
|
|
-
|
107
|
175
|
}
|