Browse Source

String Array Utilities CRUSHED

Nathan Hall 6 years ago
parent
commit
13e9c17cd5
1 changed files with 123 additions and 12 deletions
  1. 123
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 123
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java View File

1
 package com.zipcodewilmington;
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
  * Created by leon on 1/29/18.
11
  * Created by leon on 1/29/18.
5
  */
12
  */
9
      * @return first element of specified array
16
      * @return first element of specified array
10
      */ // TODO
17
      */ // TODO
11
     public static String getFirstElement(String[] array) {
18
     public static String getFirstElement(String[] array) {
12
-        return null;
19
+
20
+
21
+        return array[0];
13
     }
22
     }
14
 
23
 
15
     /**
24
     /**
17
      * @return second element in specified array
26
      * @return second element in specified array
18
      */
27
      */
19
     public static String getSecondElement(String[] array) {
28
     public static String getSecondElement(String[] array) {
20
-        return null;
29
+        return array[1];
21
     }
30
     }
22
 
31
 
23
     /**
32
     /**
25
      * @return last element in specified array
34
      * @return last element in specified array
26
      */ // TODO
35
      */ // TODO
27
     public static String getLastElement(String[] array) {
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
      * @return second to last element in specified array
43
      * @return second to last element in specified array
34
      */ // TODO
44
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
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
      * @return true if the array contains the specified `value`
54
      * @return true if the array contains the specified `value`
43
      */ // TODO
55
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
         return false;
62
         return false;
46
     }
63
     }
47
 
64
 
50
      * @return an array with identical contents in reverse order
67
      * @return an array with identical contents in reverse order
51
      */ // TODO
68
      */ // TODO
52
     public static String[] reverse(String[] array) {
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
      * @return true if the order of the array is the same backwards and forwards
86
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
87
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
      * @return true if each letter in the alphabet has been used in the array
102
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
103
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
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
      * @return number of occurrences the specified `value` has occurred
145
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
146
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
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
      * @return array with identical contents excluding values of `value`
161
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
162
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @return array of Strings with consecutive duplicates removes
181
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
182
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
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
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
200
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
201
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
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
 }