Seth il y a 8 ans
Parent
révision
84d5ebcbde
1 fichiers modifiés avec 66 ajouts et 40 suppressions
  1. 66
    40
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 66
- 40
src/main/java/com/zipcodewilmington/StringArrayUtils.java Voir le fichier

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
 
3
 
4
+import java.lang.reflect.Array;
4
 import java.util.Arrays;
5
 import java.util.Arrays;
5
 
6
 
6
 /**
7
 /**
49
      * @return true if the array contains the specified `value`
50
      * @return true if the array contains the specified `value`
50
      */ // TODO
51
      */ // TODO
51
     public static boolean contains(String[] array, String value) {
52
     public static boolean contains(String[] array, String value) {
52
-        for(String counter : array) {
53
-            if(counter.equals(value)) {
53
+        for (String counter : array) {
54
+            if (counter.equals(value)) {
54
                 return true;
55
                 return true;
55
             }
56
             }
56
         }
57
         }
65
     public static String[] reverse(String[] array) {
66
     public static String[] reverse(String[] array) {
66
         String[] reverseArray = new String[array.length];
67
         String[] reverseArray = new String[array.length];
67
         int b = 1;
68
         int b = 1;
68
-        for(int j = 0; j < reverseArray.length; j++){
69
+        for (int j = 0; j < reverseArray.length; j++) {
69
             reverseArray[j] = array[array.length - b];
70
             reverseArray[j] = array[array.length - b];
70
             b++;
71
             b++;
71
-            }
72
+        }
72
 
73
 
73
 
74
 
74
-            return reverseArray;
75
+        return reverseArray;
75
     }
76
     }
76
 
77
 
77
     /**
78
     /**
79
      * @return true if the order of the array is the same backwards and forwards
80
      * @return true if the order of the array is the same backwards and forwards
80
      */ // TODO
81
      */ // TODO
81
     public static boolean isPalindromic(String[] array) {
82
     public static boolean isPalindromic(String[] array) {
82
-        String[] reverseArray = new String[array.length];
83
-        int b = 1;
84
-        for(int j = 0; j < reverseArray.length; j++){
85
-            reverseArray[j] = array[array.length - b];
86
-            b++;
87
-        }
83
+        String[] testArray = reverse(Arrays.copyOf(array, array.length));
88
 
84
 
89
-        return reverseArray == array ? false: true;
85
+        return Arrays.equals(array, testArray);
90
     }
86
     }
91
 
87
 
92
     /**
88
     /**
94
      * @return true if each letter in the alphabet has been used in the array
90
      * @return true if each letter in the alphabet has been used in the array
95
      */ // TODO
91
      */ // TODO
96
     public static boolean isPangramic(String[] array) {
92
     public static boolean isPangramic(String[] array) {
97
-            String alphaString = array.toString().toLowerCase();
98
-            for(char l = 'a'; l <= 'z'; l++) {
99
-                if (!alphaString.contains(String.valueOf(l))) {
100
-                    return false;
101
-                }
93
+        String alphaString = Arrays.toString(array).toLowerCase();
94
+        for (char l = 'a'; l <= 'z'; l++) {
95
+            if (!alphaString.contains(String.valueOf(l))) {
96
+                return false;
102
             }
97
             }
103
-              return true;
98
+        }
99
+        return true;
104
     }
100
     }
105
 
101
 
106
     /**
102
     /**
110
      */ // TODO
106
      */ // TODO
111
     public static int getNumberOfOccurrences(String[] array, String value) {
107
     public static int getNumberOfOccurrences(String[] array, String value) {
112
         int counter = 0;
108
         int counter = 0;
113
-        for(String looper : array) {
109
+        for (String looper : array) {
114
             if (looper.equals(value)) {
110
             if (looper.equals(value)) {
115
                 counter++;
111
                 counter++;
116
             }
112
             }
123
      * @param valueToRemove value to remove from array
119
      * @param valueToRemove value to remove from array
124
      * @return array with identical contents excluding values of `value`
120
      * @return array with identical contents excluding values of `value`
125
      */ // TODO
121
      */ // TODO
126
-
127
-        public static String[] removeValue(String[] array, String valueToRemove) {
128
-            String[] tempArray = new String[array.length];
129
-            int newIdx = 0;
130
-            for(int i = 0; i < array.length; i++){
131
-                if(array[i] != valueToRemove) {
132
-                    tempArray[newIdx] = array[i];
133
-                    newIdx++;
134
-                }
135
-
122
+    public static String[] removeValue(String[] array, String valueToRemove) {
123
+        String[] tempArray = new String[array.length];
124
+        int newIdx = 0;
125
+        for (int i = 0; i < array.length; i++) {
126
+            if (array[i] != valueToRemove) {
127
+                tempArray[newIdx] = array[i];
128
+                newIdx++;
136
             }
129
             }
137
-            return Arrays.copyOf(tempArray, newIdx);
138
 
130
 
139
         }
131
         }
132
+        return Arrays.copyOf(tempArray, newIdx);
133
+
134
+    }
140
 
135
 
141
     /**
136
     /**
142
      * @param array array of chars
137
      * @param array array of chars
146
         String[] tempArray = new String[array.length];
141
         String[] tempArray = new String[array.length];
147
         int newIdx = 0;
142
         int newIdx = 0;
148
 
143
 
149
-        for(int i = 1; i < array.length; i++){
144
+        for (int i = 1; i < array.length; i++) {
150
             String current = array[i - 1];
145
             String current = array[i - 1];
151
-            if(!current.equals(array[i])){
146
+            if (!current.equals(array[i])) {
152
                 tempArray[newIdx] = array[i - 1];
147
                 tempArray[newIdx] = array[i - 1];
153
-              newIdx++;
148
+                newIdx++;
154
             }
149
             }
155
         }
150
         }
156
         tempArray[newIdx] = array[array.length - 1];
151
         tempArray[newIdx] = array[array.length - 1];
159
     }
154
     }
160
 
155
 
161
 
156
 
162
-
163
     /**
157
     /**
164
      * @param array array of chars
158
      * @param array array of chars
165
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
159
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
167
     public static String[] packConsecutiveDuplicates(String[] array) {
161
     public static String[] packConsecutiveDuplicates(String[] array) {
168
         String[] tempArray = new String[array.length];
162
         String[] tempArray = new String[array.length];
169
         int newIdx = 0;
163
         int newIdx = 0;
164
+        String last = array[0];
165
+        String combiner = array[0];
166
+
167
+        for (int i = 1; i < array.length; i++) {
168
+            if (!last.equals(array[i])) {
169
+                last = array[i];
170
+                tempArray[newIdx] = combiner;
171
+                combiner = array[i];
172
+                newIdx++;
173
+            } else {
174
+                combiner += array[i];
175
+
176
+            }
177
+
178
+        }
179
+        tempArray[newIdx] = combiner;
180
+        //tempArray[newIdx] = array[array.length - 1];
181
+        return Arrays.copyOf(tempArray, newIdx + 1);
182
+
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+        /*String[] tempArray = new String[array.length];
195
+        int newIdx = 0;
170
         String last  = array[0];
196
         String last  = array[0];
171
         String next = "";
197
         String next = "";
172
         String set = "";
198
         String set = "";
174
              next = array[i+1];
200
              next = array[i+1];
175
             if(next.equals(last)){
201
             if(next.equals(last)){
176
                 set += last;
202
                 set += last;
203
+                last = array[i+1];
177
 
204
 
178
             } else {
205
             } else {
179
                 tempArray[newIdx] = set;
206
                 tempArray[newIdx] = set;
180
                 set ="";
207
                 set ="";
181
                 newIdx++;
208
                 newIdx++;
182
-            }
183
-
184
-        }
185
-
186
-         return Arrays.copyOf(tempArray, newIdx + 1);
187
-        
209
+            }*/
210
+        //return Arrays.copyOf(tempArray, newIdx + 1);
188
     }
211
     }
189
 
212
 
213
+
190
 }
214
 }
191
 
215
 
192
 
216
 
217
+
218
+