Connor Dunnigan пре 6 година
родитељ
комит
e34a5f88f0
1 измењених фајлова са 101 додато и 13 уклоњено
  1. 101
    13
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 101
- 13
src/main/java/com/zipcodewilmington/StringArrayUtils.java Прегледај датотеку

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