William Simkins 8 vuotta sitten
vanhempi
commit
7741e5a421
1 muutettua tiedostoa jossa 102 lisäystä ja 14 poistoa
  1. 102
    14
      StringArrayUtils.java

+ 102
- 14
StringArrayUtils.java Näytä tiedosto

1
- 
2
-
1
+import java.util.Arrays;
3
 /**
2
 /**
4
  * Created by leon on 1/29/18.
3
  * Created by leon on 1/29/18.
5
  */
4
  */
9
      * @return first element of specified array
8
      * @return first element of specified array
10
      */ // TODO
9
      */ // TODO
11
     public static String getFirstElement(String[] array) {
10
     public static String getFirstElement(String[] array) {
12
-        return null;
11
+        return array[0];
13
     }
12
     }
14
 
13
 
15
     /**
14
     /**
17
      * @return second element in specified array
16
      * @return second element in specified array
18
      */
17
      */
19
     public static String getSecondElement(String[] array) {
18
     public static String getSecondElement(String[] array) {
20
-        return null;
19
+        return array[1];
21
     }
20
     }
22
 
21
 
23
     /**
22
     /**
25
      * @return last element in specified array
24
      * @return last element in specified array
26
      */ // TODO
25
      */ // TODO
27
     public static String getLastElement(String[] array) {
26
     public static String getLastElement(String[] array) {
28
-        return null;
27
+        return array[array.length-1];
29
     }
28
     }
30
 
29
 
31
     /**
30
     /**
33
      * @return second to last element in specified array
32
      * @return second to last element in specified array
34
      */ // TODO
33
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
34
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
35
+        return array[array.length-2];
37
     }
36
     }
38
 
37
 
39
     /**
38
     /**
42
      * @return true if the array contains the specified `value`
41
      * @return true if the array contains the specified `value`
43
      */ // TODO
42
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
         return false;
49
         return false;
46
     }
50
     }
47
 
51
 
50
      * @return an array with identical contents in reverse order
54
      * @return an array with identical contents in reverse order
51
      */ // TODO
55
      */ // TODO
52
     public static String[] reverse(String[] array) {
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
      * @return true if the order of the array is the same backwards and forwards
68
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
69
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
      * @return true if each letter in the alphabet has been used in the array
83
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
84
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
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
      * @return number of occurrences the specified `value` has occurred
110
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
111
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
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
      * @return array with identical contents excluding values of `value`
123
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
124
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @return array of Strings with consecutive duplicates removes
146
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
147
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
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
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
175
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
176
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
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
 }