Przeglądaj źródła

String Array Utilities CRUSHED

Nathan Hall 6 lat temu
rodzic
commit
13e9c17cd5
1 zmienionych plików z 123 dodań i 12 usunięć
  1. 123
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 123
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java Wyświetl plik

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