瀏覽代碼

finished lab

John Kim 8 年之前
父節點
當前提交
77c271ec42
共有 1 個檔案被更改,包括 73 行新增38 行删除
  1. 73
    38
      StringArrayUtils.java

+ 73
- 38
StringArrayUtils.java 查看文件

43
      * @return true if the array contains the specified `value`
43
      * @return true if the array contains the specified `value`
44
      */ // TODO
44
      */ // TODO
45
     public static boolean contains(String[] array, String value) {
45
     public static boolean contains(String[] array, String value) {
46
+        boolean contains = false;
46
         for (String ithString : array) {
47
         for (String ithString : array) {
47
             if (ithString.equals(value)) {
48
             if (ithString.equals(value)) {
48
-                return true;
49
+                contains = true;
50
+                break;
49
             }
51
             }
50
         }
52
         }
51
-        return false;
53
+        return contains;
52
     }
54
     }
53
 
55
 
54
     /**
56
     /**
69
      */ // TODO
71
      */ // TODO
70
     public static boolean isPalindromic(String[] array) {
72
     public static boolean isPalindromic(String[] array) {
71
         String[] reverse = reverse(array);
73
         String[] reverse = reverse(array);
74
+        boolean isPalindrome = true;
72
         for (int i = 0; i < array.length; i++) {
75
         for (int i = 0; i < array.length; i++) {
73
             if (!array[i].equals(reverse[i])) {
76
             if (!array[i].equals(reverse[i])) {
74
-                return false;
77
+                isPalindrome = false;
78
+                break;
75
             }
79
             }
76
         }
80
         }
77
-        return true;
81
+        return isPalindrome;
78
     }
82
     }
79
 
83
 
80
     /**
84
     /**
82
      * @return true if each letter in the alphabet has been used in the array
86
      * @return true if each letter in the alphabet has been used in the array
83
      */ // TODO
87
      */ // TODO
84
     public static boolean isPangramic(String[] array) {
88
     public static boolean isPangramic(String[] array) {
85
-        //stringbuilder holding all letters of alpha
86
-        StringBuilder alphabet = new StringBuilder("abcdefghijklmnopqrstuvwxyz");
87
-        //iterate over each word of the array and then each char of word
88
-        for (String word : array) {
89
-            for (int i = 0; i < word.length(); i++) {
90
-                int index = alphabet.indexOf("" + word.toLowerCase().charAt(i));
91
-                if (index >= 0) {
92
-                    alphabet.deleteCharAt(index);
93
-                }
94
-                if (alphabet.length() == 0) {
95
-                    return true;
96
-                }
89
+        boolean isPangramic = true;
90
+        String alpha = "abcdefghijklmnopqrstuvwxyz";
91
+        String[] alphaArr = newArray(alpha);
92
+        StringBuilder allChars = new StringBuilder();
93
+        for(String word1 : array) {
94
+            allChars.append(word1);
95
+        }
96
+        String[] allCharsArr = newArray(allChars.toString().toLowerCase());
97
+        
98
+        for (String letter1 : alphaArr) {
99
+            if (!contains(allCharsArr, letter1)) {
100
+                isPangramic = false;
101
+                break;
97
             }
102
             }
98
         }
103
         }
99
-        return false;
104
+        return isPangramic;
105
+    }
106
+    
107
+    public static String[] newArray(String word) {
108
+        String[] newArr = new String[word.length()];
109
+        for (int i = 0; i < word.length(); i++) {
110
+            newArr[i] = "" + word.charAt(i);
111
+        }
112
+        return newArr;
100
     }
113
     }
101
 
114
 
102
     /**
115
     /**
120
      * @return array with identical contents excluding values of `value`
133
      * @return array with identical contents excluding values of `value`
121
      */ // TODO
134
      */ // TODO
122
     public static String[] removeValue(String[] array, String valueToRemove) {
135
     public static String[] removeValue(String[] array, String valueToRemove) {
123
-        List<String> arrRemoved = new ArrayList<String>();
124
-        for (String word : array) {
125
-            if (!word.equals(valueToRemove)) {
126
-                arrRemoved.add(word);
136
+        int numOcc = getNumberOfOccurrences(array, valueToRemove);
137
+        String[] removedArr = new String[array.length - numOcc];
138
+        int i = 0;
139
+        for (String word1 : array) {
140
+            if (!word1.equals(valueToRemove)) {
141
+                removedArr[i] = word1;
142
+                i++;
127
             }
143
             }
128
         }
144
         }
129
-        String[] removedArray = new String[arrRemoved.size()];
130
-        removedArray = arrRemoved.toArray(removedArray);
131
-        return removedArray;
132
-             
145
+        return removedArr;
146
+    }
147
+    
148
+    public static int countDuplicated(String[] array) {
149
+        int count = 0;
150
+        for (int i = 1; i < array.length; i++) {
151
+            if (array[i].equals(array[i - 1])) {
152
+                count++;
153
+            }
154
+        }
155
+        return count;
133
     }
156
     }
134
-
135
     /**
157
     /**
136
      * @param array array of chars
158
      * @param array array of chars
137
      * @return array of Strings with consecutive duplicates removes
159
      * @return array of Strings with consecutive duplicates removes
138
      */ // TODO
160
      */ // TODO
139
     public static String[] removeConsecutiveDuplicates(String[] array) {
161
     public static String[] removeConsecutiveDuplicates(String[] array) {
140
-        List<String> removedValues = new ArrayList<String>();
141
-        for (String word : array) {
142
-            //empty or last added value doesnt equal iterated word of array
143
-            if (removedValues.isEmpty() || 
144
-            !removedValues.get(removedValues.size() - 1).equals(word)) {
145
-                removedValues.add(word);
162
+        int totalDups = countDuplicated(array);
163
+        String[] removedArr = new String[array.length - totalDups];
164
+        
165
+        int index = 1;
166
+        removedArr[0] = array[0];
167
+        for (int i = 1; i < array.length; i++) {
168
+            if (!array[i].equals(array[i - 1])) {
169
+                removedArr[index] = array[i];
170
+                index++;
146
             }
171
             }
147
         }
172
         }
148
-        String[] nonDuplicate = new String[removedValues.size()];
149
-        nonDuplicate = removedValues.toArray(nonDuplicate);
150
-        return nonDuplicate;
173
+        return removedArr;
151
     }
174
     }
152
 
175
 
153
     /**
176
     /**
155
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
178
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
156
      */ // TODO
179
      */ // TODO
157
     public static String[] packConsecutiveDuplicates(String[] array) {
180
     public static String[] packConsecutiveDuplicates(String[] array) {
158
-        for (String singleChar : array) {
159
-            
181
+        StringBuilder dupB = new StringBuilder(array[0]);
182
+        String[] packedDupArr= new String[array.length - countDuplicated(array)];
183
+        
184
+        int index = 0;
185
+        for (int i = 1; i < array.length; i++) {
186
+            if (array[i].equals(array[i - 1])) {
187
+                dupB.append(array[i]);
188
+            } else {
189
+                packedDupArr[index] = dupB.toString();
190
+                dupB.setLength(0);
191
+                dupB.append(array[i]);
192
+                index++;
193
+            }
160
         }
194
         }
161
-        return null;
195
+        packedDupArr[index] = dupB.toString();
196
+        return packedDupArr;
162
     }
197
     }
163
 
198
 
164
 }
199
 }