diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..16ee771 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,12 @@ package com.zipcodewilmington; +import com.sun.tools.javac.util.ArrayUtils; + +import java.sql.SQLOutput; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; + /** * Created by leon on 1/29/18. */ @@ -25,7 +32,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 +40,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 +49,11 @@ 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 val: array){ + if (val.equals(value)){ + return true; + } + } return false; } @@ -50,7 +62,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; + ArrayList arrayL = new ArrayList(); + Collections.addAll(arrayL,array); + Collections.reverse(arrayL); + String[] arr = new String[array.length]; + return arrayL.toArray(arr); } /** @@ -58,7 +74,7 @@ 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; + return Arrays.equals(array,StringArrayUtils.reverse(array)); } /** @@ -66,7 +82,42 @@ 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; + //an algo which goes thru the string[] and takes out stuff as it sees it + String[] abc = "abcdefghijklmnopqrstuvwxyz".split(""); + ArrayList atLarge = new ArrayList(); + Collections.addAll(atLarge, abc); + ArrayList found = new ArrayList(); + int count = 0; + boolean wasFound = false; + for(int i = 0; i < array.length; i++){ + String[] cur = array[i].split(""); + for(int j = 0; j < array[i].length(); j++){ + wasFound = false; + //& ignores stuff already found + if(! found.isEmpty()) { + for(int l = 0; l < found.size(); l++) { + if(found.get(l).equals(cur[j])){ + l = found.size(); + wasFound = true; + } + } + } + if(j < cur.length && !wasFound) { + for (int k = 0; k < atLarge.size(); k++) { + if(atLarge.isEmpty()){ + return true; + } + if (atLarge.get(k).equalsIgnoreCase(cur[j])) { + found.add(atLarge.get(k)); + atLarge.remove(k); + k = atLarge.size(); + } + + } + } + } + } + return atLarge.isEmpty(); } /** @@ -75,7 +126,14 @@ 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 occs = 0; + boolean different = false; + for(int i = 0; i < array.length; i++) { + if (array[i].equals(value)) { + occs++; + } + } + return occs; } /** @@ -84,7 +142,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 strings = new ArrayList(); + Collections.addAll(strings, array); + strings.remove(valueToRemove); + return strings.toArray(new String[0]); } /** @@ -92,7 +153,14 @@ 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 strings = new ArrayList(); + Collections.addAll(strings, array); + for(int i = 0; i < strings.size(); i++){ + if(i != 0 && strings.get(i).equals(strings.get(i - 1))){ + strings.remove(i--); + } + } + return strings.toArray(new String[0]); } /** @@ -100,7 +168,15 @@ 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 strings = new ArrayList(); + Collections.addAll(strings, array); + for(int i = 0; i < strings.size(); i++){ + while(i != strings.size() -1 && strings.get(i).charAt(0) == (strings.get(i+1).charAt(0))){ + strings.set(i, strings.get(i) + strings.get(i+1)); + strings.remove(i+1); + } + } + return strings.toArray(new String[0]); }