diff --git a/pom.xml b/pom.xml index d10c35e..e80e4ef 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,10 @@ 4.12 + + 1.8 + 1.8 + \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..281a116 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,9 @@ package com.zipcodewilmington; +import javax.print.DocFlavor; +import java.util.ArrayList; +import java.util.Arrays; + /** * Created by leon on 1/29/18. */ @@ -25,7 +29,7 @@ public static String getSecondElement(String[] array) { * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { - return null; + return array[array.length - 1]; } /** @@ -33,7 +37,7 @@ public static String getLastElement(String[] array) { * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { - return null; + return array[array.length - 2]; } /** @@ -42,6 +46,10 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { + for (String s : array) { + if (s.equals(value)) + return true; + } return false; } @@ -50,7 +58,11 @@ public static boolean contains(String[] array, String value) { * @return an array with identical contents in reverse order */ // TODO public static String[] reverse(String[] array) { - return null; + String[] newArray = new String[array.length]; + for (int i = array.length - 1, j = 0; i >= 0; i--, j++) { + newArray[j] = array[i]; + } + return newArray; } /** @@ -58,7 +70,14 @@ public static String[] reverse(String[] array) { * @return true if the order of the array is the same backwards and forwards */ // TODO public static boolean isPalindromic(String[] array) { - return false; + String[] reversed = reverse(array); + boolean isPalindrome = true; + for (int i = 0; i < array.length; i++) { + if(!array[i].equals(reversed[i])) { + isPalindrome = false; + } + } + return isPalindrome; } /** @@ -66,7 +85,17 @@ public static boolean isPalindromic(String[] array) { * @return true if each letter in the alphabet has been used in the array */ // TODO public static boolean isPangramic(String[] array) { - return false; + boolean[] letters = new boolean[26]; + for (String s : array) { + for (char c : s.toCharArray()) { + if (c >= 'A' && c <= 'Z') letters[c - 'A'] = true; + else if (c >= 'a' && c <= 'z') letters[c - 'a'] = true; + } + } + for (boolean b : letters) { + if(b == false) return false; + } + return true; } /** @@ -75,7 +104,11 @@ public static boolean isPangramic(String[] array) { * @return number of occurrences the specified `value` has occurred */ // TODO public static int getNumberOfOccurrences(String[] array, String value) { - return 0; + int count = 0; + for(String s : array) { + if(s.equals(value)) count++; + } + return count; } /** @@ -84,7 +117,9 @@ public static int getNumberOfOccurrences(String[] array, String value) { * @return array with identical contents excluding values of `value` */ // TODO public static String[] removeValue(String[] array, String valueToRemove) { - return null; + ArrayList list = new ArrayList(Arrays.asList(array)); + list.remove(valueToRemove); + return list.toArray(new String[list.size()]); } /** @@ -92,7 +127,17 @@ public static String[] removeValue(String[] array, String valueToRemove) { * @return array of Strings with consecutive duplicates removes */ // TODO public static String[] removeConsecutiveDuplicates(String[] array) { - return null; + ArrayList list = new ArrayList(); + + for (String s : array) { + if(list.size() == 0) list.add(s); + else if(!list.get(list.size() - 1).equals(s)) { + list.add(s); + System.out.println(s); + } + } + + return list.toArray(new String[list.size()]); } /** @@ -100,7 +145,23 @@ public static String[] removeConsecutiveDuplicates(String[] array) { * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings */ // TODO public static String[] packConsecutiveDuplicates(String[] array) { - return null; + ArrayList list = new ArrayList(); + String currentWord = ""; + + for (String s : array) { + if(list.size() == 0) { + list.add(s); + currentWord = s; + } + else if(currentWord.equals(s)) { + list.set(list.size() - 1, list.get(list.size() - 1) + s); + } + else { + list.add(s); + currentWord = s; + } + } + return list.toArray(new String[list.size()]); }