Bladeren bron

all tests passing

Jacob Andersen 8 jaren geleden
bovenliggende
commit
1a3267f9fc
2 gewijzigde bestanden met toevoegingen van 125 en 11 verwijderingen
  1. 12
    0
      pom.xml
  2. 113
    11
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 12
- 0
pom.xml Bestand weergeven

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>8</source>
17
+                    <target>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>

+ 113
- 11
src/main/java/com/zipcodewilmington/StringArrayUtils.java Bestand weergeven

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+
3
 /**
6
 /**
4
  * Created by leon on 1/29/18.
7
  * Created by leon on 1/29/18.
5
  */
8
  */
9
      * @return first element of specified array
12
      * @return first element of specified array
10
      */ // TODO
13
      */ // TODO
11
     public static String getFirstElement(String[] array) {
14
     public static String getFirstElement(String[] array) {
12
-        return null;
15
+
16
+        return array[0];
13
     }
17
     }
14
 
18
 
15
     /**
19
     /**
17
      * @return second element in specified array
21
      * @return second element in specified array
18
      */
22
      */
19
     public static String getSecondElement(String[] array) {
23
     public static String getSecondElement(String[] array) {
20
-        return null;
24
+
25
+        return array[1];
21
     }
26
     }
22
 
27
 
23
     /**
28
     /**
25
      * @return last element in specified array
30
      * @return last element in specified array
26
      */ // TODO
31
      */ // TODO
27
     public static String getLastElement(String[] array) {
32
     public static String getLastElement(String[] array) {
28
-        return null;
33
+
34
+        return array[(array.length - 1)];
29
     }
35
     }
30
 
36
 
31
     /**
37
     /**
33
      * @return second to last element in specified array
39
      * @return second to last element in specified array
34
      */ // TODO
40
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
41
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
42
+        return array[(array.length - 2)];
37
     }
43
     }
38
 
44
 
39
     /**
45
     /**
42
      * @return true if the array contains the specified `value`
48
      * @return true if the array contains the specified `value`
43
      */ // TODO
49
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
50
     public static boolean contains(String[] array, String value) {
51
+
52
+        for (int i = 0; i < array.length; i++)
53
+            if (value.equals(array[i])) {
54
+                return true;
55
+            }
45
         return false;
56
         return false;
46
     }
57
     }
47
 
58
 
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[] newStr = new String[array.length];
65
+        int n = 0;
66
+        for (int i = array.length - 1; i >= 0; i--) {
67
+            newStr[n] = array[i];
68
+            n++;
69
+        }
70
+        return newStr;
54
     }
71
     }
55
 
72
 
56
     /**
73
     /**
58
      * @return true if the order of the array is the same backwards and forwards
75
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
76
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
77
     public static boolean isPalindromic(String[] array) {
61
-        return false;
78
+        String[] newStr = Arrays.copyOf(array, array.length);
79
+        boolean same = false;
80
+        int n = 0;
81
+        for (int i = array.length - 1; i >= 0; i--) {
82
+            if (array[i] != newStr[n]) {
83
+                return false;
84
+            }
85
+            n++;
86
+        }
87
+        return true;
62
     }
88
     }
63
 
89
 
64
     /**
90
     /**
66
      * @return true if each letter in the alphabet has been used in the array
92
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
93
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
94
     public static boolean isPangramic(String[] array) {
95
+
96
+        String str = String.join(",", array);
97
+        String alphaStr = "abcdefghijklmnopqrstuvwxyz";
98
+        char[] alphaArray = alphaStr.toCharArray();
99
+        char[] charArray = str.toCharArray();
100
+
101
+        int count = 0;
102
+        for (int i = 0; i < alphaArray.length; i++) {
103
+            for (int j = 0; j < charArray.length; j++) {
104
+                if (alphaArray[i] == charArray[j]) {
105
+                    count++;
106
+                    //break;
107
+                }
108
+            }
109
+        }
110
+        if (count >= 26) {
111
+            return true;
112
+        }
69
         return false;
113
         return false;
70
     }
114
     }
71
 
115
 
75
      * @return number of occurrences the specified `value` has occurred
119
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
120
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
121
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
122
+        int counter = 0;
123
+        for (int i = 0; i < array.length; i++) {
124
+            if (array[i] == value) {
125
+                counter++;
126
+            }
127
+        }
128
+
129
+        return counter;
79
     }
130
     }
80
 
131
 
81
     /**
132
     /**
84
      * @return array with identical contents excluding values of `value`
135
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
136
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
137
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
138
+        String[] newStr = new String[array.length];
139
+        int n = 0;
140
+        for (int i = 0; i < array.length; i++) {
141
+            if (array[i] != valueToRemove) {
142
+                newStr[n] = array[i];
143
+                n++;
144
+            }
145
+
146
+        }
147
+        return Arrays.copyOf(newStr, n);
148
+
149
+
88
     }
150
     }
89
 
151
 
90
     /**
152
     /**
92
      * @return array of Strings with consecutive duplicates removes
154
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
155
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
156
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
157
+        int counter = 0;
158
+        ArrayList<String> temp = new ArrayList<>();
159
+        temp.add(array[0]);
160
+
161
+        for (int i=1;i<array.length;i++) {
162
+            if (!array[i].equals(temp.get(counter))) {
163
+                temp.add(array[i]);
164
+                counter++;
165
+            }
166
+
167
+        }
168
+        String[] answer = new String[temp.size()];
169
+        temp.toArray(answer);
170
+
171
+        return answer;
96
     }
172
     }
97
 
173
 
174
+
175
+
98
     /**
176
     /**
99
      * @param array array of chars
177
      * @param array array of chars
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
178
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
179
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
180
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105
 
181
 
182
+        int counter = 0;
183
+        ArrayList<String> temp = new ArrayList<>();
184
+        String str = array[0];
185
+
186
+
187
+        for (int i=1;i<array.length;i++) {
188
+            //if (array[i-1].charAt(0)!=(array[counter].charAt(0))) {
106
 
189
 
190
+            if (array[i].equals(array[i-1])) {
191
+                str=str.concat(array[i]);
192
+
193
+            } else {
194
+                temp.add(str);
195
+                str="";
196
+                str=str.concat(array[i]);
197
+            }
198
+        }
199
+        
200
+        temp.add(str);
201
+
202
+         array = new String[temp.size()];
203
+        temp.toArray(array);
204
+
205
+        return array;
206
+    }
107
 }
207
 }
208
+
209
+