From eb176582453763845a42ad2064a900a6730b97b6 Mon Sep 17 00:00:00 2001 From: Zach Date: Thu, 1 Jul 2021 13:50:54 -0400 Subject: [PATCH 1/2] All test cases evaluate to true --- pom.xml | 12 +++ .../zipcodewilmington/StringArrayUtils.java | 78 ++++++++++++++++--- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index d10c35e..4d4515a 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 + + 8 + 8 + + + + junit diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..469fa71 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,7 @@ package com.zipcodewilmington; +import java.util.*; + /** * Created by leon on 1/29/18. */ @@ -25,7 +27,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 +35,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 +44,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) { - return false; + List arrayList = new ArrayList<>(Arrays.asList(array)); + return arrayList.contains(value); } /** @@ -50,7 +53,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[] reversedArray = new String[array.length]; + int j = array.length - 1; + for(int i = 0; i < array.length; i++){ + reversedArray[j--] = array[i]; + } + return reversedArray; } /** @@ -58,7 +66,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) { - return false; + int midPoint = array.length / 2; + int lastIndex = array.length - 1; + for(int i = 0; i < midPoint; i++){ + if(!array[i].equals(array[lastIndex - i])) return false; + } + return true; } /** @@ -66,7 +79,14 @@ 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 = "abcdefghijklmnopqrstuvwxyz"; + List arrayList = new ArrayList<>(Arrays.asList(array)); + String allContents = arrayList.toString(); + allContents = allContents.replaceAll(",", "").replaceAll(" ", "").toLowerCase(Locale.ROOT); + for(int i = 0; i < alphabet.length(); i++){ + if(allContents.indexOf(alphabet.charAt(i)) == -1) return false; + } + return true; } /** @@ -75,7 +95,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) { - return 0; + int count = 0; + for(String element : array){ + if(element == value){ + count++; + } + } + return count; } /** @@ -84,7 +110,12 @@ 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 arrayList = new ArrayList<>(); + Collections.addAll(arrayList, array); + while(arrayList.contains(valueToRemove)){ + arrayList.remove(valueToRemove); + } + return arrayList.toArray(new String[0]); } /** @@ -92,15 +123,42 @@ 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 arrayList = new ArrayList<>(); + String lastElement = ""; + for(String element : array){ + if(element != lastElement){ + arrayList.add(element); + } + lastElement = element; + } + return arrayList.toArray(new String[0]); } /** * @param array array of chars * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings */ // TODO + + /* + String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"}; + String[] expected = {"aaa", "b", "cc", "aa", "d"}; + */ public static String[] packConsecutiveDuplicates(String[] array) { - return null; + int length = array.length; + int lastIndex = 0; + ArrayList arrayList = new ArrayList<>(); + arrayList.add(array[0]); + + for(int i = 1; i < length; i++){ + System.out.println(arrayList.get(lastIndex)); + if(arrayList.get(lastIndex).contains(array[i])){//array[i] == arrayList.get(lastIndex)){ + arrayList.set(lastIndex, arrayList.get(lastIndex) + array[i]); + } else { + lastIndex++; + arrayList.add(array[i]); + } + } + return arrayList.toArray(new String[0]); } From 70a853817e4a602e49a9b34d80d804f610d4e07d Mon Sep 17 00:00:00 2001 From: Zach Date: Fri, 2 Jul 2021 00:13:55 -0400 Subject: [PATCH 2/2] fixed error where last method was not passing --- .../java/com/zipcodewilmington/StringArrayUtils.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 469fa71..3f3859d 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -140,8 +140,9 @@ public static String[] removeConsecutiveDuplicates(String[] array) { */ // TODO /* - String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"}; + String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"}; arrayList has 1 position, and it's an a. String[] expected = {"aaa", "b", "cc", "aa", "d"}; + */ public static String[] packConsecutiveDuplicates(String[] array) { int length = array.length; @@ -150,11 +151,10 @@ public static String[] packConsecutiveDuplicates(String[] array) { arrayList.add(array[0]); for(int i = 1; i < length; i++){ - System.out.println(arrayList.get(lastIndex)); - if(arrayList.get(lastIndex).contains(array[i])){//array[i] == arrayList.get(lastIndex)){ - arrayList.set(lastIndex, arrayList.get(lastIndex) + array[i]); - } else { - lastIndex++; + if(arrayList.get(lastIndex).contains(array[i])){ //does my arrayList, at the last examined index contain what the array current shows + arrayList.set(lastIndex, (arrayList.get(lastIndex) + array[i])); //arrayList.set(index, value) + } else { //no longer looking at a consecutive + lastIndex++; //I'm looking at a new 0R non-duplicate character arrayList.add(array[i]); } }