From 2d92fd185d00afd11fa5eadcf8257b7560098e36 Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 1 Jul 2021 00:13:23 -0400 Subject: [PATCH] Passed all tests --- pom.xml | 12 +++ .../zipcodewilmington/StringArrayUtils.java | 98 +++++++++++++++++-- 2 files changed, 100 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index d10c35e..991cf63 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 + + 11 + 11 + + + + junit diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..d69f6ec 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,11 @@ package com.zipcodewilmington; +import java.awt.*; +import java.sql.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Locale; + /** * Created by leon on 1/29/18. */ @@ -25,7 +31,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,15 +39,22 @@ 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]; } /** * @param array array of String objects * @param value value to check array for * @return true if the array contains the specified `value` - */ // TODO + */ // Ask about: + // List nameList = new ArrayList<>(Arrays.asList(array)); + // return nameList.contains(value); public static boolean contains(String[] array, String value) { + for(int i = 0; i < array.length; i++) { + if (array[i] == value) { + return true; + } + } return false; } @@ -50,7 +63,11 @@ 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[] reversedArr = new String[array.length]; + for(int i = 0; i < array.length; i++){ + reversedArr[i] = array[array.length - (i+1)]; + } + return reversedArr; } /** @@ -58,7 +75,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; + for(int i = 0; i < array.length; i++){ + if(array[i] != array[array.length - (i+1)]){ + return false; + } + } + return true; } /** @@ -66,7 +88,19 @@ 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; + //Need a stringBuffer to hold all array strings together AND use .append + StringBuffer oneString = new StringBuffer(); + for(String string: array){ + oneString.append(string); + } + //Turn stringBuffer into String + String fullString = oneString.toString().toLowerCase(); + //Delete everything thats not a letter + String lettersOnly = fullString.replaceAll("(.)(?=.*\\1)", ""); + //Delete all repeating letters + String noRepeatChars = lettersOnly.replaceAll("[^a-z]", ""); + //Return true if string has 26 letters. + return noRepeatChars.length() == 26; } /** @@ -75,7 +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) { - return 0; + int count = 0; + for (String string: array) { + if(string.equals(value)){ + count++; + } + } + return count; } /** @@ -84,7 +124,18 @@ 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; + int index = 0; + for(String string: array){ + if(string != valueToRemove){ + array[index++] = string; + //index++; + } + } + String[] newArray = new String[index]; + for(int i = 0; i < index; i++){ + newArray[i] = array[i]; + } + return newArray; } /** @@ -92,7 +143,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; + ArrayList newArray = new ArrayList(); + newArray.add(array[0]); + for(int i = 1; i < array.length; i++){ + if(array[i] != array[i-1]){ + newArray.add(array[i]); + } + } + return newArray.toArray(new String[0]); } /** @@ -100,7 +158,27 @@ 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 countOfchars = 1; + for(int i = 0; i < array.length-1; i++){ + if(array[i] != array[i+1]){ + countOfchars++; + } + } + String[] newArr = new String[countOfchars]; + int index = 0; + String addMe = ""; + addMe += array[0]; + for(int i = 1; i < array.length; i++){ + if(array[i] == array[i-1]){ + addMe += array[i-1]; + } else if (array[i] != array[i-1]){ + newArr[index] = addMe; + addMe = array[i]; + index++; + } + } + newArr[index] = addMe; + return newArr; }