From eda1fb8ba0b39eb1b61fa872806fc2dbd6ce3544 Mon Sep 17 00:00:00 2001 From: Zachary Kitto Date: Wed, 30 Jun 2021 10:44:55 -0400 Subject: [PATCH 1/2] First go thru --- .../zipcodewilmington/StringArrayUtils.java | 73 ++++++++++++++++--- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..9f6f6a4 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -25,7 +25,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 +33,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,7 +42,14 @@ 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; + String arrayToString = array.toString(); + if (arrayToString.contains(value)) { + return true; + } + else { + return false; + } + } /** @@ -50,7 +57,12 @@ 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; + String arrayAsString = ""; + for (int i = array.length; i >= 0; i--) { + arrayAsString += (array[i] + " "); + } + String[] newArray = arrayAsString.split(" "); + return newArray; } /** @@ -58,7 +70,17 @@ 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; + String arrayBackwards = ""; + for (int i = array.length; i >= 0; i--) { + arrayBackwards += (array[i]); + } + String arrayFowards = array.toString().trim(); + if (arrayFowards.equals(arrayBackwards)) { + return true; + } + else { + return false; + } } /** @@ -66,7 +88,20 @@ 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; + String[] alphabet = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; + String arrayAsString = array.toString(); + int amountOfLettersInAlphabet = 0; + for (int i = 0; i < arrayAsString.length(); i++) { + if (arrayAsString.contains(alphabet[i])) { + amountOfLettersInAlphabet += 1; + } + } + if (amountOfLettersInAlphabet == 26) { + return true; + } + else { + return false; + } } /** @@ -75,7 +110,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; + String arrayAsString = array.toString().trim(); + arrayAsString.replaceAll(value, " "); + int index = 0; + int numberOfOccurrences = 0; + while ((index = arrayAsString.indexOf(" ", index)) != -1) { + numberOfOccurrences += 1; + } + return numberOfOccurrences; } /** @@ -84,7 +126,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; + String arrayAsString = array.toString(); + arrayAsString.replaceAll(valueToRemove, ""); + String[] backToArray = arrayAsString.split(","); + return backToArray; } /** @@ -92,7 +137,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; + String arrayAsString = array.toString(); + String[] newArray; + String stringWithNoDuplicates = ""; + for (int i = 0; i < array.length; i++) { + if (arrayAsString.charAt(i) == arrayAsString.charAt(i + 1)) { + stringWithNoDuplicates = arrayAsString.replaceFirst(String.format("%s", arrayAsString.charAt(i)), ""); + } + } + newArray = stringWithNoDuplicates.split(","); + return newArray; } /** @@ -100,6 +154,7 @@ 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; } From f1486ffafbbf82611e72ab84c46681e29b23248c Mon Sep 17 00:00:00 2001 From: Zachary Kitto Date: Fri, 2 Jul 2021 20:22:15 -0400 Subject: [PATCH 2/2] All tests passed --- pom.xml | 12 +++ .../zipcodewilmington/StringArrayUtils.java | 100 ++++++++++-------- 2 files changed, 67 insertions(+), 45 deletions(-) diff --git a/pom.xml b/pom.xml index d10c35e..0285ce3 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 + + 6 + 6 + + + + junit diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 9f6f6a4..07906fb 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,9 @@ package com.zipcodewilmington; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * Created by leon on 1/29/18. */ @@ -42,14 +46,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) { - String arrayToString = array.toString(); - if (arrayToString.contains(value)) { + ArrayList arrayToList = new ArrayList(Arrays.asList(array)); + if (arrayToList.contains(value)) { return true; } else { return false; } - } /** @@ -57,12 +60,12 @@ public static boolean contains(String[] array, String value) { * @return an array with identical contents in reverse order */ // TODO public static String[] reverse(String[] array) { - String arrayAsString = ""; - for (int i = array.length; i >= 0; i--) { - arrayAsString += (array[i] + " "); + ArrayList arrayList = new ArrayList(); + for (int i = array.length - 1; i >= 0; i--) { + arrayList.add(array[i]); } - String[] newArray = arrayAsString.split(" "); - return newArray; + String[] backToArray = arrayList.toArray(new String[0]); + return backToArray; } /** @@ -70,12 +73,12 @@ 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) { - String arrayBackwards = ""; - for (int i = array.length; i >= 0; i--) { - arrayBackwards += (array[i]); + ArrayList arrayList = new ArrayList(); + for (int i = array.length - 1; i >= 0; i--) { + arrayList.add(array[i]); } - String arrayFowards = array.toString().trim(); - if (arrayFowards.equals(arrayBackwards)) { + String[] backToArray = arrayList.toArray(new String[0]); + if (Arrays.equals(backToArray,array)) { return true; } else { @@ -88,20 +91,16 @@ 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) { - String[] alphabet = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; - String arrayAsString = array.toString(); - int amountOfLettersInAlphabet = 0; - for (int i = 0; i < arrayAsString.length(); i++) { - if (arrayAsString.contains(alphabet[i])) { - amountOfLettersInAlphabet += 1; + String[] alphabet = new String[] {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; + List arrayToList = new ArrayList(Arrays.asList(array)); + String arrayToString = arrayToList.toString(); + arrayToString = arrayToString.replaceAll(",", "").replaceAll(" ","").toLowerCase(); + for (int i = 0; i < alphabet.length; i++) { + if (!arrayToString.contains(alphabet[i])) { + return false; } } - if (amountOfLettersInAlphabet == 26) { - return true; - } - else { - return false; - } + return true; } /** @@ -110,14 +109,13 @@ public static boolean isPangramic(String[] array) { * @return number of occurrences the specified `value` has occurred */ // TODO public static int getNumberOfOccurrences(String[] array, String value) { - String arrayAsString = array.toString().trim(); - arrayAsString.replaceAll(value, " "); - int index = 0; - int numberOfOccurrences = 0; - while ((index = arrayAsString.indexOf(" ", index)) != -1) { - numberOfOccurrences += 1; + int occurrences = 0; + for (String word : array) { + if (word.equals(value)) { + occurrences++; + } } - return numberOfOccurrences; + return occurrences; } /** @@ -126,9 +124,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) { - String arrayAsString = array.toString(); - arrayAsString.replaceAll(valueToRemove, ""); - String[] backToArray = arrayAsString.split(","); + ArrayList arrayToList = new ArrayList(Arrays.asList(array)); + arrayToList.remove(valueToRemove); + String[] backToArray = new String[arrayToList.size()]; + backToArray = arrayToList.toArray(backToArray); return backToArray; } @@ -137,16 +136,16 @@ public static String[] removeValue(String[] array, String valueToRemove) { * @return array of Strings with consecutive duplicates removes */ // TODO public static String[] removeConsecutiveDuplicates(String[] array) { - String arrayAsString = array.toString(); - String[] newArray; - String stringWithNoDuplicates = ""; - for (int i = 0; i < array.length; i++) { - if (arrayAsString.charAt(i) == arrayAsString.charAt(i + 1)) { - stringWithNoDuplicates = arrayAsString.replaceFirst(String.format("%s", arrayAsString.charAt(i)), ""); + List arrayList = new ArrayList(); + arrayList.add(array[0]); + int indexForList = 0; // Have to create an index for the list b/c when it removes a dup, the size of the list doesn't increase + for (int i = 1; i < array.length; i++) { + if (!arrayList.get(indexForList).contains(array[i])) { + arrayList.add(array[i]); + indexForList++; } } - newArray = stringWithNoDuplicates.split(","); - return newArray; + return arrayList.toArray(new String[0]); } /** @@ -154,8 +153,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; + ArrayList arrayList = new ArrayList(); + arrayList.add(array[0]); + int indexForList = 0; // Have to create an index for the list b/c when it concats a dup, the size of the list doesn't increase + for (int i = 1; i < array.length; i++) { + if (arrayList.get(indexForList).contains(array[i])) { + arrayList.set(indexForList, (arrayList.get(indexForList) + array[i])); + } + else { + arrayList.add(array[i]); + indexForList++; + } + } + return arrayList.toArray(new String[0]); }