瀏覽代碼

completed arrays

jtith5 8 年之前
父節點
當前提交
e151450834
共有 1 個文件被更改,包括 136 次插入13 次删除
  1. 136
    13
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 136
- 13
src/main/java/com/zipcodewilmington/StringArrayUtils.java 查看文件

@@ -1,15 +1,21 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+
3 9
 /**
4 10
  * Created by leon on 1/29/18.
5 11
  */
6
-public class StringArrayUtils {
12
+public class  StringArrayUtils {
7 13
     /**
8 14
      * @param array array of String objects
9 15
      * @return first element of specified array
10 16
      */ // TODO
11 17
     public static String getFirstElement(String[] array) {
12
-        return null;
18
+        return array[0];
13 19
     }
14 20
 
15 21
     /**
@@ -17,15 +23,16 @@ public class StringArrayUtils {
17 23
      * @return second element in specified array
18 24
      */
19 25
     public static String getSecondElement(String[] array) {
20
-        return null;
26
+        return array[1];
21 27
     }
22 28
 
23 29
     /**
24 30
      * @param array array of String objects
25 31
      * @return last element in specified array
26 32
      */ // TODO
27
-    public static String getLastElement(String[] array) {
28
-        return null;
33
+    public static String getLastElement(String[] array)
34
+    {
35
+        return array[array.length-1];
29 36
     }
30 37
 
31 38
     /**
@@ -33,7 +40,7 @@ public class StringArrayUtils {
33 40
      * @return second to last element in specified array
34 41
      */ // TODO
35 42
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
43
+        return array[array.length-2];
37 44
     }
38 45
 
39 46
     /**
@@ -42,6 +49,11 @@ public class StringArrayUtils {
42 49
      * @return true if the array contains the specified `value`
43 50
      */ // TODO
44 51
     public static boolean contains(String[] array, String value) {
52
+        for (int i =0; i<array.length;i++){
53
+            if (array[i].equals(value)){
54
+                return true;
55
+            }
56
+        }
45 57
         return false;
46 58
     }
47 59
 
@@ -50,7 +62,13 @@ public class StringArrayUtils {
50 62
      * @return an array with identical contents in reverse order
51 63
      */ // TODO
52 64
     public static String[] reverse(String[] array) {
53
-        return null;
65
+        String[] reverseArray = new String[array.length];
66
+        int counter = 0;
67
+        for (int i = array.length-1; i>=0; i--){
68
+              reverseArray[counter] = array[i];
69
+                counter++;
70
+        }
71
+        return reverseArray;
54 72
     }
55 73
 
56 74
     /**
@@ -58,7 +76,13 @@ public class StringArrayUtils {
58 76
      * @return true if the order of the array is the same backwards and forwards
59 77
      */ // TODO
60 78
     public static boolean isPalindromic(String[] array) {
61
-        return false;
79
+        String[] reverseArray = new String[array.length];
80
+        int counter = 0;
81
+        for (int i = array.length-1; i>=0; i--){
82
+            reverseArray[counter] = array[i];
83
+            counter++;
84
+        }
85
+        return Arrays.deepEquals(array,reverseArray);
62 86
     }
63 87
 
64 88
     /**
@@ -66,7 +90,17 @@ public class StringArrayUtils {
66 90
      * @return true if each letter in the alphabet has been used in the array
67 91
      */ // TODO
68 92
     public static boolean isPangramic(String[] array) {
69
-        return false;
93
+        StringBuilder sb = new StringBuilder();
94
+        for (String var:array){
95
+            sb.append(var);
96
+        } String str = sb.toString();
97
+
98
+        return str.toLowerCase()
99
+                .replaceAll("[^a-z]", "")
100
+                .replaceAll("(.)(?=.*\\1)", "")
101
+                .length() == 26;
102
+
103
+
70 104
     }
71 105
 
72 106
     /**
@@ -75,7 +109,13 @@ public class StringArrayUtils {
75 109
      * @return number of occurrences the specified `value` has occurred
76 110
      */ // TODO
77 111
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
112
+        int counter = 0;
113
+        for (String var: array){
114
+            if (var.equals(value)){
115
+                counter++;
116
+            }
117
+        }
118
+        return counter;
79 119
     }
80 120
 
81 121
     /**
@@ -84,7 +124,15 @@ public class StringArrayUtils {
84 124
      * @return array with identical contents excluding values of `value`
85 125
      */ // TODO
86 126
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
127
+        ArrayList<String> valueRemoved = new ArrayList<String>();
128
+        for (String var: array){
129
+            if (!var.equals(valueToRemove)){
130
+                valueRemoved.add(var);
131
+            }
132
+        }
133
+        String[] strArray = new String[valueRemoved.size()];
134
+        strArray = valueRemoved.toArray(strArray);
135
+        return strArray;
88 136
     }
89 137
 
90 138
     /**
@@ -92,7 +140,15 @@ public class StringArrayUtils {
92 140
      * @return array of Strings with consecutive duplicates removes
93 141
      */ // TODO
94 142
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
143
+        ArrayList<String> rCDA = new ArrayList<String>();
144
+        rCDA.add(array[0]);
145
+        for (int i = 1;i<array.length;i++){
146
+            if (!array[i].equals(array[i-1])){
147
+                rCDA.add(array[i]);
148
+            }
149
+        } String[] strArray = new String[rCDA.size()];
150
+            strArray = rCDA.toArray(strArray);
151
+        return strArray;
96 152
     }
97 153
 
98 154
     /**
@@ -100,8 +156,75 @@ public class StringArrayUtils {
100 156
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 157
      */ // TODO
102 158
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
159
+        Integer startingPoint = 0;
160
+        ArrayList<String> listToReturn = new ArrayList<String>();
161
+        HashMap<String, Integer> map = determineNumberOfDuplicateCharactersInARow(array, startingPoint);
162
+        String character =  getKey(map);
163
+        Integer integer = getInteger(map);
164
+        startingPoint = integer;
165
+        listToReturn.add(buildString(character, integer));
166
+
167
+
168
+
169
+        System.out.println(listToReturn);
170
+        String[] strArray = new String[listToReturn.size()];
171
+        strArray = listToReturn.toArray(strArray);
172
+        return strArray;
104 173
     }
105 174
 
175
+    private static String buildString(String string, Integer integer) {
176
+        String returnString = "";
177
+        for (int i = 0; i < integer; i++) {
178
+            returnString += string;
179
+        }
180
+        return returnString;
181
+    }
182
+
183
+    private static String getKey(HashMap<String, Integer> map) {
184
+        String string = "";
185
+        for (Map.Entry<String, Integer> entry : map.entrySet()) {
186
+            string = entry.getKey();
187
+        }
188
+        return string;
189
+    }
190
+
191
+    private static Integer getInteger(HashMap<String, Integer> map) {
192
+        Integer integer = 0;
193
+        for (Map.Entry<String, Integer> entry : map.entrySet()) {
194
+            integer = entry.getValue();
195
+        }
196
+        return integer;
197
+    }
198
+
199
+    private static HashMap<String, Integer> determineNumberOfDuplicateCharactersInARow(String[] arrayToFindConsecutiveDuplicatesIn, Integer startingPoint) {
200
+        HashMap<String, Integer> map = new HashMap<String, Integer>();
201
+        ArrayList<String> arrayList = createArrayList(arrayToFindConsecutiveDuplicatesIn);
202
+        String currentCharacter = arrayList.get(startingPoint);
203
+        //System.out.println("current character = " + currentCharacter);
204
+        int numberOfDuplicates = 1;
205
+        for (int i = startingPoint; i < arrayList.size(); i ++) {
206
+            String nextCharacter = null;
207
+            try {
208
+                nextCharacter = arrayList.get(i + 1);
209
+            } catch (IndexOutOfBoundsException e) {
210
+                map.put(currentCharacter, numberOfDuplicates);
211
+            }
212
+            //System.out.println("Comparing " + currentCharacter + " to " + nextCharacter);
213
+            if (!currentCharacter.equals(nextCharacter)) {
214
+                map.put(currentCharacter, numberOfDuplicates);
215
+                return map;
216
+            }
217
+            //System.out.println("Increasing duplicates");
218
+            numberOfDuplicates++;
219
+        }
220
+        return  map;
221
+    }
106 222
 
223
+    private static ArrayList<String> createArrayList(String[] array) {
224
+        ArrayList<String> arrayListToFindConsecutiveDuplicatesIn = new ArrayList<String>();
225
+        for (int i = 0; i < array.length; i++) {
226
+            arrayListToFindConsecutiveDuplicatesIn.add(array[i]);
227
+        }
228
+        return arrayListToFindConsecutiveDuplicatesIn;
229
+    }
107 230
 }