Khalil Malik Saboor 8 years ago
parent
commit
cb192f7396
2 changed files with 77 additions and 39 deletions
  1. 73
    35
      StringArrayUtils.java
  2. 4
    4
      package.bluej

+ 73
- 35
StringArrayUtils.java View File

1
-
1
+import java.util.Arrays;
2
 /**
2
 /**
3
  * Created by leon on 1/29/18.
3
  * Created by leon on 1/29/18.
4
  */
4
  */
89
          */
89
          */
90
         boolean returnPal = false;
90
         boolean returnPal = false;
91
         String [] reverseArray = reverse(array);
91
         String [] reverseArray = reverse(array);
92
-        /*
93
-         * 
94
-         */
95
         for(int i = 0; i < array.length; i++){
92
         for(int i = 0; i < array.length; i++){
96
-            System.out.println("array[i] " + array[i] + " reverseArray[i] " + reverseArray[i]);
93
+            // System.out.println("array[i] " + array[i] + " reverseArray[i] " + reverseArray[i]);
97
             if(array[i].equals(reverseArray[i])){
94
             if(array[i].equals(reverseArray[i])){
98
                 returnPal = true;
95
                 returnPal = true;
99
             }
96
             }
100
         }
97
         }
101
-
102
         return returnPal;
98
         return returnPal;
103
     }
99
     }
104
 
100
 
111
          * if(isPangramic.length() < letters){
107
          * if(isPangramic.length() < letters){
112
          *  }
108
          *  }
113
          */
109
          */
114
-        int letters = 26;
115
-        boolean pangram = false;
116
-
117
-        if(array.length < letters){
118
-            pangram = true;
119
-        }
120
-        if(array.length == letters){
121
-            pangram = false;
122
-        }else{
123
-
124
-            pangram = true;
125
-
110
+        //        int letters = 26;
111
+        //        boolean pangram = false;
112
+        //
113
+        //        if(array.length < letters){
114
+        //            pangram = true;
115
+        //        }
116
+        //        if(array.length == letters){
117
+        //            pangram = false;
118
+        //        }else{
119
+        //            pangram = true;
120
+        //        }
121
+        //        return pangram;
122
+        String stringToArray = array.toString();
123
+        String str = stringToArray.toLowerCase();
124
+        boolean answer = false;
125
+        for (char start = 'a';start <='z';++start){
126
+            if (!str.contains(String.valueOf(start))){
127
+                answer = true;
128
+                break;
129
+            }
126
         }
130
         }
127
-
128
-        return pangram;
131
+        return answer;
129
     }
132
     }
130
 
133
 
131
     /**
134
     /**
134
      * @return number of occurrences the specified `value` has occurred
137
      * @return number of occurrences the specified `value` has occurred
135
      */ // TODO
138
      */ // TODO
136
     public static int getNumberOfOccurrences(String[] array, String value) {
139
     public static int getNumberOfOccurrences(String[] array, String value) {
137
-        /*
138
-         * 
139
-         */
140
+
140
         int count = 0;
141
         int count = 0;
141
         for(int i = 0;i < array.length;i++){
142
         for(int i = 0;i < array.length;i++){
142
             if(array[i] == value){
143
             if(array[i] == value){
153
      */ // TODO
154
      */ // TODO
154
     public static String[] removeValue(String[] array, String valueToRemove) {
155
     public static String[] removeValue(String[] array, String valueToRemove) {
155
         StringBuilder sb = new StringBuilder();
156
         StringBuilder sb = new StringBuilder();
156
-        
157
+
157
         //Looping through array
158
         //Looping through array
158
         for(int index = 0;index < array.length;index++){
159
         for(int index = 0;index < array.length;index++){
159
             //if the array value does not equal
160
             //if the array value does not equal
160
             //the value to remove, then add the values to a temp array
161
             //the value to remove, then add the values to a temp array
161
-           
162
+
162
             if(array[index] != valueToRemove ){
163
             if(array[index] != valueToRemove ){
163
-             sb.append(array[index] + "~");
164
-                
164
+                sb.append(array[index] + "~");
165
+
165
             }
166
             }
166
-            
167
-            
167
+
168
         }
168
         }
169
         String answer = sb.toString();
169
         String answer = sb.toString();
170
-        
171
         String [] temp = answer.split("~");
170
         String [] temp = answer.split("~");
172
         return temp;
171
         return temp;
173
     }
172
     }
177
      * @return array of Strings with consecutive duplicates removes
176
      * @return array of Strings with consecutive duplicates removes
178
      */ // TODO
177
      */ // TODO
179
     public static String[] removeConsecutiveDuplicates(String[] array) {
178
     public static String[] removeConsecutiveDuplicates(String[] array) {
180
-        
181
-        
182
-        
183
-        return null;
179
+        //created emptry array with length of array
180
+        //created an
181
+        String [] answer = new String[array.length];
182
+        int i = 0;
183
+        answer[i]=array[0];
184
+         i++; 
185
+        for(int j=1;j<array.length;j++){
186
+             if(!answer[i-1].equals(array[j])){
187
+                answer[i]=array[j];
188
+                 i++; 
189
+                }
190
+            }
191
+        String [] newArray = new String [i];
192
+        //copies a source array from a specific beginning position to the destination array
193
+        // from the mentioned position.
194
+        // No. of arguments to be copied are decided by len argument.
195
+        System.arraycopy(answer, 0, newArray, 0, i);
196
+        return newArray;
197
+    }
198
+    
199
+    public static int counter(String[] array){
200
+         int counter = 0;
201
+        for(int index = 0; index < array.length-1; index++){
202
+            if(array[index].equals(array[index+1])){
203
+            counter = counter+=1;
204
+          }
205
+        }          
206
+        return counter; 
184
     }
207
     }
185
 
208
 
186
     /**
209
     /**
188
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
211
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
189
      */ // TODO
212
      */ // TODO
190
     public static String[] packConsecutiveDuplicates(String[] array) {
213
     public static String[] packConsecutiveDuplicates(String[] array) {
191
-        return null;
214
+           StringBuilder pack = new StringBuilder(array[0]);
215
+           String [] answer = new String[array.length-counter(array)];
216
+           int index = 0;  
217
+           for(int i=1;i<array.length;i++){
218
+            if(array[i-1].equals(array[i])){           
219
+                pack.append(array[i]);
220
+                
221
+            } else{            
222
+                answer[index] = pack.toString();
223
+                pack.setLength(0);
224
+                pack.append(array[i]);
225
+                index++;
226
+            }
227
+            }
228
+             answer[index]=pack.toString();
229
+        return answer;
192
     }
230
     }
193
 
231
 
194
 }
232
 }

+ 4
- 4
package.bluej View File

2
 dependency1.from=StringArrayUtilsTest
2
 dependency1.from=StringArrayUtilsTest
3
 dependency1.to=StringArrayUtils
3
 dependency1.to=StringArrayUtils
4
 dependency1.type=UsesDependency
4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=709
6
-editor.fx.0.width=799
7
-editor.fx.0.x=481
8
-editor.fx.0.y=23
5
+editor.fx.0.height=722
6
+editor.fx.0.width=800
7
+editor.fx.0.x=240
8
+editor.fx.0.y=26
9
 objectbench.height=102
9
 objectbench.height=102
10
 objectbench.width=461
10
 objectbench.width=461
11
 package.divider.horizontal=0.6
11
 package.divider.horizontal=0.6