Demetrius Murray il y a 8 ans
Parent
révision
fdbe3d0b29
1 fichiers modifiés avec 103 ajouts et 12 suppressions
  1. 103
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 103
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java Voir le fichier

@@ -1,5 +1,8 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import java.util.Arrays;
4
+
5
+
3 6
 /**
4 7
  * Created by leon on 1/29/18.
5 8
  */
@@ -9,7 +12,7 @@ public class StringArrayUtils {
9 12
      * @return first element of specified array
10 13
      */ // TODO
11 14
     public static String getFirstElement(String[] array) {
12
-        return null;
15
+        return array[0];
13 16
     }
14 17
 
15 18
     /**
@@ -17,7 +20,7 @@ public class StringArrayUtils {
17 20
      * @return second element in specified array
18 21
      */
19 22
     public static String getSecondElement(String[] array) {
20
-        return null;
23
+        return array[1];
21 24
     }
22 25
 
23 26
     /**
@@ -25,7 +28,7 @@ public class StringArrayUtils {
25 28
      * @return last element in specified array
26 29
      */ // TODO
27 30
     public static String getLastElement(String[] array) {
28
-        return null;
31
+        return array[array.length-1];
29 32
     }
30 33
 
31 34
     /**
@@ -33,7 +36,7 @@ public class StringArrayUtils {
33 36
      * @return second to last element in specified array
34 37
      */ // TODO
35 38
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
39
+        return array[array.length-2];
37 40
     }
38 41
 
39 42
     /**
@@ -42,7 +45,15 @@ public class StringArrayUtils {
42 45
      * @return true if the array contains the specified `value`
43 46
      */ // TODO
44 47
     public static boolean contains(String[] array, String value) {
45
-        return false;
48
+        boolean result = false;
49
+
50
+        for(String str:array){
51
+            if(str.equals(value)){
52
+                result = true;
53
+                break;
54
+            }
55
+        }
56
+        return result;
46 57
     }
47 58
 
48 59
     /**
@@ -50,7 +61,15 @@ public class StringArrayUtils {
50 61
      * @return an array with identical contents in reverse order
51 62
      */ // TODO
52 63
     public static String[] reverse(String[] array) {
53
-        return null;
64
+
65
+        String[] result = new String[array.length];
66
+        int j = 0;
67
+
68
+        for(int i = array.length-1; i>=0; i--){
69
+            result[j] = array[i];
70
+            j++;
71
+        }
72
+        return result;
54 73
     }
55 74
 
56 75
     /**
@@ -58,7 +77,16 @@ public class StringArrayUtils {
58 77
      * @return true if the order of the array is the same backwards and forwards
59 78
      */ // TODO
60 79
     public static boolean isPalindromic(String[] array) {
61
-        return false;
80
+        boolean result = true;
81
+        int j = array.length-1;
82
+
83
+        for(String str:array){
84
+            if(!str.equals(array[j])){
85
+                result = false;
86
+            }
87
+            j--;
88
+        }
89
+        return result;
62 90
     }
63 91
 
64 92
     /**
@@ -66,7 +94,29 @@ public class StringArrayUtils {
66 94
      * @return true if each letter in the alphabet has been used in the array
67 95
      */ // TODO
68 96
     public static boolean isPangramic(String[] array) {
69
-        return false;
97
+        char[] alphabet = new char[26]; //string of alphabet characters
98
+        alphabet[0] = 'a';
99
+        for(int i = 0; i < 26; i++){
100
+            alphabet[i] = (char) ('a'+ i);
101
+        }
102
+
103
+        boolean result = true; //beginning with default true statement
104
+
105
+        StringBuilder sb = new StringBuilder();
106
+        for(String str : array){
107
+            sb.append(str.toLowerCase());
108
+        }
109
+
110
+        String arrStr = sb.toString();
111
+
112
+        for(char letter: alphabet){
113
+            if(!arrStr.contains(Character.toString(letter).toLowerCase())){
114
+                result = false;
115
+                break;
116
+            }
117
+        }
118
+
119
+        return result;
70 120
     }
71 121
 
72 122
     /**
@@ -75,7 +125,13 @@ public class StringArrayUtils {
75 125
      * @return number of occurrences the specified `value` has occurred
76 126
      */ // TODO
77 127
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
128
+        int i = 0;
129
+        for(String str : array){
130
+            if(str.contains(value)){
131
+                i++;
132
+            }
133
+        }
134
+        return i;
79 135
     }
80 136
 
81 137
     /**
@@ -84,7 +140,19 @@ public class StringArrayUtils {
84 140
      * @return array with identical contents excluding values of `value`
85 141
      */ // TODO
86 142
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
143
+        String[] arr2 = new String[array.length-1];
144
+        int j = 0;
145
+
146
+        for(int i=0; i<array.length; i++){
147
+            if(array[i].equals(valueToRemove)){
148
+                continue;
149
+            }
150
+
151
+            arr2[j] = array[i];
152
+            j++;
153
+        }
154
+
155
+        return arr2;
88 156
     }
89 157
 
90 158
     /**
@@ -92,7 +160,18 @@ public class StringArrayUtils {
92 160
      * @return array of Strings with consecutive duplicates removes
93 161
      */ // TODO
94 162
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
163
+        String lastWord = "";
164
+
165
+        StringBuilder sb = new StringBuilder();
166
+        for(String str : array){
167
+            if(!str.equals(lastWord)) {
168
+                sb.append(str + "-");
169
+            }
170
+            lastWord = str;
171
+        }
172
+
173
+        String[] arr2 = sb.toString().split("-");
174
+        return arr2;
96 175
     }
97 176
 
98 177
     /**
@@ -100,7 +179,19 @@ public class StringArrayUtils {
100 179
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 180
      */ // TODO
102 181
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
182
+        StringBuilder sb = new StringBuilder(array[0]);
183
+        String lastWord = array[0];
184
+
185
+        for(int i=1; i<array.length; i++){
186
+            if(!array[i].equals(lastWord)){
187
+                sb.append("-");
188
+            }
189
+            sb.append(array[i]);
190
+            lastWord = array[i];
191
+        }
192
+
193
+        String[] result = sb.toString().split("-");
194
+        return result;
104 195
     }
105 196
 
106 197