From 2b85a322ab4ab8d246c89928ea1e681e04764794 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 1 Jul 2021 17:07:53 -0400 Subject: [PATCH] Generated code to complete all available test cases --- .../zipcodewilmington/StringArrayUtils.java | 96 +++++++++++++++++-- 1 file changed, 86 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..71b9859 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,9 +1,19 @@ package com.zipcodewilmington; +import com.sun.tools.javac.util.ArrayUtils; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + /** * Created by leon on 1/29/18. */ public class StringArrayUtils { + + /** * @param array array of String objects * @return first element of specified array @@ -25,7 +35,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 +43,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,15 +52,28 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { - return false; + Boolean result = false; + for (int i = 0; i < array.length; i++) { + if (array[i].contains(value)) { + result = true; + } + } + return result; } /** * @param array of String objects * @return an array with identical contents in reverse order */ // TODO + public static String[] reverse(String[] array) { - return null; + String[] result = new String[array.length]; + int decrement = array.length - 1; + for (int i = 0; i < result.length; i++) { + result[decrement] = array[i]; + decrement--; + } + return result; } /** @@ -58,7 +81,16 @@ 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 decrement = array.length - 1; + for (int i = 0; i < decrement - 1; i++) { + if (array[i].equals(array[decrement--])) { + return true; + } + if (!array[i].equals(array[decrement--])) { + return false; + } + } + return true; } /** @@ -75,7 +107,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 numberOfTimes = 0; + for (int i = 0; i < array.length; i++) { + if (array[i].equals(value)) { + numberOfTimes += 1; + } + } + + return numberOfTimes; } /** @@ -84,7 +123,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; + // Generate a Constructor with a new Object + // Add all elements(or indexes) to the newly generated 'arrList' + // call to remove the specific value from the new arrList + // return the arrList casted to a new String Array starting at the 0'th index + ArrayList arrList = new ArrayList(); + Collections.addAll(arrList, array); + arrList.remove(valueToRemove); + return arrList.toArray(new String[0]); } /** @@ -92,7 +138,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; + // this returns out of bounds exception for 2/3 (2/3 are 10 indexes (11 places) + // test 1 returns array lengths differ/ (9 indexes (10 places) + ArrayList arrList = new ArrayList(); + Collections.addAll(arrList, array); + for (int i = 1; i < arrList.size() - 1; i++) { + if (arrList.equals(i) == arrList.equals(i + 1)) { + arrList.remove(i); + } + } + return arrList.toArray(new String[0]); } /** @@ -100,8 +155,29 @@ 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; - } + // + int amountOfChars = 1; + for (int i = 0; i < array.length - 1; i++) { + if (array[i] != array[i + 1]) { + amountOfChars++; + } + } + String[] newArr = new String[amountOfChars]; + Integer index = 0; + String stringToBeAdded = ""; + stringToBeAdded += array[0]; + for (int i = 0; i < array.length; i++) { + if (array[i] == array[i - 1]) { + stringToBeAdded += array[i - 1]; + } else if (array[i] != array[i - 1]) { + newArr[index] = stringToBeAdded; + stringToBeAdded = array[i]; + index++; + } + } + newArr[index] = stringToBeAdded; + return newArr; + } }