Demetrius Murray 8 år sedan
förälder
incheckning
fdbe3d0b29
1 ändrade filer med 103 tillägg och 12 borttagningar
  1. 103
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 103
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java Visa fil

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
+import java.util.Arrays;
4
+
5
+
3
 /**
6
 /**
4
  * Created by leon on 1/29/18.
7
  * Created by leon on 1/29/18.
5
  */
8
  */
9
      * @return first element of specified array
12
      * @return first element of specified array
10
      */ // TODO
13
      */ // TODO
11
     public static String getFirstElement(String[] array) {
14
     public static String getFirstElement(String[] array) {
12
-        return null;
15
+        return array[0];
13
     }
16
     }
14
 
17
 
15
     /**
18
     /**
17
      * @return second element in specified array
20
      * @return second element in specified array
18
      */
21
      */
19
     public static String getSecondElement(String[] array) {
22
     public static String getSecondElement(String[] array) {
20
-        return null;
23
+        return array[1];
21
     }
24
     }
22
 
25
 
23
     /**
26
     /**
25
      * @return last element in specified array
28
      * @return last element in specified array
26
      */ // TODO
29
      */ // TODO
27
     public static String getLastElement(String[] array) {
30
     public static String getLastElement(String[] array) {
28
-        return null;
31
+        return array[array.length-1];
29
     }
32
     }
30
 
33
 
31
     /**
34
     /**
33
      * @return second to last element in specified array
36
      * @return second to last element in specified array
34
      */ // TODO
37
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
38
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
39
+        return array[array.length-2];
37
     }
40
     }
38
 
41
 
39
     /**
42
     /**
42
      * @return true if the array contains the specified `value`
45
      * @return true if the array contains the specified `value`
43
      */ // TODO
46
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
      * @return an array with identical contents in reverse order
61
      * @return an array with identical contents in reverse order
51
      */ // TODO
62
      */ // TODO
52
     public static String[] reverse(String[] array) {
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
      * @return true if the order of the array is the same backwards and forwards
77
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
78
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
      * @return true if each letter in the alphabet has been used in the array
94
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
95
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
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
      * @return number of occurrences the specified `value` has occurred
125
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
126
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
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
      * @return array with identical contents excluding values of `value`
140
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
141
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @return array of Strings with consecutive duplicates removes
160
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
161
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
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
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
179
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
180
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
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