Quellcode durchsuchen

all tests passed

Saurav Kamath vor 8 Jahren
Ursprung
Commit
f956d9c690
2 geänderte Dateien mit 100 neuen und 22 gelöschten Zeilen
  1. 12
    0
      pom.xml
  2. 88
    22
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 12
- 0
pom.xml Datei anzeigen

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington.labs</groupId>
8 8
     <artifactId>arrayutils</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>6</source>
17
+                    <target>6</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 88
- 22
src/main/java/com/zipcodewilmington/StringArrayUtils.java Datei anzeigen

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