Sfoglia il codice sorgente

passed all of the test.

William Brown 8 anni fa
parent
commit
1a7d01893a
1 ha cambiato i file con 100 aggiunte e 14 eliminazioni
  1. 100
    14
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 100
- 14
src/main/java/com/zipcodewilmington/StringArrayUtils.java Vedi File

1
 package com.zipcodewilmington;
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
 public class StringArrayUtils {
13
 public class StringArrayUtils {
7
     /**
14
     /**
9
      * @return first element of specified array
16
      * @return first element of specified array
10
      */ // TODO
17
      */ // TODO
11
     public static String getFirstElement(String[] array) {
18
     public static String getFirstElement(String[] array) {
12
-        return null;
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
+        return array[1];
21
     }
28
     }
22
 
29
 
23
     /**
30
     /**
25
      * @return last element in specified array
32
      * @return last element in specified array
26
      */ // TODO
33
      */ // TODO
27
     public static String getLastElement(String[] array) {
34
     public static String getLastElement(String[] array) {
28
-        return null;
35
+        return array[array.length -1];
29
     }
36
     }
30
 
37
 
31
     /**
38
     /**
33
      * @return second to last element in specified array
40
      * @return second to last element in specified array
34
      */ // TODO
41
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
42
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
43
+        return array[array.length -2];
37
     }
44
     }
38
 
45
 
39
     /**
46
     /**
42
      * @return true if the array contains the specified `value`
49
      * @return true if the array contains the specified `value`
43
      */ // TODO
50
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
      * @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[] 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
      * @return true if the order of the array is the same backwards and forwards
78
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
79
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
      * @return true if each letter in the alphabet has been used in the array
96
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
97
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
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
      * @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 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
      * @return array with identical contents excluding values of `value`
134
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
135
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @param array array of chars
149
      * @param array array of chars
92
      * @return array of Strings with consecutive duplicates removes
150
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
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
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
167
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
168
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
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