donnaj пре 6 година
родитељ
комит
fd8b4f75de
2 измењених фајлова са 93 додато и 37 уклоњено
  1. 88
    32
      StringArrayUtils.java
  2. 5
    5
      package.bluej

+ 88
- 32
StringArrayUtils.java Прегледај датотеку

@@ -1,9 +1,10 @@
1
- 
1
+import java.util.Arrays;
2 2
 
3 3
 /**
4 4
  * Created by leon on 1/29/18.
5 5
  */
6 6
 public class StringArrayUtils {
7
+    
7 8
     /**
8 9
      * @param array array of String objects
9 10
      * @return first element of specified array
@@ -47,7 +48,7 @@ public class StringArrayUtils {
47 48
         }
48 49
         return false;
49 50
     }
50
-    
51
+
51 52
     /**
52 53
      * @param array of String objects
53 54
      * @return an array with identical contents in reverse order
@@ -65,35 +66,45 @@ public class StringArrayUtils {
65 66
      * @return true if the order of the array is the same backwards and forwards
66 67
      */ // TODO
67 68
     public static boolean isPalindromic(String[] array) {
68
-        
69
-    for ( int i = 0; i < array.length / 2; i++) {
70
-        if (array[i] != array[array.length - 1 - i]) {
71
-          return false;
72
-        }
73
-    }    
74
-    return true;
69
+
70
+        for ( int i = 0; i < array.length / 2; i++) {
71
+            if (array[i] != array[array.length - 1 - i]) {
72
+                return false;
73
+            }
74
+        }    
75
+        return true;
75 76
     }
76
-    
77
+
77 78
     /**
78 79
      * @param array array of String objects
79 80
      * @return true if each letter in the alphabet has been used in the array
80 81
      */ // TODO
81 82
     public static boolean isPangramic(String[] array) {
82
-        for(char c = 'a';c <= 'z'; ++c) {
83
-            if(array.toString().contains(String.valueOf(c))) {
84
-                return true;
85
-        
83
+        //boolean result= true;
84
+        String words = String.join("", array).toLowerCase();
85
+
86
+      int counterTrue = 0;
87
+        for(char c = 'a';c <= 'z'; c++) {
88
+
89
+            if(words.contains(String.valueOf(c))) {
90
+                //result = false;
91
+                counterTrue++;
92
+                //return true;
93
+            } 
94
+        }
95
+
96
+        if (counterTrue == 26) return true;
97
+        return false;
86 98
     }
87 99
         
88
-    }return false;
89
-    }
100
+    
90 101
 
91 102
     /**
92 103
      * @param array array of String objects
93 104
      * @param value value to check array for
94 105
      * @return number of occurrences the specified `value` has occurred
95 106
      */ // TODO
96
-    
107
+
97 108
     public static int getNumberOfOccurrences(String[] array, String value) {
98 109
         int num = 0;
99 110
         for (int i=0;i<array.length;i++){
@@ -105,24 +116,30 @@ public class StringArrayUtils {
105 116
 
106 117
         return num;
107 118
     }
108
-    
119
+
109 120
     /**
110 121
      * @param array         array of String objects
111 122
      * @param valueToRemove value to remove from array
112 123
      * @return array with identical contents excluding values of `value`
113 124
      */ // TODO
114 125
     public static String[] removeValue(String[] array, String valueToRemove) {
115
-        String[] arr = new String[array.length-1];
116
-        String[] remArr = new String[1];
117
-        for (int i=0;i<array.length;i++){
118
-            if (array[i]!=valueToRemove){
119
-                arr[i] = array[i];
120
-
121
-            }else { remArr[i] += array[i];}
122
-
126
+        int newLength = array.length;
127
+        String[] result = new String[array.length-1];
128
+        String[] removedValue = new String[1];
129
+        int count1 = 0; // count tracks the current index of the output array
130
+        int count2 = 0; // count tracks the current index of the output array
131
+        
132
+        for(int i = 0; i < array.length; i++) // i tracks the current index of the input array
133
+        {
134
+            if(!array[i].contains(valueToRemove)) {
135
+                result[count1] = array[i]; 
136
+                count1++;
137
+            }else {
138
+                removedValue[count2] = array[i];
139
+                count2++;
140
+            }
123 141
         }
124
-
125
-        return arr;
142
+        return result;
126 143
     }
127 144
 
128 145
     /**
@@ -130,16 +147,55 @@ public class StringArrayUtils {
130 147
      * @return array of Strings with consecutive duplicates removes
131 148
      */ // TODO
132 149
     public static String[] removeConsecutiveDuplicates(String[] array) {
133
-        return null;
150
+        String[] resultArray = new String[array.length];
151
+        int resultIndex = 0;
152
+        for(int i = 0; i < array.length; i++) {
153
+            
154
+            if(i == 0) {
155
+                resultArray[resultIndex] = array[i];
156
+                resultIndex++;
157
+            }
158
+            else if((i > 0 && i < array.length-2) && (!array[i].equals(array[i-1]) || !array[i].equals(array[i+1])))
159
+            {
160
+                resultArray[resultIndex] = array[i];
161
+                resultIndex++;
162
+            }else if (i == array.length-1 && !array[i].equals(array[i-1])) {
163
+                resultArray[resultIndex] = array[i];
164
+                resultIndex++;
165
+            }
166
+        }
167
+        String[] realAnswer = Arrays.copyOf(resultArray, resultIndex);
168
+        return realAnswer;
134 169
     }
135
-    
170
+
136 171
     /**
137 172
      * @param array array of chars
138 173
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
139 174
      */ // TODO
140 175
     public static String[] packConsecutiveDuplicates(String[] array) {
141
-        return null;
176
+        String[] resultArray = new String[array.length];
177
+        StringBuilder dups = new StringBuilder();
178
+        int resultIndex = 0;
179
+        for(int i = 0; i < array.length; i++) {
180
+            
181
+            if(i == 0) {
182
+                resultArray[resultIndex] = array[i];
183
+                dups.append(array[i].toString());
184
+                resultIndex++;
185
+            }
186
+            else if((i > 0 && i < array.length-2) && (array[i].equals(array[i-1]) || array[i].equals(array[i+1])))
187
+            {
188
+                resultArray[resultIndex] = array[i];
189
+                dups.append(array[i].toString());
190
+                resultIndex++;
191
+            }else if (i == array.length-1 && array[i].equals(array[i-1])) {
192
+                resultArray[resultIndex] = array[i];
193
+                dups.append(array[i].toString());
194
+                resultIndex++;
195
+            }
196
+        }
197
+        String[] realAnswer = Arrays.copyOf(resultArray, resultIndex);
198
+        return realAnswer;
142 199
     }
143 200
 
144
-
145 201
 }

+ 5
- 5
package.bluej Прегледај датотеку

@@ -2,18 +2,18 @@
2 2
 dependency1.from=StringArrayUtilsTest
3 3
 dependency1.to=StringArrayUtils
4 4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=722
5
+editor.fx.0.height=714
6 6
 editor.fx.0.width=800
7
-editor.fx.0.x=240
8
-editor.fx.0.y=23
7
+editor.fx.0.x=442
8
+editor.fx.0.y=33
9 9
 objectbench.height=101
10 10
 objectbench.width=461
11 11
 package.divider.horizontal=0.6
12 12
 package.divider.vertical=0.8007380073800738
13 13
 package.editor.height=427
14 14
 package.editor.width=674
15
-package.editor.x=91
16
-package.editor.y=68
15
+package.editor.x=267
16
+package.editor.y=85
17 17
 package.frame.height=600
18 18
 package.frame.width=800
19 19
 package.numDependencies=1