Browse Source

Still need to work on packConsecutiveDuplicates!

Kate Moore 8 years ago
parent
commit
cc7e6c349e
1 changed files with 105 additions and 12 deletions
  1. 105
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 105
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java View File

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
+import com.sun.deploy.util.ArrayUtil;
4
+
5
+import java.lang.reflect.Array;
6
+import java.util.ArrayList;
7
+
3
 /**
8
 /**
4
  * Created by leon on 1/29/18.
9
  * Created by leon on 1/29/18.
5
  */
10
  */
6
 public class StringArrayUtils {
11
 public class StringArrayUtils {
12
+
7
     /**
13
     /**
8
      * @param array array of String objects
14
      * @param array array of String objects
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) {
55
+        for (String n : array) {
56
+            if (value == n)
57
+                return true;
58
+        }
45
         return false;
59
         return false;
46
     }
60
     }
47
 
61
 
50
      * @return an array with identical contents in reverse order
64
      * @return an array with identical contents in reverse order
51
      */ // TODO
65
      */ // TODO
52
     public static String[] reverse(String[] array) {
66
     public static String[] reverse(String[] array) {
53
-        return null;
67
+        String a[] = new String[array.length];
68
+        for (int i = 0; i < a.length; i++) {
69
+            a[i] = array[array.length - 1 - i];
70
+        }
71
+        return a;
54
     }
72
     }
55
 
73
 
56
     /**
74
     /**
58
      * @return true if the order of the array is the same backwards and forwards
76
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
77
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
78
     public static boolean isPalindromic(String[] array) {
61
-        return false;
79
+        for (int i = 0; i < array.length / 2; i++) {
80
+            if (array[i] != array[array.length - 1 - i])
81
+                return false;
82
+
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) {
92
+
93
+        String[] arr = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
94
+
95
+        int count = 0;
96
+
97
+        for (int i = 0; i < arr.length; i++) {
98
+            for (int j = 0; j < array.length; j++) {
99
+                if (array[j].contains(arr[i])) {
100
+                    count++;
101
+                }
102
+
103
+            }
104
+        }
105
+        if (count >= 26) {
106
+            return true;
107
+        }
108
+
69
         return false;
109
         return false;
70
     }
110
     }
71
 
111
 
75
      * @return number of occurrences the specified `value` has occurred
115
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
116
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
117
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
118
+        int count = 0;
119
+        for (String n : array) {
120
+            if (n.equals(value)) {
121
+                count += 1;
122
+            }
123
+        }
124
+
125
+        return count;
126
+
79
     }
127
     }
80
 
128
 
81
     /**
129
     /**
84
      * @return array with identical contents excluding values of `value`
132
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
133
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
134
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
135
+
136
+        ArrayList<String> list = new ArrayList<String>();
137
+
138
+        for (int i = 0; i < array.length; i++) {
139
+            if (array[i] != (valueToRemove)) {
140
+                list.add(array[i]);
141
+            }
142
+
143
+        }
144
+        String[] newStr = new String[list.size()];
145
+        for (int i = 0; i < list.size(); i++) {
146
+            newStr[i] = list.get(i);
147
+        }
148
+
149
+        return newStr;
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
+
158
+        ArrayList<String> list = new ArrayList<String>();
159
+
160
+
161
+        for (int i = 0; i < array.length; i++) {
162
+            if (i + 1 < array.length && array[i].equals(array[i + 1])) {
163
+            } else {
164
+                list.add(array[i]);
165
+            }
166
+        }
167
+        String[] result = list.toArray(new String[list.size()]);
168
+        return result;
96
     }
169
     }
97
 
170
 
98
     /**
171
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
173
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
174
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
175
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105
 
176
 
177
+       /* int countDups = 0;
178
+
179
+        for (int i = 0; i< array.length; i++) {
180
+            for(int j = i+1; j <array.length; j++) {
181
+                if(array[i].equals(array[j])) {
182
+                   countDups+=1;
183
+                }
184
+            }
185
+        }
186
+
187
+        String arr2[] = new String[array.length - countDups];
188
+
189
+        for (String element: array) {
190
+            if( element.equals(element)) {
106
 
191
 
107
-}
192
+            }
193
+        }
194
+
195
+        return arr2
196
+    }*/
197
+
198
+        return null;
199
+    }
200
+}