diff --git a/pom.xml b/pom.xml index d10c35e..4d4515a 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 + + 8 + 8 + + + + junit diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..dfa7abc 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,10 @@ package com.zipcodewilmington; +import java.util.*; +import java.util.stream.IntStream; + +import static java.lang.System.*; + /** * Created by leon on 1/29/18. */ @@ -7,7 +12,7 @@ public class StringArrayUtils { /** * @param array array of String objects * @return first element of specified array - */ // TODO + */ // WORKING public static String getFirstElement(String[] array) { return array[0]; } @@ -15,7 +20,7 @@ public static String getFirstElement(String[] array) { /** * @param array array of String objects * @return second element in specified array - */ + */ //WORKING public static String getSecondElement(String[] array) { return array[1]; } @@ -23,85 +28,168 @@ public static String getSecondElement(String[] array) { /** * @param array array of String objects * @return last element in specified array - */ // TODO + */ + //WORKING public static String getLastElement(String[] array) { - return null; + return array[array.length - 1]; } /** * @param array array of String objects * @return second to last element in specified array - */ // TODO + */ + //WORKING public static String getSecondToLastElement(String[] array) { - return null; + + return array[array.length - 2]; } /** * @param array array of String objects * @param value value to check array for * @return true if the array contains the specified `value` - */ // TODO + */ // WORKING public static boolean contains(String[] array, String value) { - return false; + boolean presentOrNot = false; + for (int i = 0; i < array.length; i++) { + if (array[i].equals(value)) { + return true; + } else { + presentOrNot = false; + } + } + return presentOrNot; } /** * @param array of String objects * @return an array with identical contents in reverse order - */ // TODO + */ // WORKING public static String[] reverse(String[] array) { - return null; + List list = Arrays.asList(array); + Collections.reverse(list); + String[] revArr = list.toArray(array); + return revArr; } /** * @param array array of String objects * @return true if the order of the array is the same backwards and forwards - */ // TODO + */ // WORKING public static boolean isPalindromic(String[] array) { - return false; + Boolean revOrNot = false; + for (int i = 0; i < array.length / 2; i++) { + if (!array[i].equalsIgnoreCase((array[array.length - i - 1]))) { + revOrNot = false; + } else { + revOrNot = true; + } + } + return revOrNot; } /** * @param array array of String objects * @return true if each letter in the alphabet has been used in the array - */ // TODO + */ // WORKING public static boolean isPangramic(String[] array) { - return false; + boolean isOrIsnt = false; + boolean[] mark = new boolean[26]; + String concatStr = ""; + for (int i = 0; i < array.length; i++) { + concatStr = concatStr.concat(array[i]); + } + concatStr = concatStr.toUpperCase(); + int index = 0; + for (int i = 0; i < concatStr.length(); i++) { + if ('A' <= concatStr.charAt(i) && concatStr.charAt(i) <= 'Z') { + index = concatStr.charAt(i) - 'A'; + } else { + continue; + } + mark[index] = true; + } + for (int i = 0; i <= 25; i++) { + if (mark[i] == false) { + isOrIsnt = false; + } else { + isOrIsnt = true; + } + } + return isOrIsnt; } - /** * @param array array of String objects * @param value value to check array for * @return number of occurrences the specified `value` has occurred - */ // TODO + */ // WORKING public static int getNumberOfOccurrences(String[] array, String value) { - return 0; + int numOfTimes = 0; + for (int i = 0; i < array.length; i++) { + if (array[i].equalsIgnoreCase(value)) { + numOfTimes++; + } + } + return numOfTimes; } /** * @param array array of String objects * @param valueToRemove value to remove from array * @return array with identical contents excluding values of `value` - */ // TODO + */ // WORKING public static String[] removeValue(String[] array, String valueToRemove) { - return null; + ArrayList output = new ArrayList<>(); + int counter = 0; + for (int i = 0; i < array.length; i++) { + if (array[i].equals(valueToRemove)) { + continue; + } else { + output.add(array[i]); + counter++; + } + } + String[] outArr = output.toArray(new String[counter]); + return outArr; } /** * @param array array of chars * @return array of Strings with consecutive duplicates removes - */ // TODO + */ // WORKING public static String[] removeConsecutiveDuplicates(String[] array) { - return null; + ArrayList strList = new ArrayList<>(); + strList.add(array[0]); + for (int i = 1; i < array.length; i++) { + if (!array[i - 1].equals(array[i])) { + strList.add(array[i]); + } + } + String[] outStrArr = strList.toArray(new String[0]); + return outStrArr; } /** * @param array array of chars * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings - */ // TODO + */ // WORKING public static String[] packConsecutiveDuplicates(String[] array) { - return null; + ArrayList midList = new ArrayList<>(); + String strHolder = ""; + int counter = 0; + int compare = counter + 1; + while (counter < array.length) { + strHolder = strHolder.concat(array[counter]); + while (compare != array.length && array[counter].equals(array[compare])) { + strHolder = strHolder.concat(array[compare]); + compare++; + } + midList.add(strHolder); + strHolder = ""; + counter = compare; + compare++; + } + String[] outArr = midList.toArray(new String[0]); + return outArr; } - - }