From e2cecab09d16b01e231fb3e05303881ac2529a48 Mon Sep 17 00:00:00 2001 From: Roy_Yu Date: Fri, 30 Oct 2020 03:50:17 -0400 Subject: [PATCH] Assignment 5 answers --- StringArrayUtils.java | 73 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/StringArrayUtils.java b/StringArrayUtils.java index e86810b..60d849e 100644 --- a/StringArrayUtils.java +++ b/StringArrayUtils.java @@ -1,4 +1,8 @@ - +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; /** * Created by Yang on 1/23/20. @@ -9,7 +13,7 @@ public class StringArrayUtils { * @return first element of specified array */ // TODO public static String getFirstElement(String[] array) { - return null; + return (array == null || array.length == 0) ? null : array[0]; } /** @@ -17,7 +21,7 @@ public static String getFirstElement(String[] array) { * @return second element in specified array */ public static String getSecondElement(String[] array) { - return null; + return (array == null || array.length == 0) ? null : array[1]; } /** @@ -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 == null || array.length == 0) ? null : 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 == null || array.length == 0) ? null : array[array.length - 2]; } /** @@ -42,6 +46,8 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { + if (array == null || array.length == 0) return false; + for (String s : array) if (s.equals(value)) return true; return false; } @@ -50,7 +56,14 @@ 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; + if (array == null || array.length < 2) return array; + int i = 0, j = array.length - 1; + while (i < j) { + String tmp = array[i]; + array[i++] = array[j]; + array[j--] = tmp; + } + return array; } /** @@ -58,7 +71,10 @@ 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; + if (array == null || array.length == 0) return false; + int i = 0, j = array.length - 1; + while (i < j) if (!array[i++].equals(array[j--])) return false; + return true; } /** @@ -66,7 +82,17 @@ 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; + if (array == null || array.length == 0) return false; + Set set = new HashSet<>(); + for (char c = 'a'; c <= 'z'; c++) { + set.add(c); + } + for (String s : array) { + for (char c : s.toCharArray()) { + set.remove(Character.toLowerCase(c)); + } + } + return set.isEmpty(); } /** @@ -75,7 +101,10 @@ 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; + if (array == null || array.length == 0) return 0; + int result = 0; + for (String s : array) if (s.equals(value)) result++; + return result; } /** @@ -84,7 +113,7 @@ 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; + return Arrays.stream(array).filter(e -> !e.equals(valueToRemove)).toArray(String[] :: new); } /** @@ -92,7 +121,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; + if (array == null || array.length < 2) return array; + int i = 1; + for (int j = 1; j < array.length; j++) { + if (array[j] != array[i - 1]) { + array[i++] = array[j]; + } + } + String[] result = new String[i]; + for (int j = 0; j < i; j++) result[j] = array[j]; + return result; } /** @@ -100,7 +138,18 @@ 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; + if (array == null || array.length < 2) return array; + List result = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i != 0 && !array[i].equals(array[i - 1])) { + result.add(sb.toString()); + sb.delete(0, sb.length()); + } + sb.append(array[i]); + } + result.add(sb.toString()); + return result.toArray(new String[0]); }