Quellcode durchsuchen

passed all of the test.

William Brown vor 8 Jahren
Ursprung
Commit
1a7d01893a
1 geänderte Dateien mit 100 neuen und 14 gelöschten Zeilen
  1. 100
    14
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 100
- 14
src/main/java/com/zipcodewilmington/StringArrayUtils.java Datei anzeigen

@@ -1,7 +1,14 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import com.sun.deploy.util.ArrayUtil;
4
+import com.sun.tools.javac.util.ArrayUtils;
5
+import java.lang.String;
6
+import java.lang.reflect.Array;
7
+import java.util.ArrayList;
8
+import java.util.Arrays;
9
+
3 10
 /**
4
- * Created by leon on 1/29/18.
11
+ * Edited by William 10.24.2018.
5 12
  */
6 13
 public class StringArrayUtils {
7 14
     /**
@@ -9,7 +16,7 @@ public class StringArrayUtils {
9 16
      * @return first element of specified array
10 17
      */ // TODO
11 18
     public static String getFirstElement(String[] array) {
12
-        return null;
19
+        return array[0];
13 20
     }
14 21
 
15 22
     /**
@@ -17,7 +24,7 @@ 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
+        return array[1];
21 28
     }
22 29
 
23 30
     /**
@@ -25,7 +32,7 @@ public class StringArrayUtils {
25 32
      * @return last element in specified array
26 33
      */ // TODO
27 34
     public static String getLastElement(String[] array) {
28
-        return null;
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,7 +49,14 @@ 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) {
45
-        return false;
52
+        //Arrays.asList(array).indexOf(value);
53
+        boolean output = false;
54
+        for(int i = 0; i< array.length; i++){
55
+            if(array[i].equals(value)){
56
+                output = true;
57
+            }
58
+        }
59
+        return output;
46 60
     }
47 61
 
48 62
     /**
@@ -50,7 +64,13 @@ public class StringArrayUtils {
50 64
      * @return an array with identical contents in reverse order
51 65
      */ // TODO
52 66
     public static String[] reverse(String[] array) {
53
-        return null;
67
+        String[] output = new String[array.length];
68
+        int j = 0;
69
+        for(int i = array.length - 1; i >= 0; i--){
70
+            output[j] = array[i];
71
+            j++;
72
+        }
73
+        return output;
54 74
     }
55 75
 
56 76
     /**
@@ -58,7 +78,17 @@ public class StringArrayUtils {
58 78
      * @return true if the order of the array is the same backwards and forwards
59 79
      */ // TODO
60 80
     public static boolean isPalindromic(String[] array) {
61
-        return false;
81
+        boolean Palindromic = false;
82
+        String[] newArray = reverse(array);
83
+        //reverse(array);
84
+        for(int i = 0; i < array.length; i++) {
85
+            if (newArray[i] == array[i]) {
86
+                Palindromic = true;
87
+            } else if(newArray[i] != array[i]){
88
+                Palindromic = false;
89
+            }
90
+        }
91
+        return Palindromic;
62 92
     }
63 93
 
64 94
     /**
@@ -66,7 +96,21 @@ public class StringArrayUtils {
66 96
      * @return true if each letter in the alphabet has been used in the array
67 97
      */ // TODO
68 98
     public static boolean isPangramic(String[] array) {
69
-        return false;
99
+        String arrayString = "";
100
+        for(int k = 0; k < array.length; k++){
101
+            arrayString += array[k];
102
+        }
103
+        arrayString = arrayString.toLowerCase();
104
+        String alphabet = "abcdefghijklmnopqrstuvwxyz";
105
+
106
+        for(int i = 0; i < arrayString.length(); i++){
107
+            for(int j = 0; j <= 25; j++){
108
+                if(arrayString.indexOf(alphabet.charAt(j)) == -1){
109
+                    return false;
110
+                }
111
+            }
112
+        }
113
+        return true;
70 114
     }
71 115
 
72 116
     /**
@@ -75,7 +119,13 @@ public class StringArrayUtils {
75 119
      * @return number of occurrences the specified `value` has occurred
76 120
      */ // TODO
77 121
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
122
+        int output = 0;
123
+        for(int i = 0; i< array.length; i++){
124
+            if(array[i].equals(value)){
125
+                output = output + 1;
126
+            }
127
+        }
128
+        return output;
79 129
     }
80 130
 
81 131
     /**
@@ -84,15 +134,32 @@ public class StringArrayUtils {
84 134
      * @return array with identical contents excluding values of `value`
85 135
      */ // TODO
86 136
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
137
+        //int output = 0;
138
+        ArrayList<String> arrayWithout = new ArrayList<String>();
139
+        for(int i = 0; i < array.length; i++) {
140
+            if (!(array[i].equals(valueToRemove))) {
141
+                arrayWithout.add(array[i]);
142
+            }
143
+        }
144
+        String[] finalArray = arrayWithout.toArray(new String[arrayWithout.size()]);
145
+        return finalArray;
88 146
     }
89 147
 
90 148
     /**
91 149
      * @param array array of chars
92 150
      * @return array of Strings with consecutive duplicates removes
93 151
      */ // TODO
94
-    public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
152
+    public static String[] removeConsecutiveDuplicates(String[] array){
153
+        ArrayList<String> arrayWithout = new ArrayList<String>();
154
+        String previous = "";
155
+        for(int i = 0; i < array.length; i++){
156
+            if(!(previous.equals(array[i]))){
157
+                arrayWithout.add(array[i]);
158
+            }
159
+            previous = array[i];
160
+        }
161
+        String[] finalArray = arrayWithout.toArray(new String[arrayWithout.size()]);
162
+        return finalArray;
96 163
     }
97 164
 
98 165
     /**
@@ -100,7 +167,26 @@ public class StringArrayUtils {
100 167
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 168
      */ // TODO
102 169
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
170
+        ArrayList<String> packArray = new ArrayList<String>();
171
+        String previous = array[0];
172
+        String holder = array[0];
173
+        for(int i = 1; i < array.length; i++){
174
+            if(!(previous.equals(array[i]))){
175
+                packArray.add(holder);
176
+                holder = "";
177
+            }
178
+            holder += array[i];
179
+                //if(!holder.equals("")){
180
+                //    packArray.add(holder);
181
+                //    holder = "";
182
+                //}
183
+            previous = array[i];
184
+        }
185
+        packArray.add(holder);
186
+        String[] finalArray = packArray.toArray(new String[packArray.size()]);
187
+        for(int k = 0; k < finalArray.length; k++) {
188
+        }
189
+        return finalArray;
104 190
     }
105 191
 
106 192