Explorar el Código

lab completed

ThuyKhong hace 8 años
padre
commit
cfa68e7163

+ 12
- 0
pom.xml Ver fichero

7
     <groupId>com.zipcodewilmington.labs</groupId>
7
     <groupId>com.zipcodewilmington.labs</groupId>
8
     <artifactId>arrayutils</artifactId>
8
     <artifactId>arrayutils</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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
     <dependencies>
22
     <dependencies>
11
         <dependency>
23
         <dependency>
12
             <groupId>junit</groupId>
24
             <groupId>junit</groupId>

+ 80
- 13
src/main/java/com/zipcodewilmington/StringArrayUtils.java Ver fichero

1
 package com.zipcodewilmington;
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
  * Created by leon on 1/29/18.
10
  * Created by leon on 1/29/18.
5
  */
11
  */
9
      * @return first element of specified array
15
      * @return first element of specified array
10
      */ // TODO
16
      */ // TODO
11
     public static String getFirstElement(String[] array) {
17
     public static String getFirstElement(String[] array) {
12
-        return null;
18
+
19
+        return array[0];
13
     }
20
     }
14
 
21
 
15
     /**
22
     /**
17
      * @return second element in specified array
24
      * @return second element in specified array
18
      */
25
      */
19
     public static String getSecondElement(String[] array) {
26
     public static String getSecondElement(String[] array) {
20
-        return null;
27
+
28
+        return array[1];
21
     }
29
     }
22
 
30
 
23
     /**
31
     /**
25
      * @return last element in specified array
33
      * @return last element in specified array
26
      */ // TODO
34
      */ // TODO
27
     public static String getLastElement(String[] array) {
35
     public static String getLastElement(String[] array) {
28
-        return null;
36
+
37
+        return array[array.length - 1];
29
     }
38
     }
30
 
39
 
31
     /**
40
     /**
33
      * @return second to last element in specified array
42
      * @return second to last element in specified array
34
      */ // TODO
43
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
44
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
45
+
46
+        return array[array.length - 2];
37
     }
47
     }
38
 
48
 
39
     /**
49
     /**
42
      * @return true if the array contains the specified `value`
52
      * @return true if the array contains the specified `value`
43
      */ // TODO
53
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
      * @return an array with identical contents in reverse order
61
      * @return an array with identical contents in reverse order
51
      */ // TODO
62
      */ // TODO
52
     public static String[] reverse(String[] array) {
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
      * @return true if the order of the array is the same backwards and forwards
72
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
73
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
      * @return true if each letter in the alphabet has been used in the array
89
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
90
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
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
      * @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
+
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
      * @param array         array of String objects
116
      * @param array         array of String objects
83
      * @param valueToRemove value to remove from array
117
      * @param valueToRemove value to remove from array
84
      * @return array with identical contents excluding values of `value`
118
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
119
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @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
+
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
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
153
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
154
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
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 Ver fichero

210
     public void testReverse3() {
210
     public void testReverse3() {
211
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
211
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
212
         String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected));
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
         String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
408
         String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
409
         String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
409
         String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
410
         String[] actual = StringArrayUtils.removeValue(array, "The");
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
     @Test
415
     @Test