diff --git a/pom.xml b/pom.xml index d10c35e..0285ce3 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ com.zipcodewilmington.labs arrayutils 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + junit diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..07906fb 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 java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * 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,7 +46,13 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { - return false; + ArrayList arrayToList = new ArrayList(Arrays.asList(array)); + if (arrayToList.contains(value)) { + return true; + } + else { + return false; + } } /** @@ -50,7 +60,12 @@ 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; + ArrayList arrayList = new ArrayList(); + for (int i = array.length - 1; i >= 0; i--) { + arrayList.add(array[i]); + } + String[] backToArray = arrayList.toArray(new String[0]); + return backToArray; } /** @@ -58,7 +73,17 @@ 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; + ArrayList arrayList = new ArrayList(); + for (int i = array.length - 1; i >= 0; i--) { + arrayList.add(array[i]); + } + String[] backToArray = arrayList.toArray(new String[0]); + if (Arrays.equals(backToArray,array)) { + return true; + } + else { + return false; + } } /** @@ -66,7 +91,16 @@ 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; + String[] alphabet = new String[] {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; + List arrayToList = new ArrayList(Arrays.asList(array)); + String arrayToString = arrayToList.toString(); + arrayToString = arrayToString.replaceAll(",", "").replaceAll(" ","").toLowerCase(); + for (int i = 0; i < alphabet.length; i++) { + if (!arrayToString.contains(alphabet[i])) { + return false; + } + } + return true; } /** @@ -75,7 +109,13 @@ 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 occurrences = 0; + for (String word : array) { + if (word.equals(value)) { + occurrences++; + } + } + return occurrences; } /** @@ -84,7 +124,11 @@ 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 arrayToList = new ArrayList(Arrays.asList(array)); + arrayToList.remove(valueToRemove); + String[] backToArray = new String[arrayToList.size()]; + backToArray = arrayToList.toArray(backToArray); + return backToArray; } /** @@ -92,7 +136,16 @@ 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; + List arrayList = new ArrayList(); + arrayList.add(array[0]); + int indexForList = 0; // Have to create an index for the list b/c when it removes a dup, the size of the list doesn't increase + for (int i = 1; i < array.length; i++) { + if (!arrayList.get(indexForList).contains(array[i])) { + arrayList.add(array[i]); + indexForList++; + } + } + return arrayList.toArray(new String[0]); } /** @@ -100,7 +153,19 @@ 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 arrayList = new ArrayList(); + arrayList.add(array[0]); + int indexForList = 0; // Have to create an index for the list b/c when it concats a dup, the size of the list doesn't increase + for (int i = 1; i < array.length; i++) { + if (arrayList.get(indexForList).contains(array[i])) { + arrayList.set(indexForList, (arrayList.get(indexForList) + array[i])); + } + else { + arrayList.add(array[i]); + indexForList++; + } + } + return arrayList.toArray(new String[0]); }