From e6b5bad8d9fb093c392967a3d7ae2bf147d41778 Mon Sep 17 00:00:00 2001 From: Brian Patterson Date: Wed, 23 Oct 2019 08:59:53 -0400 Subject: [PATCH 1/4] first commit --- src/main/java/com/zipcodewilmington/StringArrayUtils.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..4f10e56 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,8 @@ 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]; + } /** From 6aed4c2f914729a301e24967a5cf5b70ba4ef58b Mon Sep 17 00:00:00 2001 From: Brian Patterson Date: Wed, 23 Oct 2019 17:44:31 -0400 Subject: [PATCH 2/4] added answers --- .../zipcodewilmington/StringArrayUtils.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4f10e56..fb563df 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,13 @@ package com.zipcodewilmington; +import com.sun.tools.javac.util.ArrayUtils; +import com.sun.xml.internal.xsom.util.DeferedCollection; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + /** * Created by leon on 1/29/18. */ @@ -25,7 +33,7 @@ public static String getSecondElement(String[] array) { * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { - return array[array.length-1]; + return array[array.length - 1]; } /** @@ -33,7 +41,7 @@ public static String getLastElement(String[] array) { * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { - return array[array.length-2]; + return array[array.length - 2]; } @@ -43,6 +51,7 @@ 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; } @@ -51,7 +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) { - return null; + List arrList = Arrays.asList(array); + Collections.reverse(arrList); + + + array = arrList.toArray(new String[arrList.size()]); + return array; } /** @@ -76,16 +90,26 @@ 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 s : array) { + if (s.equals(value)) + count++; + } + return count; } + + /** * @param array array of String objects * @param valueToRemove value to remove from array * @return array with identical contents excluding values of `value` */ // TODO public static String[] removeValue(String[] array, String valueToRemove) { - return null; + + List arrList = Arrays.asList(array); + + return array; } /** @@ -106,3 +130,4 @@ public static String[] packConsecutiveDuplicates(String[] array) { } + From 3a90a487766f22d4b1c8df8ad744730e9cd2c5e5 Mon Sep 17 00:00:00 2001 From: Brian Patterson Date: Thu, 24 Oct 2019 13:16:30 -0400 Subject: [PATCH 3/4] Updated answers --- .../zipcodewilmington/StringArrayUtils.java | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index fb563df..dcb46fe 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -3,10 +3,7 @@ import com.sun.tools.javac.util.ArrayUtils; import com.sun.xml.internal.xsom.util.DeferedCollection; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; /** * Created by leon on 1/29/18. @@ -60,7 +57,7 @@ public static boolean contains(String[] array, String value) { * @return an array with identical contents in reverse order */ // TODO public static String[] reverse(String[] array) { - List arrList = Arrays.asList(array); + ArrayList arrList = new ArrayList(Arrays.asList(array)); Collections.reverse(arrList); @@ -107,9 +104,12 @@ public static int getNumberOfOccurrences(String[] array, String value) { */ // TODO public static String[] removeValue(String[] array, String valueToRemove) { - List arrList = Arrays.asList(array); - - return array; + ArrayList arrList = new ArrayList(Arrays.asList(array)); + for (int i = 0; i < arrList.size(); i++) + if(arrList.get(i).equals(valueToRemove)) { + arrList.remove(arrList.get(i)); + } + return arrList.toArray(new String[arrList.size()]); } /** @@ -117,7 +117,18 @@ 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 arrList = new ArrayList(Arrays.asList(array)); + + for (int i = 1; i < arrList.size(); i++) { + + if (arrList.get(i).equals(arrList.get(i-1))) { + arrList.remove(arrList.get(i-1)); + } + + } + return arrList.toArray(new String[arrList.size()]); } /** @@ -125,9 +136,15 @@ 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) { + ArrayList arrList = new ArrayList(Arrays.asList(array)); + + /*for (int i = 1; i < arrList.size(); i++){ + if(arrList.get(i-1).equals(arrList.get(i))){ + arrList.get(i-1} arrList.get(i); */ return null; } } + From d4c6decf44393e536bac47eddc7a215d943b7461 Mon Sep 17 00:00:00 2001 From: Brian Patterson Date: Wed, 30 Oct 2019 07:48:32 -0400 Subject: [PATCH 4/4] complete --- .../zipcodewilmington/StringArrayUtils.java | 79 ++++++++++++++----- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index dcb46fe..2022d36 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -48,10 +48,18 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { + boolean hasString = false; + for (int i = 0; i < array.length; i++) { + if (array[i].equals(value)) { + hasString = true; - return false; + } + + } + return hasString; } + /** * @param array of String objects * @return an array with identical contents in reverse order @@ -70,7 +78,14 @@ 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; + boolean isPal = true; + for (int i = 0; i < array.length / 2; i++) { + if (array[i] == array[array.length - (i + 1)]) { + } else { + isPal = false; + } + } + return isPal; } /** @@ -78,8 +93,29 @@ 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 str = Arrays.toString(array); + boolean[] alphabetList = new boolean[26]; + + + int index = 0; + int verifier = 1; + String str2 = str.toUpperCase(); + for (int i = 0; i < str2.length(); i++) { + if (str2.charAt(i) >= 'A' && str2.charAt(i) <= 'Z') { + index = str2.charAt(i) - 'A'; + } + alphabetList[index] = true; + } + for (int i = 0; i <= 25; i++) { + if (alphabetList[i] == false) { + verifier = 0;} + } + return verifier == 1; + } + + + + /** * @param array array of String objects @@ -96,7 +132,6 @@ public static int getNumberOfOccurrences(String[] array, String value) { } - /** * @param array array of String objects * @param valueToRemove value to remove from array @@ -106,7 +141,7 @@ public static String[] removeValue(String[] array, String valueToRemove) { ArrayList arrList = new ArrayList(Arrays.asList(array)); for (int i = 0; i < arrList.size(); i++) - if(arrList.get(i).equals(valueToRemove)) { + if (arrList.get(i).equals(valueToRemove)) { arrList.remove(arrList.get(i)); } return arrList.toArray(new String[arrList.size()]); @@ -119,15 +154,13 @@ public static String[] removeValue(String[] array, String valueToRemove) { public static String[] removeConsecutiveDuplicates(String[] array) { - ArrayList arrList = new ArrayList(Arrays.asList(array)); - - for (int i = 1; i < arrList.size(); i++) { + ArrayList arrList = new ArrayList(); + arrList.add(array[0]); - if (arrList.get(i).equals(arrList.get(i-1))) { - arrList.remove(arrList.get(i-1)); + for (int i = 1; i < array.length; i++) + if (array[i] != array[i-1]) { + arrList.add(array[i]); } - - } return arrList.toArray(new String[arrList.size()]); } @@ -136,15 +169,21 @@ 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) { - ArrayList arrList = new ArrayList(Arrays.asList(array)); - - /*for (int i = 1; i < arrList.size(); i++){ - if(arrList.get(i-1).equals(arrList.get(i))){ - arrList.get(i-1} arrList.get(i); */ - return null; - } + String catt = ""; + String temp = array[0]; + for (int i = 0; i < array.length; i++) { + if (temp == array[i]) { + catt += array[i]; + } else { + temp = array[i]; + catt += " " + temp; + } + } + String[] arr = catt.split(" "); + return arr; + } }