ソースを参照

lab completed

ThuyKhong 8 年 前
コミット
cfa68e7163
共有3 個のファイルを変更した95 個の追加15 個の削除を含む
  1. 12
    0
      pom.xml
  2. 80
    13
      src/main/java/com/zipcodewilmington/StringArrayUtils.java
  3. 3
    2
      src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java

+ 12
- 0
pom.xml ファイルの表示

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington.labs</groupId>
8 8
     <artifactId>arrayutils</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.8</source>
17
+                    <target>1.8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 80
- 13
src/main/java/com/zipcodewilmington/StringArrayUtils.java ファイルの表示

@@ -1,5 +1,11 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import com.sun.tools.javac.util.ArrayUtils;
4
+import org.omg.CORBA.Current;
5
+
6
+import java.sql.SQLOutput;
7
+import java.util.*;
8
+
3 9
 /**
4 10
  * Created by leon on 1/29/18.
5 11
  */
@@ -9,7 +15,8 @@ public class StringArrayUtils {
9 15
      * @return first element of specified array
10 16
      */ // TODO
11 17
     public static String getFirstElement(String[] array) {
12
-        return null;
18
+
19
+        return array[0];
13 20
     }
14 21
 
15 22
     /**
@@ -17,7 +24,8 @@ public class StringArrayUtils {
17 24
      * @return second element in specified array
18 25
      */
19 26
     public static String getSecondElement(String[] array) {
20
-        return null;
27
+
28
+        return array[1];
21 29
     }
22 30
 
23 31
     /**
@@ -25,7 +33,8 @@ public class StringArrayUtils {
25 33
      * @return last element in specified array
26 34
      */ // TODO
27 35
     public static String getLastElement(String[] array) {
28
-        return null;
36
+
37
+        return array[array.length - 1];
29 38
     }
30 39
 
31 40
     /**
@@ -33,7 +42,8 @@ public class StringArrayUtils {
33 42
      * @return second to last element in specified array
34 43
      */ // TODO
35 44
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
45
+
46
+        return array[array.length - 2];
37 47
     }
38 48
 
39 49
     /**
@@ -42,7 +52,8 @@ public class StringArrayUtils {
42 52
      * @return true if the array contains the specified `value`
43 53
      */ // TODO
44 54
     public static boolean contains(String[] array, String value) {
45
-        return false;
55
+
56
+        return Arrays.asList(array).contains(value);
46 57
     }
47 58
 
48 59
     /**
@@ -50,7 +61,10 @@ public class StringArrayUtils {
50 61
      * @return an array with identical contents in reverse order
51 62
      */ // TODO
52 63
     public static String[] reverse(String[] array) {
53
-        return null;
64
+        String[] a = Arrays.copyOf(array, array.length);
65
+        List<String> list = Arrays.asList(a);
66
+        Collections.reverse(list);
67
+        return (String[]) list.toArray();
54 68
     }
55 69
 
56 70
     /**
@@ -58,7 +72,16 @@ public class StringArrayUtils {
58 72
      * @return true if the order of the array is the same backwards and forwards
59 73
      */ // TODO
60 74
     public static boolean isPalindromic(String[] array) {
61
-        return false;
75
+        int length = array.length;
76
+        for (int i = 0; i < array.length; i++) {
77
+            String start = array[i];
78
+            String end = array[--length];
79
+            if (array.length < i) {
80
+                return true;
81
+            }
82
+            if (start != end) return false;
83
+        }
84
+        return true;
62 85
     }
63 86
 
64 87
     /**
@@ -66,7 +89,11 @@ public class StringArrayUtils {
66 89
      * @return true if each letter in the alphabet has been used in the array
67 90
      */ // TODO
68 91
     public static boolean isPangramic(String[] array) {
69
-        return false;
92
+        String asString = Arrays.toString(array);
93
+        asString = asString.toLowerCase();
94
+        boolean isPang = asString.chars().filter(i -> i >= 'a' && i <= 'z').distinct().count() == 26;
95
+
96
+        return isPang;
70 97
     }
71 98
 
72 99
     /**
@@ -75,16 +102,31 @@ public class StringArrayUtils {
75 102
      * @return number of occurrences the specified `value` has occurred
76 103
      */ // TODO
77 104
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
105
+
106
+        int counter = 0;
107
+        for (int i = 0; i < array.length; i++) {
108
+            if (array[i].equals(value))
109
+                counter++;
110
+        }
111
+        return counter;
79 112
     }
80 113
 
114
+
81 115
     /**
82 116
      * @param array         array of String objects
83 117
      * @param valueToRemove value to remove from array
84 118
      * @return array with identical contents excluding values of `value`
85 119
      */ // TODO
86 120
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
121
+
122
+        List result = new LinkedList();
123
+        for (String keepMe : array) {
124
+            if (!valueToRemove.equals(keepMe)) {
125
+                result.add(keepMe);
126
+            }
127
+        }
128
+        String[] newArray = new String[result.size()]; //create new array with length of list
129
+        return (String[]) result.toArray(newArray);
88 130
     }
89 131
 
90 132
     /**
@@ -92,7 +134,18 @@ public class StringArrayUtils {
92 134
      * @return array of Strings with consecutive duplicates removes
93 135
      */ // TODO
94 136
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
137
+
138
+        ArrayList<String> listArray = new ArrayList<>();
139
+
140
+        for (int i = 0; i < array.length - 1; i++) {
141
+            if (!array[i].equals(array[i + 1])) {
142
+                listArray.add(array[i]);
143
+                System.out.println(listArray);
144
+            }
145
+        }
146
+        listArray.add(array[array.length - 1]);
147
+        String[] newArray = new String[listArray.size()];
148
+        return listArray.toArray(newArray);
96 149
     }
97 150
 
98 151
     /**
@@ -100,8 +153,22 @@ public class StringArrayUtils {
100 153
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 154
      */ // TODO
102 155
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
156
+        StringBuilder builder2 = new StringBuilder();
157
+        String CurrentChar = "";
105 158
 
159
+        for (int i = 0; i < array.length; i++) {
106 160
 
161
+            if (CurrentChar.equals(array[i])) {
162
+                builder2.append(array[i]);
163
+
164
+            } else {
165
+                builder2.append("," + array[i]);
166
+                CurrentChar = array[i];
167
+            }
168
+
169
+        }
170
+        CurrentChar = builder2.substring(1, builder2.length());
171
+
172
+        return CurrentChar.split(",");
173
+    }
107 174
 }

+ 3
- 2
src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java ファイルの表示

@@ -210,7 +210,7 @@ public class StringArrayUtilsTest {
210 210
     public void testReverse3() {
211 211
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
212 212
         String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected));
213
-        Assert.assertEquals(expected, actual);
213
+        Assert.assertArrayEquals(expected, actual);
214 214
     }
215 215
 
216 216
 
@@ -408,7 +408,8 @@ public class StringArrayUtilsTest {
408 408
         String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
409 409
         String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
410 410
         String[] actual = StringArrayUtils.removeValue(array, "The");
411
-        Assert.assertEquals(expected, actual);
411
+        System.out.println(actual.length);
412
+        Assert.assertArrayEquals(expected, actual);
412 413
     }
413 414
 
414 415
     @Test