diff --git a/pom.xml b/pom.xml index d10c35e..6505d27 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 + + 14 + 14 + + + + junit diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..839c983 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.Locale; + /** * Created by leon on 1/29/18. */ @@ -17,6 +21,7 @@ public static String getFirstElement(String[] array) { * @return second element in specified array */ public static String getSecondElement(String[] array) { + return array[1]; } @@ -25,7 +30,8 @@ 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 +39,8 @@ 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 +49,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) { + for (String s: array){ + if (s.equals(value)) + return true; + + if (!s.equals(value)); + + } return false; } @@ -50,23 +64,52 @@ 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; - } - /** - * @param array array of String objects - * @return true if the order of the array is the same backwards and forwards - */ // TODO - public static boolean isPalindromic(String[] array) { - return false; + String[] reverse = new String[array.length]; + int j = array.length - 1; + for (int i = 0; i < array.length; i++) { + + reverse[j--] = array[i]; + + + } + return reverse; } + /** + * @param array array of String objects + * @return true if the order of the array is the same backwards and forwards + */ // TODO + public static boolean isPalindromic (String[]array){ + String[] reverseArr = reverse(array); + for (int i = 0; i < reverseArr.length; i++) { + if(array[i].equals(reverseArr[i])) { + return true; + } + } + return false; + } + + + + /** * @param array array of String objects * @return true if each letter in the alphabet has been used in the array */ // TODO public static boolean isPangramic(String[] array) { - return false; + String stringArray = Arrays.toString(array); + + if(stringArray.toLowerCase().length() < 26) { + return false; + } else { + for (char index = 'a'; index <= 'z'; index++) { + if (stringArray.toLowerCase().indexOf(index) < 0) { + return false; + } + } + } + return true; } /** @@ -75,7 +118,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 numOcc = 0; + for (int i = 0; i < array.length; i++){ + if(array[i].equals(value)){ + numOcc++; + } + } + return numOcc; } /** @@ -84,7 +133,10 @@ 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 +144,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; + ArrayList list = new ArrayList(); + list.add(array[0]); + for (int i = 1; i < array.length; i++) { + if (array[i] != array[i-1]) { + list.add(array[i]); + } + + } + + return list.toArray(new String [list.size()]); } /** @@ -100,7 +161,20 @@ 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<>(); + list.add(array[0]); + for (int i = 1; i < array.length ; i++) { + if (array[i] != array[i-1]) { + list.add(array[i]); + } else { + String value = list.get(list.size() - 1) + array[i]; + list.set(list.size() - 1, value); + } + } + + + return list.toArray(new String [list.size()]); + }