Brandon Defrancis vor 6 Jahren
Ursprung
Commit
4f222886a4
1 geänderte Dateien mit 119 neuen und 12 gelöschten Zeilen
  1. 119
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 119
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java Datei anzeigen

@@ -1,15 +1,25 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import com.sun.tools.javac.util.ArrayUtils;
4
+import java.util.Arrays;
5
+import java.util.List;
6
+import java.util.ArrayList;
7
+import java.util.HashSet;
8
+import java.util.Set;
9
+
3 10
 /**
4 11
  * Created by leon on 1/29/18.
5 12
  */
6 13
 public class StringArrayUtils {
14
+    private static String[] array;
15
+
7 16
     /**
8 17
      * @param array array of String objects
9 18
      * @return first element of specified array
10 19
      */ // TODO
11 20
     public static String getFirstElement(String[] array) {
12
-        return null;
21
+
22
+        return array[0];
13 23
     }
14 24
 
15 25
     /**
@@ -17,7 +27,8 @@ public class StringArrayUtils {
17 27
      * @return second element in specified array
18 28
      */
19 29
     public static String getSecondElement(String[] array) {
20
-        return null;
30
+
31
+        return array[1];
21 32
     }
22 33
 
23 34
     /**
@@ -25,7 +36,8 @@ 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;
39
+
40
+        return array[array.length - 1];
29 41
     }
30 42
 
31 43
     /**
@@ -33,7 +45,9 @@ public class StringArrayUtils {
33 45
      * @return second to last element in specified array
34 46
      */ // TODO
35 47
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
48
+
49
+        return array[array.length - 2];
50
+
37 51
     }
38 52
 
39 53
     /**
@@ -42,6 +56,12 @@ public class StringArrayUtils {
42 56
      * @return true if the array contains the specified `value`
43 57
      */ // TODO
44 58
     public static boolean contains(String[] array, String value) {
59
+
60
+        for (String s : array) {
61
+            if (s.equals(value))
62
+                return true;
63
+
64
+        }
45 65
         return false;
46 66
     }
47 67
 
@@ -50,7 +70,15 @@ public class StringArrayUtils {
50 70
      * @return an array with identical contents in reverse order
51 71
      */ // TODO
52 72
     public static String[] reverse(String[] array) {
53
-        return null;
73
+
74
+        String temp = "";
75
+
76
+        for (int i = 0; i < array.length / 2; i++) {
77
+            temp = array[i];
78
+            array[i] = array[array.length - 1 - i];
79
+            array[array.length - 1 - i] = temp;
80
+        }
81
+        return array;
54 82
     }
55 83
 
56 84
     /**
@@ -58,7 +86,14 @@ public class StringArrayUtils {
58 86
      * @return true if the order of the array is the same backwards and forwards
59 87
      */ // TODO
60 88
     public static boolean isPalindromic(String[] array) {
61
-        return false;
89
+
90
+        for (int i = 0; i < array.length; i++) {
91
+            if (!array[i].equals(array[array.length - 1 - i])) {
92
+                return false;
93
+            }
94
+        }
95
+
96
+        return true;
62 97
     }
63 98
 
64 99
     /**
@@ -66,16 +101,50 @@ public class StringArrayUtils {
66 101
      * @return true if each letter in the alphabet has been used in the array
67 102
      */ // TODO
68 103
     public static boolean isPangramic(String[] array) {
69
-        return false;
104
+
105
+        String combo = "";
106
+
107
+        for (int i = 0; i < array.length; i++) {
108
+            String temp = array[i].toLowerCase();
109
+            String[] tempArray = temp.split(" ");
110
+            for (int j = 0; j < tempArray.length; j++) {
111
+                combo += tempArray[j];
112
+            }
113
+        }
114
+        String[] apart = combo.split("");
115
+
116
+        String[] counter = new String[26];
117
+        for (int i = 0; i < apart.length; i++) { //z-z=0 ;  z-a=25 (26-26=0  ; 26-1=25)
118
+            if (counter['z' - apart[i].charAt(0)] == null) {
119
+                counter['z' - apart[i].charAt(0)] = apart[i];
120
+            }
121
+        }
122
+        for (int i = 0; i < 26; i++) {
123
+            if (counter[i] == null) {
124
+                return false;
125
+            }
126
+        }
127
+        return true;
70 128
     }
71 129
 
130
+
72 131
     /**
73 132
      * @param array array of String objects
74 133
      * @param value value to check array for
75 134
      * @return number of occurrences the specified `value` has occurred
76 135
      */ // TODO
77 136
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
137
+
138
+        int count = 0;
139
+
140
+        for (int i = 0; i < array.length; i++) {
141
+            if (array[i] == value) {
142
+                count++;
143
+            } else {
144
+            }
145
+        }
146
+
147
+        return count;
79 148
     }
80 149
 
81 150
     /**
@@ -84,7 +153,13 @@ public class StringArrayUtils {
84 153
      * @return array with identical contents excluding values of `value`
85 154
      */ // TODO
86 155
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
156
+
157
+        List<String> list = new ArrayList<String>(Arrays.asList(array));
158
+        list.remove(valueToRemove);
159
+        array = list.toArray(new String[0]);
160
+
161
+
162
+        return array;
88 163
     }
89 164
 
90 165
     /**
@@ -92,16 +167,48 @@ public class StringArrayUtils {
92 167
      * @return array of Strings with consecutive duplicates removes
93 168
      */ // TODO
94 169
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
170
+
171
+        ArrayList<String> temp = new ArrayList<String>();
172
+
173
+        temp.add(array[0]);
174
+
175
+        for (int i = 1; i < array.length; i++) {
176
+            if (!array[i - 1].equals(array[i])) {
177
+                temp.add(array[i]);
178
+            }
179
+        }
180
+        String[] newArray = new String[temp.size()];
181
+        newArray = temp.toArray(new String[temp.size()]);
182
+
183
+
184
+        return newArray;
96 185
     }
97 186
 
187
+
98 188
     /**
99 189
      * @param array array of chars
100 190
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 191
      */ // TODO
102 192
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105 193
 
194
+        ArrayList<String> arrayList = new ArrayList<String>();
106 195
 
196
+        String str = new String();
197
+        str = array[0];
198
+        arrayList.add(array[0]);
199
+
200
+        for (int i = 1; i < array.length; i++) {
201
+            if (array[i] == str) {
202
+                arrayList.set(arrayList.size() - 1, arrayList.get(arrayList.size() - 1) + array[i]);
203
+            } else {
204
+                arrayList.add(array[i]);
205
+                str = array[i];
206
+            }
207
+        }
208
+
209
+        String[] updatedArray = new String[arrayList.size()];
210
+        updatedArray = arrayList.toArray(new String[arrayList.size()]);
211
+        return updatedArray;
212
+    }
107 213
 }
214
+