ソースを参照

All Tests Pass

Nicholas Satinover 6 年 前
コミット
e7e027a78e
共有1 個のファイルを変更した89 個の追加12 個の削除を含む
  1. 89
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 89
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java ファイルの表示

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
+import com.sun.xml.internal.fastinfoset.util.StringArray;
4
+
5
+import java.util.ArrayList;
6
+import java.util.Arrays;
7
+
3
 /**
8
 /**
4
  * Created by leon on 1/29/18.
9
  * Created by leon on 1/29/18.
5
  */
10
  */
9
      * @return first element of specified array
14
      * @return first element of specified array
10
      */ // TODO
15
      */ // TODO
11
     public static String getFirstElement(String[] array) {
16
     public static String getFirstElement(String[] array) {
12
-        return null;
17
+        return array[0];
13
     }
18
     }
14
 
19
 
15
     /**
20
     /**
17
      * @return second element in specified array
22
      * @return second element in specified array
18
      */
23
      */
19
     public static String getSecondElement(String[] array) {
24
     public static String getSecondElement(String[] array) {
20
-        return null;
25
+        return array[1];
21
     }
26
     }
22
 
27
 
23
     /**
28
     /**
25
      * @return last element in specified array
30
      * @return last element in specified array
26
      */ // TODO
31
      */ // TODO
27
     public static String getLastElement(String[] array) {
32
     public static String getLastElement(String[] array) {
28
-        return null;
33
+        return array[array.length - 1];
29
     }
34
     }
30
 
35
 
31
     /**
36
     /**
33
      * @return second to last element in specified array
38
      * @return second to last element in specified array
34
      */ // TODO
39
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
40
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
41
+        return array[array.length - 2];
37
     }
42
     }
38
 
43
 
39
     /**
44
     /**
42
      * @return true if the array contains the specified `value`
47
      * @return true if the array contains the specified `value`
43
      */ // TODO
48
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
49
     public static boolean contains(String[] array, String value) {
45
-        return false;
50
+        boolean contains = false;
51
+        for (String s: array) {
52
+            if (s.equals(value)){
53
+                contains = true;
54
+            }
55
+        }
56
+        return contains;
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
+        String[] strArrReversed = new String[array.length];
65
+        int x = 0;
66
+        for (int i = array.length - 1; i >= 0; i--){
67
+            strArrReversed[x] = (array[i]);
68
+            x++;
69
+        }
70
+
71
+        return strArrReversed;
54
     }
72
     }
55
 
73
 
56
     /**
74
     /**
58
      * @return true if the order of the array is the same backwards and forwards
76
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
77
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
78
     public static boolean isPalindromic(String[] array) {
61
-        return false;
79
+        boolean isAPalindrom = true;
80
+        int countRev = array.length - 1;
81
+        for (int countUp = 0; countUp < array.length; countUp++){
82
+            if (array[countUp].equals(array[countRev]) == false){
83
+                isAPalindrom = false;
84
+            }
85
+            countRev--;
86
+        }
87
+        return isAPalindrom;
62
     }
88
     }
63
 
89
 
64
     /**
90
     /**
66
      * @return true if each letter in the alphabet has been used in the array
92
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
93
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
94
     public static boolean isPangramic(String[] array) {
69
-        return false;
95
+        boolean isAPanGramic = true;
96
+        String strLower = "";
97
+    //Array - toLowerCase - Concat
98
+        for (String var : array) {
99
+            strLower = strLower.concat(var.toLowerCase());
100
+        }
101
+    //loop through ANSI lower case values a-z
102
+        // IF! contains - boolean set to false
103
+        for(int i = 97; i <= 122; i++){
104
+            if(!strLower.contains(Character.toString((char)i))){
105
+                isAPanGramic = false;
106
+            }
107
+        }
108
+        return isAPanGramic;
70
     }
109
     }
71
 
110
 
72
     /**
111
     /**
75
      * @return number of occurrences the specified `value` has occurred
114
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
115
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
116
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
117
+        int count = 0;
118
+        for (String arrayVar: array) {
119
+            if (arrayVar.equals(value)){
120
+                count++;
121
+            }
122
+        }
123
+        return count;
79
     }
124
     }
80
 
125
 
81
     /**
126
     /**
84
      * @return array with identical contents excluding values of `value`
129
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
130
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
131
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
132
+        ArrayList<String> arrayList = new ArrayList<String>();
133
+        for (int i = 0; i < array.length; i++) {
134
+            if(!array[i].equals(valueToRemove)) {
135
+                arrayList.add(array[i]);
136
+            }
137
+        }
138
+        String[] strReturn = arrayList.toArray(new String[arrayList.size()]);
139
+
140
+        return strReturn;
88
     }
141
     }
89
 
142
 
90
     /**
143
     /**
92
      * @return array of Strings with consecutive duplicates removes
145
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
146
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
147
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
148
+        ArrayList<String> arrayList = new ArrayList<String>();
149
+        arrayList.add(array[0]);
150
+        for (int i = 0; i < array.length - 1; i++) {
151
+            if (!array[i].equals(array[i+1])) {
152
+                arrayList.add(array[i+1]);
153
+            }
154
+        }
155
+        String[] strReturn = arrayList.toArray(new String[arrayList.size()]);
156
+
157
+        return strReturn;
96
     }
158
     }
97
 
159
 
98
     /**
160
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
162
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
163
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
164
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
165
+        ArrayList<String> arrayList = new ArrayList<String>();
166
+        arrayList.add(array[0]);
167
+        for(int i = 1; i < array.length; i++)
168
+        {
169
+            if(array[i].equals(array[i-1]))
170
+            {
171
+                String str = arrayList.get(arrayList.size() - 1).concat(array[i]);
172
+                arrayList.set(arrayList.size() - 1, str);
173
+            }
174
+            else
175
+            {
176
+                arrayList.add(array[i]);
177
+            }
178
+        }
179
+        String[] strReturn = arrayList.toArray(new String[arrayList.size()]);
180
+        return strReturn;
104
     }
181
     }
105
 
182
 
106
 
183