Joshua Chung hace 8 años
padre
commit
2288989ed4
Se han modificado 4 ficheros con 172 adiciones y 23 borrados
  1. 0
    0
      README.md
  2. 86
    23
      StringArrayUtils.java
  3. 44
    0
      StringArrayUtilsTest.java
  4. 42
    0
      package.bluej

+ 0
- 0
README.md Ver fichero


+ 86
- 23
StringArrayUtils.java Ver fichero

@@ -1,4 +1,4 @@
1
-import java.util.Arrays;
1
+import java.util.*;
2 2
 
3 3
 /**
4 4
  * Created by leon on 1/29/18.
@@ -24,8 +24,8 @@ public class StringArrayUtils {
24 24
      * @param array array of String objects
25 25
      * @return last element in specified array
26 26
      */ // TODO
27
-    public static String getLastElement(String[] array) {        
28
-        return array[array.length - 1];
27
+    public static String getLastElement(String[] array) {
28
+        return array[array.length-1];
29 29
     }
30 30
 
31 31
     /**
@@ -41,16 +41,17 @@ public class StringArrayUtils {
41 41
      * @param value value to check array for
42 42
      * @return true if the array contains the specified `value`
43 43
      */ // TODO
44
-    public static boolean contains(String[] array, String value) {        
44
+    public static boolean contains(String[] array, String value) {
45 45
         
46 46
         for(String x : array) {
47
-            if(value == x) {
47
+            if(value.equals(x)) {
48 48
                 return true;
49 49
             }
50 50
         }
51 51
         return false;
52 52
     }
53 53
 
54
+
54 55
     /**
55 56
      * @param array of String objects
56 57
      * @return an array with identical contents in reverse order
@@ -59,7 +60,7 @@ public class StringArrayUtils {
59 60
         
60 61
         String[] reverseArr = new String[array.length];
61 62
         
62
-        for(int i=0; i<array.length; i++){            
63
+        for(int i = 0; i < array.length; i++){
63 64
             reverseArr[i] = array[array.length - i - 1];
64 65
         }
65 66
         return reverseArr;
@@ -74,33 +75,60 @@ public class StringArrayUtils {
74 75
         String[] reverseArr = reverse(array);
75 76
         String[] regArr = new String[array.length];
76 77
         
77
-        for(int i=0; i<array.length; i++){
78
-             regArr[i] = array[i];
79
-        }
80
-        
81
-        if(regArr ==  reverseArr) {
82
-            return true;
83
-        }
78
+        for(int i= 0; i < array.length; i++){
79
+            regArr[i] = array[i];
80
+                if(regArr[i] == reverseArr[i]) {
81
+                    return true;
82
+                }
83
+            }
84 84
         return false;
85 85
     }
86 86
 
87
-
88
-
89 87
     /**
90 88
      * @param array array of String objects
91 89
      * @return true if each letter in the alphabet has been used in the array
92 90
      */ // TODO
93 91
     public static boolean isPangramic(String[] array) {
94
-        return false;
92
+        String alphabet = "abcdefghijklmnopqrstuvwxyz";
93
+        String fullCombinedString = String.join("", array).toLowerCase();
94
+        for(char c : alphabet.toCharArray()) {
95
+            if(fullCombinedString.indexOf(c) < 0) {
96
+                return false;
97
+            }
98
+        }
99
+        return true;
100
+        
101
+        
102
+        /*
103
+        for(char c : alphabet.toCharArray()) {
104
+            boolean found = false;
105
+            for(String word : array) {
106
+                word = word.toLowerCase();
107
+                if(word.indexOf(c) >= 0)
108
+                found = true;
109
+                break;
110
+            }
111
+            if(!found) {
112
+                return false;
113
+        }
114
+    }
115
+        return true;*/
95 116
     }
96 117
 
118
+
97 119
     /**
98 120
      * @param array array of String objects
99 121
      * @param value value to check array for
100 122
      * @return number of occurrences the specified `value` has occurred
101 123
      */ // TODO
102 124
     public static int getNumberOfOccurrences(String[] array, String value) {
103
-        return 0;
125
+        int count = 0;
126
+        for(String s : array) {
127
+            if(s == value) {
128
+                count++;
129
+            }
130
+        }
131
+        return count;
104 132
     }
105 133
 
106 134
     /**
@@ -109,7 +137,14 @@ public class StringArrayUtils {
109 137
      * @return array with identical contents excluding values of `value`
110 138
      */ // TODO
111 139
     public static String[] removeValue(String[] array, String valueToRemove) {
112
-        return null;
140
+        List<String> newArray = new ArrayList<>();
141
+        
142
+        for(String s : array) {
143
+            if(s != valueToRemove) {
144
+                newArray.add(s);
145
+            }
146
+        }
147
+        return newArray.toArray(new String[] {});
113 148
     }
114 149
 
115 150
     /**
@@ -117,18 +152,46 @@ public class StringArrayUtils {
117 152
      * @return array of Strings with consecutive duplicates removes
118 153
      */ // TODO
119 154
     public static String[] removeConsecutiveDuplicates(String[] array) {
155
+        List<String> newArray = new ArrayList<>();
120 156
         
157
+        for(int i = 0; i < array.length; i++) {
158
+            String added = array[i];
159
+            newArray.add(added);
160
+            int toRemove = 0;
161
+            
162
+            for(; toRemove + i + 1 < array.length; toRemove++) {
163
+                if(array[i + 1 + toRemove] != added){
164
+                    break;
165
+                }                
166
+            }
167
+            i += toRemove;        
168
+        }
169
+        return newArray.toArray(new String[] {});
170
+}
121 171
 
122
-        
123
-        return null;    
124
-    }
125
-    
126 172
     /**
127 173
      * @param array array of chars
128 174
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
129 175
      */ // TODO
130 176
     public static String[] packConsecutiveDuplicates(String[] array) {
131
-        return null;
177
+        List<String> newArray = new ArrayList<>();
178
+        
179
+        for(int i = 0; i < array.length; i++) {
180
+            String added = array[i];
181
+            String toAdd = added;
182
+            int toRemove = 0;
183
+            
184
+            for(; toRemove + i + 1 < array.length; toRemove++) {
185
+                if(array[i + 1 + toRemove] != added){
186
+                    break;
187
+                }  
188
+                toAdd += array[i + 1 + toRemove];
189
+            }
190
+            newArray.add(toAdd);
191
+            i += toRemove;        
192
+        }
193
+        System.out.println(Arrays.toString(newArray.toArray()));
194
+        return newArray.toArray(new String[] {});
132 195
     }
133 196
 
134 197
 

+ 44
- 0
StringArrayUtilsTest.java Ver fichero

@@ -33,6 +33,19 @@ public class StringArrayUtilsTest {
33 33
         Assert.assertEquals(expected, actual);
34 34
     }
35 35
 
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
36 49
     @Test
37 50
     public void testGetSecondElement1() {
38 51
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -49,6 +62,7 @@ public class StringArrayUtilsTest {
49 62
         Assert.assertEquals(expected, actual);
50 63
     }
51 64
 
65
+
52 66
     @Test
53 67
     public void testGetSecondElement3() {
54 68
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -57,6 +71,16 @@ public class StringArrayUtilsTest {
57 71
         Assert.assertEquals(expected, actual);
58 72
     }
59 73
 
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
60 84
     @Test
61 85
     public void testGetLastElement1() {
62 86
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -82,6 +106,26 @@ public class StringArrayUtilsTest {
82 106
         Assert.assertEquals(expected, actual);
83 107
     }
84 108
 
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
85 129
     @Test
86 130
     public void testGetSecondToLastElement1() {
87 131
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};

+ 42
- 0
package.bluej Ver fichero

@@ -0,0 +1,42 @@
1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=1087
6
+editor.fx.0.width=1239
7
+editor.fx.0.x=600
8
+editor.fx.0.y=0
9
+objectbench.height=155
10
+objectbench.width=760
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.676
13
+package.editor.height=331
14
+package.editor.width=670
15
+package.editor.x=78
16
+package.editor.y=78
17
+package.frame.height=600
18
+package.frame.width=800
19
+package.numDependencies=1
20
+package.numTargets=2
21
+package.showExtends=true
22
+package.showUses=true
23
+project.charset=windows-1252
24
+readme.height=58
25
+readme.name=@README
26
+readme.width=47
27
+readme.x=10
28
+readme.y=10
29
+target1.height=50
30
+target1.name=StringArrayUtilsTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=150
34
+target1.x=120
35
+target1.y=10
36
+target2.height=50
37
+target2.name=StringArrayUtils
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=120
41
+target2.x=70
42
+target2.y=70