From e80732356b6b058049e8a12f469764c741c116d7 Mon Sep 17 00:00:00 2001 From: Quatrani Paul Date: Wed, 3 Nov 2021 10:18:59 -0400 Subject: [PATCH 1/6] finished fleshing out pangram --- .../zipcodewilmington/StringArrayUtils.java | 82 +++++++++++++++++-- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..b59537c 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 com.sun.tools.javac.util.ArrayUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; + /** * 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,7 +39,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,6 +48,11 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { + for(String val: array){ + if (val.equals(value)){ + return true; + } + } return false; } @@ -50,7 +61,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; + ArrayList arrayL = new ArrayList(); + Collections.addAll(arrayL,array); + Collections.reverse(arrayL); + String[] arr = new String[array.length]; + return arrayL.toArray(arr); } /** @@ -58,7 +73,7 @@ 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; + return Arrays.equals(array,StringArrayUtils.reverse(array)); } /** @@ -66,7 +81,64 @@ 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; + //a thing which goes thru the string[] and takes out stuff as it sees it + /** + String abc = "abcdefghijklmnopqrstuvwxyz"; + int wasCharFound = 0; + int i , j , k = 0; + while(i < abc.length()){ + wasCharFound = 0; + j = 0; + for(; j < array.length; j++){ + k = 0; + for(; k < array[j].length();k++){ + if(array[j][k].equals(abc[i])){ + k = array[j].length(); + j = array.length; + i++; + wasCharFound++; + } + } + } + if(wasCharFound == 0){ + return false; + } + } + return true; + */ + String abc = "abcdefghijklmnopqrstuvwxyz"; + ArrayList atLarge = new ArrayList(); + Collections.addAll(atLarge,abc.split("")); + ArrayList found = new ArrayList(); + for(int i = 0; i < array.length; i++){ + for(int j = 0; j < array[i].length(); j++){ + //ignore stuff already found + String[] cur = array[i].split(""); + int wasFound = 0; + if(! found.isEmpty()) { + for(int l = 0; l < found.size(); l++) { + if(found.get(l).equals(cur[j])){ + j++; + l = found.size(); + wasFound++; + } + } + } + if(j < cur.length && wasFound == 0) { + for (int k = 0; k < atLarge.size(); k++) { + if (atLarge.get(k).equals(cur[j])) { + found.add(atLarge.get(k)); + //"suspicious"? + atLarge.remove(k); + k = atLarge.size(); + j = array[i].length(); + i = 0; + } + } + } + } + } + return atLarge.isEmpty(); } /** From 14eaab6dd8dc11143461226d49e2fd052c8da1b3 Mon Sep 17 00:00:00 2001 From: Quatrani Paul Date: Wed, 3 Nov 2021 12:09:51 -0400 Subject: [PATCH 2/6] pangram passes all tests --- .../zipcodewilmington/StringArrayUtils.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index b59537c..26ebae9 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -2,6 +2,7 @@ import com.sun.tools.javac.util.ArrayUtils; +import java.sql.SQLOutput; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -110,30 +111,37 @@ public static boolean isPangramic(String[] array) { ArrayList atLarge = new ArrayList(); Collections.addAll(atLarge,abc.split("")); ArrayList found = new ArrayList(); + int count = 0; + boolean wasFound = false; for(int i = 0; i < array.length; i++){ + //restart = false; + //System.out.println(i); + String[] cur = array[i].split(""); for(int j = 0; j < array[i].length(); j++){ + wasFound = false; //ignore stuff already found - String[] cur = array[i].split(""); - int wasFound = 0; if(! found.isEmpty()) { for(int l = 0; l < found.size(); l++) { if(found.get(l).equals(cur[j])){ - j++; + System.out.println("already found:" + found.get(l) + j); l = found.size(); - wasFound++; + wasFound = true; } } } - if(j < cur.length && wasFound == 0) { + if(j < cur.length && !wasFound) { for (int k = 0; k < atLarge.size(); k++) { - if (atLarge.get(k).equals(cur[j])) { + if(atLarge.isEmpty()){ + return true; + } + if (atLarge.get(k).equalsIgnoreCase(cur[j])) { found.add(atLarge.get(k)); - //"suspicious"? + System.out.println("found: " + atLarge.get(k) + j); + // "suspicious"? atLarge.remove(k); k = atLarge.size(); - j = array[i].length(); - i = 0; } + } } } @@ -147,7 +155,8 @@ 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 occs = 0; + return occs; } /** From 067026e99ea1df78ab6d8099df845b2af0779c65 Mon Sep 17 00:00:00 2001 From: Quatrani Paul Date: Wed, 3 Nov 2021 12:19:52 -0400 Subject: [PATCH 3/6] fleshed out getnumberofoccurences method --- .../zipcodewilmington/StringArrayUtils.java | 50 +++++++------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 26ebae9..cdd8f03 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -82,31 +82,7 @@ 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) { - //a thing which goes thru the string[] and takes out stuff as it sees it - /** - String abc = "abcdefghijklmnopqrstuvwxyz"; - int wasCharFound = 0; - int i , j , k = 0; - while(i < abc.length()){ - wasCharFound = 0; - j = 0; - for(; j < array.length; j++){ - k = 0; - for(; k < array[j].length();k++){ - if(array[j][k].equals(abc[i])){ - k = array[j].length(); - j = array.length; - i++; - wasCharFound++; - } - } - } - if(wasCharFound == 0){ - return false; - } - } - return true; - */ + //an algo which goes thru the string[] and takes out stuff as it sees it String abc = "abcdefghijklmnopqrstuvwxyz"; ArrayList atLarge = new ArrayList(); Collections.addAll(atLarge,abc.split("")); @@ -114,16 +90,13 @@ public static boolean isPangramic(String[] array) { int count = 0; boolean wasFound = false; for(int i = 0; i < array.length; i++){ - //restart = false; - //System.out.println(i); String[] cur = array[i].split(""); for(int j = 0; j < array[i].length(); j++){ wasFound = false; - //ignore stuff already found + //& ignores stuff already found if(! found.isEmpty()) { for(int l = 0; l < found.size(); l++) { if(found.get(l).equals(cur[j])){ - System.out.println("already found:" + found.get(l) + j); l = found.size(); wasFound = true; } @@ -136,8 +109,6 @@ public static boolean isPangramic(String[] array) { } if (atLarge.get(k).equalsIgnoreCase(cur[j])) { found.add(atLarge.get(k)); - System.out.println("found: " + atLarge.get(k) + j); - // "suspicious"? atLarge.remove(k); k = atLarge.size(); } @@ -156,6 +127,23 @@ public static boolean isPangramic(String[] array) { */ // TODO public static int getNumberOfOccurrences(String[] array, String value) { int occs = 0; + boolean different = false; + for(int i = 0; i < array.length; i++){ + for(int j = 0; j < array[i].length(); j++){ + if(array[i].charAt(j) == value.charAt(0)){ + for(int k = 1; k < value.length() ; k++){ + j++; + if(value.charAt(k) != array[i].charAt(j)){ + k = value.length(); + different = true; + } + } + if(!different){ + occs++; + } + } + } + } return occs; } From 18e638f3c3bab99fd228eed01570036a7593b1a9 Mon Sep 17 00:00:00 2001 From: Quatrani Paul Date: Wed, 3 Nov 2021 12:45:18 -0400 Subject: [PATCH 4/6] finished fleshing out all methods --- .../zipcodewilmington/StringArrayUtils.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index cdd8f03..1778629 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -83,9 +83,9 @@ public static boolean isPalindromic(String[] array) { */ // TODO public static boolean isPangramic(String[] array) { //an algo which goes thru the string[] and takes out stuff as it sees it - String abc = "abcdefghijklmnopqrstuvwxyz"; + String[] abc = "abcdefghijklmnopqrstuvwxyz".split(""); ArrayList atLarge = new ArrayList(); - Collections.addAll(atLarge,abc.split("")); + Collections.addAll(atLarge, abc); ArrayList found = new ArrayList(); int count = 0; boolean wasFound = false; @@ -153,7 +153,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; + ArrayList strings = new ArrayList(); + Collections.addAll(strings, array); + strings.remove(valueToRemove); + return strings.toArray(new String[array.length]); } /** @@ -161,7 +164,15 @@ 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 strings = new ArrayList(); + Collections.addAll(strings, array); + for(int i = 0; i < strings.size(); i++){ + if(i != 0 && strings.get(i).equals(strings.get(i - 1))){ + strings.remove(i); + i--; + } + } + return strings.toArray(new String[array.length]); } /** @@ -169,7 +180,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) { - return null; + ArrayList strings = new ArrayList(); + Collections.addAll(strings, array); + for(int i = 0; i < strings.size(); i++){ + if(i != 0 && strings.get(i).equals(strings.get(i - 1))){ + strings.set(i,strings.get(i) + strings.get(i - 1)); + strings.remove(i-1); + } + } + return strings.toArray(new String[array.length]); } From ad042d0c22bab5633bcc60248985578df06e7c63 Mon Sep 17 00:00:00 2001 From: Quatrani Paul Date: Wed, 3 Nov 2021 14:26:16 -0400 Subject: [PATCH 5/6] code passes all tests --- .../zipcodewilmington/StringArrayUtils.java | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 1778629..796de0e 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -128,20 +128,9 @@ public static boolean isPangramic(String[] array) { public static int getNumberOfOccurrences(String[] array, String value) { int occs = 0; boolean different = false; - for(int i = 0; i < array.length; i++){ - for(int j = 0; j < array[i].length(); j++){ - if(array[i].charAt(j) == value.charAt(0)){ - for(int k = 1; k < value.length() ; k++){ - j++; - if(value.charAt(k) != array[i].charAt(j)){ - k = value.length(); - different = true; - } - } - if(!different){ - occs++; - } - } + for(int i = 0; i < array.length; i++) { + if (array[i].equals(value)) { + occs++; } } return occs; @@ -156,7 +145,7 @@ public static String[] removeValue(String[] array, String valueToRemove) { ArrayList strings = new ArrayList(); Collections.addAll(strings, array); strings.remove(valueToRemove); - return strings.toArray(new String[array.length]); + return strings.toArray(new String[0]); } /** @@ -168,11 +157,10 @@ public static String[] removeConsecutiveDuplicates(String[] array) { Collections.addAll(strings, array); for(int i = 0; i < strings.size(); i++){ if(i != 0 && strings.get(i).equals(strings.get(i - 1))){ - strings.remove(i); - i--; + strings.remove(i--); } } - return strings.toArray(new String[array.length]); + return strings.toArray(new String[0]); } /** @@ -183,12 +171,12 @@ public static String[] packConsecutiveDuplicates(String[] array) { ArrayList strings = new ArrayList(); Collections.addAll(strings, array); for(int i = 0; i < strings.size(); i++){ - if(i != 0 && strings.get(i).equals(strings.get(i - 1))){ - strings.set(i,strings.get(i) + strings.get(i - 1)); - strings.remove(i-1); + while(i != strings.size() -1 && strings.get(i).charAt(0) == (strings.get(i+1).charAt(0))){ + strings.set(i, strings.get(i) + strings.get(i+1)); + strings.remove(i+1); } } - return strings.toArray(new String[array.length]); + return strings.toArray(new String[0]); } From 0e62c3b470d8c2142014b0018296dc6478abe772 Mon Sep 17 00:00:00 2001 From: Quatrani Paul Date: Wed, 3 Nov 2021 14:28:38 -0400 Subject: [PATCH 6/6] code passes all tests --- src/main/java/com/zipcodewilmington/StringArrayUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 796de0e..16ee771 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -171,7 +171,7 @@ public static String[] packConsecutiveDuplicates(String[] array) { ArrayList strings = new ArrayList(); Collections.addAll(strings, array); for(int i = 0; i < strings.size(); i++){ - while(i != strings.size() -1 && strings.get(i).charAt(0) == (strings.get(i+1).charAt(0))){ + while(i != strings.size() -1 && strings.get(i).charAt(0) == (strings.get(i+1).charAt(0))){ strings.set(i, strings.get(i) + strings.get(i+1)); strings.remove(i+1); }