|
|
@@ -1,5 +1,5 @@
|
|
1
|
1
|
|
|
2
|
|
-
|
|
|
2
|
+import java.util.*;
|
|
3
|
3
|
/**
|
|
4
|
4
|
* Created by leon on 1/29/18.
|
|
5
|
5
|
*/
|
|
|
@@ -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,7 @@ 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
|
+ return Arrays.asList(array).contains(value);
|
|
46
|
46
|
}
|
|
47
|
47
|
|
|
48
|
48
|
/**
|
|
|
@@ -50,7 +50,12 @@ public class StringArrayUtils {
|
|
50
|
50
|
* @return an array with identical contents in reverse order
|
|
51
|
51
|
*/ // TODO
|
|
52
|
52
|
public static String[] reverse(String[] array) {
|
|
53
|
|
- return null;
|
|
|
53
|
+ int length = array.length;
|
|
|
54
|
+ String[] result = new String[length];
|
|
|
55
|
+ for (int i = 0; i < length; i++) {
|
|
|
56
|
+ result[i] = array[(length - 1) - i];
|
|
|
57
|
+ }
|
|
|
58
|
+ return result;
|
|
54
|
59
|
}
|
|
55
|
60
|
|
|
56
|
61
|
/**
|
|
|
@@ -58,7 +63,7 @@ public class StringArrayUtils {
|
|
58
|
63
|
* @return true if the order of the array is the same backwards and forwards
|
|
59
|
64
|
*/ // TODO
|
|
60
|
65
|
public static boolean isPalindromic(String[] array) {
|
|
61
|
|
- return false;
|
|
|
66
|
+ return Arrays.equals(array, reverse(array));
|
|
62
|
67
|
}
|
|
63
|
68
|
|
|
64
|
69
|
/**
|
|
|
@@ -66,7 +71,19 @@ public class StringArrayUtils {
|
|
66
|
71
|
* @return true if each letter in the alphabet has been used in the array
|
|
67
|
72
|
*/ // TODO
|
|
68
|
73
|
public static boolean isPangramic(String[] array) {
|
|
69
|
|
- return false;
|
|
|
74
|
+ String[] alpha = {"a","b","c","d","e","f","g","h","i","j",
|
|
|
75
|
+ "k","l","m","n","o","p","q","r","s","t",
|
|
|
76
|
+ "u","v","w","x","y","z"};
|
|
|
77
|
+ String s = "";
|
|
|
78
|
+ int count = 0;
|
|
|
79
|
+ for (String e : array) {
|
|
|
80
|
+ s += e.replace(" ", "");
|
|
|
81
|
+ }
|
|
|
82
|
+ String[] check = s.toLowerCase().split("");
|
|
|
83
|
+ for (String letter : alpha) {
|
|
|
84
|
+ if (contains(check, letter)) count++;
|
|
|
85
|
+ }
|
|
|
86
|
+ return count == alpha.length;
|
|
70
|
87
|
}
|
|
71
|
88
|
|
|
72
|
89
|
/**
|
|
|
@@ -75,7 +92,11 @@ public class StringArrayUtils {
|
|
75
|
92
|
* @return number of occurrences the specified `value` has occurred
|
|
76
|
93
|
*/ // TODO
|
|
77
|
94
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
|
78
|
|
- return 0;
|
|
|
95
|
+ int count = 0;
|
|
|
96
|
+ for (String i : array) {
|
|
|
97
|
+ count += (i == value) ? 1 : 0;
|
|
|
98
|
+ }
|
|
|
99
|
+ return count;
|
|
79
|
100
|
}
|
|
80
|
101
|
|
|
81
|
102
|
/**
|
|
|
@@ -84,15 +105,24 @@ public class StringArrayUtils {
|
|
84
|
105
|
* @return array with identical contents excluding values of `value`
|
|
85
|
106
|
*/ // TODO
|
|
86
|
107
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
87
|
|
- return null;
|
|
|
108
|
+ ArrayList<String> result = new ArrayList<>();
|
|
|
109
|
+ for (String s : array) {
|
|
|
110
|
+ if (!s.equals(valueToRemove)) result.add(s);
|
|
|
111
|
+ }
|
|
|
112
|
+ return result.toArray(new String[result.size()]);
|
|
88
|
113
|
}
|
|
89
|
114
|
|
|
90
|
115
|
/**
|
|
91
|
116
|
* @param array array of chars
|
|
92
|
117
|
* @return array of Strings with consecutive duplicates removes
|
|
93
|
118
|
*/ // TODO
|
|
94
|
|
- public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
95
|
|
- return null;
|
|
|
119
|
+ public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
|
120
|
+ ArrayList<String> result = new ArrayList<>();
|
|
|
121
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
122
|
+ if (!array[i].equals(array[i + 1])) result.add(array[i]);
|
|
|
123
|
+ }
|
|
|
124
|
+ result.add(array[array.length - 1]);
|
|
|
125
|
+ return result.toArray(new String[result.size()]);
|
|
96
|
126
|
}
|
|
97
|
127
|
|
|
98
|
128
|
/**
|
|
|
@@ -100,7 +130,21 @@ public class StringArrayUtils {
|
|
100
|
130
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
101
|
131
|
*/ // TODO
|
|
102
|
132
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
103
|
|
- return null;
|
|
|
133
|
+ ArrayList<String> result = new ArrayList<>();
|
|
|
134
|
+ StringBuilder addMe = new StringBuilder();
|
|
|
135
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
136
|
+ if (!array[i].equals(array[i + 1])) {
|
|
|
137
|
+ addMe.append(array[i]);
|
|
|
138
|
+ result.add(addMe.toString());
|
|
|
139
|
+ addMe = new StringBuilder();
|
|
|
140
|
+ }
|
|
|
141
|
+ else {
|
|
|
142
|
+ addMe.append(array[i]);
|
|
|
143
|
+ }
|
|
|
144
|
+ }
|
|
|
145
|
+ addMe.append(array[array.length - 1]);
|
|
|
146
|
+ result.add(addMe.toString());
|
|
|
147
|
+ return result.toArray(new String[result.size()]);
|
|
104
|
148
|
}
|
|
105
|
149
|
|
|
106
|
150
|
|