ソースを参照

need to work on my habit to commit regularly

Yauheni Papko 6 年 前
コミット
22d38872b5
共有3 個のファイルを変更した139 個の追加16 個の削除を含む
  1. 82
    14
      StringArrayUtils.java
  2. 15
    2
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 82
- 14
StringArrayUtils.java ファイルの表示

1
- 
1
+import java.util.Arrays;
2
 
2
 
3
 /**
3
 /**
4
  * Created by leon on 1/29/18.
4
  * Created by leon on 1/29/18.
9
      * @return first element of specified array
9
      * @return first element of specified array
10
      */ // TODO
10
      */ // TODO
11
     public static String getFirstElement(String[] array) {
11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13
     }
13
     }
14
 
14
 
15
     /**
15
     /**
17
      * @return second element in specified array
17
      * @return second element in specified array
18
      */
18
      */
19
     public static String getSecondElement(String[] array) {
19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21
     }
21
     }
22
 
22
 
23
     /**
23
     /**
25
      * @return last element in specified array
25
      * @return last element in specified array
26
      */ // TODO
26
      */ // TODO
27
     public static String getLastElement(String[] array) {
27
     public static String getLastElement(String[] array) {
28
-        return null;
28
+        return array[array.length-1];
29
     }
29
     }
30
 
30
 
31
     /**
31
     /**
33
      * @return second to last element in specified array
33
      * @return second to last element in specified array
34
      */ // TODO
34
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
35
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
36
+        return array[array.length-2];
37
     }
37
     }
38
 
38
 
39
     /**
39
     /**
42
      * @return true if the array contains the specified `value`
42
      * @return true if the array contains the specified `value`
43
      */ // TODO
43
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
44
     public static boolean contains(String[] array, String value) {
45
-        return false;
45
+        boolean output = false;
46
+        for (String element : array) {
47
+            if (element.equals(value)) {
48
+                output = true;
49
+            }
50
+        }
51
+        return output;
46
     }
52
     }
47
 
53
 
48
     /**
54
     /**
50
      * @return an array with identical contents in reverse order
56
      * @return an array with identical contents in reverse order
51
      */ // TODO
57
      */ // TODO
52
     public static String[] reverse(String[] array) {
58
     public static String[] reverse(String[] array) {
53
-        return null;
59
+        String[] reversedArray = new String[array.length];
60
+        for (int i=array.length-1; i >= 0; i--) {
61
+            reversedArray[array.length-1-i] = array[i];
62
+        }
63
+        return reversedArray;
54
     }
64
     }
55
 
65
 
56
     /**
66
     /**
58
      * @return true if the order of the array is the same backwards and forwards
68
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
69
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
70
     public static boolean isPalindromic(String[] array) {
61
-        return false;
71
+        boolean output = false;
72
+        String[] reversedArray = reverse(array);
73
+        if (Arrays.equals(array, reversedArray)) {
74
+            output = true;
75
+        }
76
+        return output;
62
     }
77
     }
63
 
78
 
64
     /**
79
     /**
66
      * @return true if each letter in the alphabet has been used in the array
81
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
82
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
83
     public static boolean isPangramic(String[] array) {
69
-        return false;
84
+        boolean outcome = true;
85
+        String[] arrayGiven = new String[1];
86
+        arrayGiven[0]="";
87
+        for (int i=0; i < array.length; i++) {
88
+            arrayGiven[0] = arrayGiven[0].concat(array[i]);
89
+        }
90
+        arrayGiven = arrayGiven[0].toLowerCase().split("");
91
+        for (char alph = 'a'; alph <= 'z'; alph++) {
92
+            if (contains(arrayGiven, String.valueOf(alph))==false) {
93
+                outcome = false;
94
+            }
95
+        }
96
+        return outcome;
70
     }
97
     }
71
 
98
 
72
     /**
99
     /**
75
      * @return number of occurrences the specified `value` has occurred
102
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
103
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
104
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
105
+        int counter = 0;
106
+        for (String element : array) {
107
+            if (element.equals(value)) {
108
+                counter++;
109
+            }
110
+        }
111
+        return counter;
79
     }
112
     }
80
 
113
 
81
     /**
114
     /**
84
      * @return array with identical contents excluding values of `value`
117
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
118
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
119
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
120
+        int numberOfElementsToDelete = getNumberOfOccurrences(array, valueToRemove);
121
+        String[] arrayWithRemovedValue = new String[array.length-numberOfElementsToDelete];
122
+        int j=0; //index of arrayWithRemovedValue       
123
+        for (int i=0; i < array.length; i++) {
124
+            if (array[i].compareTo(valueToRemove) != 0) {
125
+                arrayWithRemovedValue[j] = array[i];
126
+                j++;
127
+            }
128
+        }
129
+        return arrayWithRemovedValue;
88
     }
130
     }
89
 
131
 
90
     /**
132
     /**
92
      * @return array of Strings with consecutive duplicates removes
134
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
135
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
136
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
137
+        int numberOfElementsToDelete = 0;
138
+        int j=0; //index of arrayWithRemovedVConsecutiveDuplicates
139
+        String[] arrayWithRemovedConsecutiveDuplicates = new String[array.length];
140
+        for (int i=0; i < array.length-1; i++) {
141
+            if (array[i].compareTo(array[i+1]) != 0) {
142
+                arrayWithRemovedConsecutiveDuplicates[j] = array[i];
143
+                j++;
144
+            } else {
145
+                numberOfElementsToDelete++;
146
+            }
147
+        }
148
+        arrayWithRemovedConsecutiveDuplicates = Arrays.copyOf(arrayWithRemovedConsecutiveDuplicates, array.length-numberOfElementsToDelete);
149
+        arrayWithRemovedConsecutiveDuplicates[arrayWithRemovedConsecutiveDuplicates.length-1] = array[array.length-1];
150
+        return arrayWithRemovedConsecutiveDuplicates;
96
     }
151
     }
97
 
152
 
98
     /**
153
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
155
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
156
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
157
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
158
+        String[] arrayWithPackedConsecutiveDuplicates = new String[array.length];
159
+        int j=0; //index of arrayWithPackedVConsecutiveDuplicates
160
+        int numberOfElementsToDelete = 0;
161
+        arrayWithPackedConsecutiveDuplicates[0] = array[0];
162
+        for (int i=0; i < array.length-1; i++) {
163
+            if (array[i].compareTo(array[i+1]) == 0) {
164
+                arrayWithPackedConsecutiveDuplicates[j] = arrayWithPackedConsecutiveDuplicates[j] + array[i+1];
165
+                numberOfElementsToDelete++;
166
+            } else {
167
+                arrayWithPackedConsecutiveDuplicates[j+1] = array[i+1];
168
+                j++;
169
+            }
170
+        }
171
+        arrayWithPackedConsecutiveDuplicates = Arrays.copyOf(arrayWithPackedConsecutiveDuplicates, array.length-numberOfElementsToDelete);
172
+        return arrayWithPackedConsecutiveDuplicates;
104
     }
173
     }
105
 
174
 
106
-
107
 }
175
 }

+ 15
- 2
StringArrayUtilsTest.java ファイルの表示

176
         }
176
         }
177
     }
177
     }
178
 
178
 
179
-
179
+    @Test
180
+    public void testContains1() {
181
+        String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
182
+       
183
+            boolean outcome = StringArrayUtils.contains(array, "fox");
184
+            Assert.assertTrue(outcome);
185
+        
186
+    }
180
 
187
 
181
 
188
 
182
 
189
 
396
         Assert.assertEquals(expected, actual);
403
         Assert.assertEquals(expected, actual);
397
     }
404
     }
398
 
405
 
399
-
406
+    @Test
407
+    public void testRemovePackDuplicates4() {
408
+        String[] array = {"m", "m", "o", "o", "n", "m", "a", "n"};
409
+        String[] expected = {"mm", "oo", "n", "m", "a", "n"};
410
+        String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
411
+        Assert.assertEquals(expected, actual);
412
+    }
400
 
413
 
401
 
414
 
402
 
415
 

+ 42
- 0
package.bluej ファイルの表示

1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=722
6
+editor.fx.0.width=829
7
+editor.fx.0.x=0
8
+editor.fx.0.y=74
9
+objectbench.height=101
10
+objectbench.width=461
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.8007380073800738
13
+package.editor.height=427
14
+package.editor.width=674
15
+package.editor.x=402
16
+package.editor.y=106
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=UTF-8
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=130
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