Seth преди 8 години
родител
ревизия
84d5ebcbde
променени са 1 файла, в които са добавени 66 реда и са изтрити 40 реда
  1. 66
    40
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 66
- 40
src/main/java/com/zipcodewilmington/StringArrayUtils.java Целия файл

@@ -1,6 +1,7 @@
1 1
 package com.zipcodewilmington;
2 2
 
3 3
 
4
+import java.lang.reflect.Array;
4 5
 import java.util.Arrays;
5 6
 
6 7
 /**
@@ -49,8 +50,8 @@ public class StringArrayUtils {
49 50
      * @return true if the array contains the specified `value`
50 51
      */ // TODO
51 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 55
                 return true;
55 56
             }
56 57
         }
@@ -65,13 +66,13 @@ public class StringArrayUtils {
65 66
     public static String[] reverse(String[] array) {
66 67
         String[] reverseArray = new String[array.length];
67 68
         int b = 1;
68
-        for(int j = 0; j < reverseArray.length; j++){
69
+        for (int j = 0; j < reverseArray.length; j++) {
69 70
             reverseArray[j] = array[array.length - b];
70 71
             b++;
71
-            }
72
+        }
72 73
 
73 74
 
74
-            return reverseArray;
75
+        return reverseArray;
75 76
     }
76 77
 
77 78
     /**
@@ -79,14 +80,9 @@ public class StringArrayUtils {
79 80
      * @return true if the order of the array is the same backwards and forwards
80 81
      */ // TODO
81 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,13 +90,13 @@ public class StringArrayUtils {
94 90
      * @return true if each letter in the alphabet has been used in the array
95 91
      */ // TODO
96 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,7 +106,7 @@ public class StringArrayUtils {
110 106
      */ // TODO
111 107
     public static int getNumberOfOccurrences(String[] array, String value) {
112 108
         int counter = 0;
113
-        for(String looper : array) {
109
+        for (String looper : array) {
114 110
             if (looper.equals(value)) {
115 111
                 counter++;
116 112
             }
@@ -123,20 +119,19 @@ public class StringArrayUtils {
123 119
      * @param valueToRemove value to remove from array
124 120
      * @return array with identical contents excluding values of `value`
125 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 137
      * @param array array of chars
@@ -146,11 +141,11 @@ public class StringArrayUtils {
146 141
         String[] tempArray = new String[array.length];
147 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 145
             String current = array[i - 1];
151
-            if(!current.equals(array[i])){
146
+            if (!current.equals(array[i])) {
152 147
                 tempArray[newIdx] = array[i - 1];
153
-              newIdx++;
148
+                newIdx++;
154 149
             }
155 150
         }
156 151
         tempArray[newIdx] = array[array.length - 1];
@@ -159,7 +154,6 @@ public class StringArrayUtils {
159 154
     }
160 155
 
161 156
 
162
-
163 157
     /**
164 158
      * @param array array of chars
165 159
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
@@ -167,6 +161,38 @@ public class StringArrayUtils {
167 161
     public static String[] packConsecutiveDuplicates(String[] array) {
168 162
         String[] tempArray = new String[array.length];
169 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 196
         String last  = array[0];
171 197
         String next = "";
172 198
         String set = "";
@@ -174,19 +200,19 @@ public class StringArrayUtils {
174 200
              next = array[i+1];
175 201
             if(next.equals(last)){
176 202
                 set += last;
203
+                last = array[i+1];
177 204
 
178 205
             } else {
179 206
                 tempArray[newIdx] = set;
180 207
                 set ="";
181 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
+