diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..50e715b 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,15 +1,20 @@ package com.zipcodewilmington; +import java.util.Arrays; + /** * Created by leon on 1/29/18. */ public class StringArrayUtils { /** * @param array array of String objects - * @return first element of specified array */ // TODO public static String getFirstElement(String[] array) { - return array[0]; + String result = null; + if (array != null && array.length >= 1) { + result = array[0]; + } + return result; } /** @@ -17,7 +22,11 @@ public static String getFirstElement(String[] array) { * @return second element in specified array */ public static String getSecondElement(String[] array) { - return array[1]; + String result = null; + if (array != null && array.length >= 2) { + result = array[1]; + } + return result; } /** @@ -25,7 +34,11 @@ public static String getSecondElement(String[] array) { * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { - return null; + String result = null; + if (array != null && array.length > 0) { + result = array[array.length - 1]; + } + return result; } /** @@ -33,7 +46,11 @@ public static String getLastElement(String[] array) { * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { - return null; + String result = null; + if (array != null && array.length >= 2) { + result = array[array.length - 2]; + } + return result; } /** @@ -42,6 +59,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(int i = 0; i< array.length; i++){ + String s = array[i]; + if(array[i].contains(value)){ + return true; + } + } + return false; } @@ -50,7 +74,13 @@ 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; + StringBuilder reverse = new StringBuilder(); + int len = array.length; + for(int i = len ; i>0 ; i--){ + reverse.append(array[i-1]).append(" "); + } + String[] reversed = reverse.toString().split(" "); + return reversed; } /** @@ -58,7 +88,13 @@ 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; + int len = array.length; + for(int i =0; i< (len/2);i++){ + if(array[i] != array[len-i-1]){ + return false; + } + } + return true; } /** @@ -66,16 +102,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; + + boolean[] used = new boolean[26]; + for(int i = 0; i < array.length; i++){ + String word = array[i].toUpperCase(); + for (int j = 0; j < word.length(); j++) { + + if (word.charAt(j) >= 'A' && word.charAt(j) <= 'Z') { + int foundLetterIndex = word.charAt(j) - 'A'; + used[foundLetterIndex] = true; + } + } + } + + for (int k = 0; k < used.length; k++) { + if (!used[k]) { + return false; + } + } + return true; } + + /** * @param array array of String objects * @param value value to check array for * @return number of occurrences the specified `value` has occurred */ // TODO public static int getNumberOfOccurrences(String[] array, String value) { - return 0; + int count = 0; + for(int i = 0; i < array.length; i++){ + if(array[i] == value){ + count++; + } + } + return count; } /** @@ -84,7 +146,14 @@ 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; + String[] result = new String[array.length-1]; + int resultIndex = 0; + for(int i = 0; i < array.length; i++){ + if(!array[i].equals(valueToRemove)){ + result[resultIndex++] = array[i]; + } + } + return result; } /** @@ -92,7 +161,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; + StringBuilder s = new StringBuilder(); + for (int i = 0; i < array.length - 1; i++) { + if (!array[i].equals(array[i + 1])) { + s.append(array[i]).append(" "); + } + } + s.append(array[array.length-1]); + return s.toString().split(" "); } /** @@ -100,7 +176,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; + StringBuilder s= new StringBuilder(); + for(int i = 0; i< array.length-1; i++){ + + if(array[i].equals(array[i+1])){ + s.append(array[i]); + }else + { + s.append(array[i]); + s.append(" "); + } + } + s.append(array[array.length-1]); + return s.toString().split(" "); } diff --git a/src/test/java/com/zipcodewilmington/GetConsecutiveDuplicatesTest.java b/src/test/java/com/zipcodewilmington/GetConsecutiveDuplicatesTest.java index 2e188d6..3c74aa3 100644 --- a/src/test/java/com/zipcodewilmington/GetConsecutiveDuplicatesTest.java +++ b/src/test/java/com/zipcodewilmington/GetConsecutiveDuplicatesTest.java @@ -17,6 +17,8 @@ public void testRemoveConsecutiveDuplicates1() { + + @Test public void testRemoveConsecutiveDuplicates2() { String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};