From d248584d564e52876f39cb7c6eaee42717dfe6a4 Mon Sep 17 00:00:00 2001 From: "chifangda@gmail.com" Date: Fri, 30 Apr 2021 19:43:19 -0700 Subject: [PATCH] Assignment Finished and Passed All Test Cases --- StringArrayUtils.java | 127 ++++++++++++++-- package.bluej | 330 +++++++++++++++++++++--------------------- 2 files changed, 282 insertions(+), 175 deletions(-) diff --git a/StringArrayUtils.java b/StringArrayUtils.java index e86810b..6de992a 100644 --- a/StringArrayUtils.java +++ b/StringArrayUtils.java @@ -1,5 +1,8 @@ - - +import java.util.Arrays; +import java.util.Deque; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; /** * Created by Yang on 1/23/20. */ @@ -9,7 +12,9 @@ public class StringArrayUtils { * @return first element of specified array */ // TODO public static String getFirstElement(String[] array) { - return null; + if(array == null || array.length == 0){ return null;} + + return array[0]; } /** @@ -17,7 +22,8 @@ public static String getFirstElement(String[] array) { * @return second element in specified array */ public static String getSecondElement(String[] array) { - return null; + if(array == null || array.length < 2 ){ return null;} + return array[1]; } /** @@ -25,7 +31,8 @@ public static String getSecondElement(String[] array) { * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { - return null; + if(array == null || array.length == 0 ){ return null;} + return array[array.length - 1]; } /** @@ -33,7 +40,8 @@ public static String getLastElement(String[] array) { * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { - return null; + if(array == null || array.length < 2 ){ return null;} + return array[array.length - 2]; } /** @@ -42,6 +50,12 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { + if(array == null || array.length == 0 || value == null){return false;} + for(String s : array){ + if(s.equals(value)){ + return true; + } + } return false; } @@ -50,7 +64,20 @@ 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; + if(array == null){ return null;} + if(array.length < 2){ return array;} + int l = 0, r = array.length - 1; + while(l < r){ + swap(array,l++,r--); + } + return array; + } + + public static void swap(String[] array, int l, int r){ + if(l == r){return;} + String temp = array[l]; + array[l] = array[r]; + array[r] = temp; } /** @@ -58,7 +85,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; + if(array == null || array.length == 0 ) return false; + int l = 0, r = array.length - 1; + while(l <= r){ + if(!array[l++].equals(array[r--])){ + return false; + } + } + return true; } /** @@ -66,7 +100,24 @@ 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; + if(array == null || array.length == 0 ) return false; + int[] alph = new int[26]; + for(String s : array){ + String s_lc = s.toLowerCase(); + for(char c : s_lc.toCharArray()){ + if(c - 'a' < 0 || c - 'a' > 26){ + continue; + } + alph[c - 'a']++; + } + } + + for(int i = 0; i < alph.length; i++){ + if(alph[i] == 0){ + return false; + } + } + return true; } /** @@ -75,7 +126,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; + if(array == null || array.length == 0 || value == null){return 0;} + int res = 0; + for(String st : array){ + if(st.equals(value)){ + res++; + } + } + return res; } /** @@ -84,7 +142,23 @@ 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; + if(array == null || array.length == 0 || valueToRemove == null) return array; + int newLength = 0; + for(String s : array){ + if(!s.equals(valueToRemove)){ + newLength++; + } + } + + String[] res = new String[newLength]; + int index = 0; + for(String s : array){ + if(!s.equals(valueToRemove)){ + res[index++] = s; + } + } + + return res; } /** @@ -92,7 +166,21 @@ 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; + if(array == null || array.length == 0) return array; + Deque deque = new ArrayDeque(); + deque.add(array[0]); + for(String st : array){ + if(!deque.peekLast().equals(st)){ + deque.offerLast(st); + } + } + int l = deque.size(); + String[] res = new String[l]; + int index = 0; + while(!deque.isEmpty()){ + res[index++] = deque.pollFirst(); + } + return res; } /** @@ -100,7 +188,20 @@ 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; + if(array == null || array.length == 0) return array; + List list = new ArrayList(); + int start = 0, end = 0; + while(end < array.length){ + String s = ""; + while(end < array.length && array[start] == array[end]){ + s += array[start]; + end++; + } + list.add(s); + start = end; + } + + return list.toArray(new String[list.size()]); } diff --git a/package.bluej b/package.bluej index e451ca6..eac43b9 100644 --- a/package.bluej +++ b/package.bluej @@ -1,162 +1,168 @@ -#BlueJ package file -dependency1.from=GetFirstElementTest -dependency1.to=StringArrayUtils -dependency1.type=UsesDependency -dependency10.from=RemoveConsecutiveDuplicatesTest -dependency10.to=StringArrayUtils -dependency10.type=UsesDependency -dependency11.from=IsPalindromicTest -dependency11.to=StringArrayUtils -dependency11.type=UsesDependency -dependency12.from=GetSecondElementTest -dependency12.to=StringArrayUtils -dependency12.type=UsesDependency -dependency13.from=ContainsTest -dependency13.to=StringArrayUtils -dependency13.type=UsesDependency -dependency2.from=ReverseTest -dependency2.to=StringArrayUtils -dependency2.type=UsesDependency -dependency3.from=StringArrayUtilsTest -dependency3.to=StringArrayUtils -dependency3.type=UsesDependency -dependency4.from=GetNumberOfOccurrencesTest -dependency4.to=StringArrayUtils -dependency4.type=UsesDependency -dependency5.from=GetSecondToLastElement -dependency5.to=StringArrayUtils -dependency5.type=UsesDependency -dependency6.from=GetLastElementTest -dependency6.to=StringArrayUtils -dependency6.type=UsesDependency -dependency7.from=RemovePackDuplicatesTest -dependency7.to=StringArrayUtils -dependency7.type=UsesDependency -dependency8.from=RemoveValueTest -dependency8.to=StringArrayUtils -dependency8.type=UsesDependency -dependency9.from=IsPangramicTest -dependency9.to=StringArrayUtils -dependency9.type=UsesDependency -editor.fx.0.height=704 -editor.fx.0.width=800 -editor.fx.0.x=224 -editor.fx.0.y=155 -objectbench.height=107 -objectbench.width=837 -package.divider.horizontal=0.6397146254458977 -package.divider.vertical=0.8235294117647058 -package.editor.height=509 -package.editor.width=727 -package.editor.x=77 -package.editor.y=158 -package.frame.height=704 -package.frame.width=861 -package.numDependencies=13 -package.numTargets=14 -package.showExtends=true -package.showUses=true -project.charset=UTF-8 -readme.height=58 -readme.name=@README -readme.width=47 -readme.x=10 -readme.y=10 -target1.height=50 -target1.name=ReverseTest -target1.showInterface=false -target1.type=UnitTestTargetJunit4 -target1.width=90 -target1.x=70 -target1.y=60 -target10.height=50 -target10.name=RemoveConsecutiveDuplicatesTest -target10.showInterface=false -target10.type=UnitTestTargetJunit4 -target10.width=220 -target10.x=300 -target10.y=290 -target11.height=50 -target11.name=StringArrayUtils -target11.showInterface=false -target11.type=ClassTarget -target11.width=110 -target11.x=170 -target11.y=10 -target12.height=50 -target12.name=IsPalindromicTest -target12.showInterface=false -target12.type=UnitTestTargetJunit4 -target12.width=120 -target12.x=620 -target12.y=280 -target13.height=50 -target13.name=GetSecondElementTest -target13.showInterface=false -target13.type=UnitTestTargetJunit4 -target13.width=160 -target13.x=500 -target13.y=130 -target14.height=50 -target14.name=ContainsTest -target14.showInterface=false -target14.type=UnitTestTargetJunit4 -target14.width=100 -target14.x=360 -target14.y=190 -target2.height=50 -target2.name=StringArrayUtilsTest -target2.showInterface=false -target2.type=UnitTestTargetJunit4 -target2.width=140 -target2.x=10 -target2.y=120 -target3.height=50 -target3.name=GetFirstElementTest -target3.showInterface=false -target3.type=UnitTestTargetJunit4 -target3.width=140 -target3.x=10 -target3.y=180 -target4.height=50 -target4.name=GetNumberOfOccurrencesTest -target4.showInterface=false -target4.type=UnitTestTargetJunit4 -target4.width=200 -target4.x=60 -target4.y=310 -target5.height=50 -target5.name=GetSecondToLastElement -target5.showInterface=false -target5.type=UnitTestTargetJunit4 -target5.width=170 -target5.x=20 -target5.y=450 -target6.height=50 -target6.name=GetLastElementTest -target6.showInterface=false -target6.type=UnitTestTargetJunit4 -target6.width=140 -target6.x=10 -target6.y=520 -target7.height=50 -target7.name=RemovePackDuplicatesTest -target7.showInterface=false -target7.type=UnitTestTargetJunit4 -target7.width=180 -target7.x=450 -target7.y=440 -target8.height=50 -target8.name=RemoveValueTest -target8.showInterface=false -target8.type=UnitTestTargetJunit4 -target8.width=130 -target8.x=150 -target8.y=120 -target9.height=50 -target9.name=IsPangramicTest -target9.showInterface=false -target9.type=UnitTestTargetJunit4 -target9.width=120 -target9.x=300 -target9.y=510 +#BlueJ package file +dependency1.from=GetNumberOfOccurrencesTest +dependency1.to=StringArrayUtils +dependency1.type=UsesDependency +dependency10.from=ReverseTest +dependency10.to=StringArrayUtils +dependency10.type=UsesDependency +dependency11.from=StringArrayUtilsTest +dependency11.to=StringArrayUtils +dependency11.type=UsesDependency +dependency12.from=RemoveConsecutiveDuplicatesTest +dependency12.to=StringArrayUtils +dependency12.type=UsesDependency +dependency13.from=RemovePackDuplicatesTest +dependency13.to=StringArrayUtils +dependency13.type=UsesDependency +dependency2.from=GetSecondToLastElement +dependency2.to=StringArrayUtils +dependency2.type=UsesDependency +dependency3.from=GetLastElementTest +dependency3.to=StringArrayUtils +dependency3.type=UsesDependency +dependency4.from=RemoveValueTest +dependency4.to=StringArrayUtils +dependency4.type=UsesDependency +dependency5.from=IsPangramicTest +dependency5.to=StringArrayUtils +dependency5.type=UsesDependency +dependency6.from=IsPalindromicTest +dependency6.to=StringArrayUtils +dependency6.type=UsesDependency +dependency7.from=GetSecondElementTest +dependency7.to=StringArrayUtils +dependency7.type=UsesDependency +dependency8.from=GetFirstElementTest +dependency8.to=StringArrayUtils +dependency8.type=UsesDependency +dependency9.from=ContainsTest +dependency9.to=StringArrayUtils +dependency9.type=UsesDependency +editor.fx.0.height=838 +editor.fx.0.width=1550 +editor.fx.0.x=-7 +editor.fx.0.y=-7 +objectbench.height=101 +objectbench.width=1512 +package.divider.horizontal=0.6397146254458977 +package.divider.vertical=0.8536255411255411 +package.editor.height=624 +package.editor.width=1427 +package.editor.x=0 +package.editor.y=0 +package.frame.height=838 +package.frame.width=1550 +package.numDependencies=13 +package.numTargets=15 +package.showExtends=true +package.showUses=true +project.charset=UTF-8 +readme.height=60 +readme.name=@README +readme.width=49 +readme.x=10 +readme.y=10 +target1.height=50 +target1.name=ReverseTest +target1.showInterface=false +target1.type=UnitTestTargetJunit4 +target1.width=100 +target1.x=70 +target1.y=60 +target10.height=50 +target10.name=IsPangramicTest +target10.showInterface=false +target10.type=UnitTestTargetJunit4 +target10.width=130 +target10.x=300 +target10.y=510 +target11.height=50 +target11.name=RemoveConsecutiveDuplicatesTest +target11.showInterface=false +target11.type=UnitTestTargetJunit4 +target11.width=260 +target11.x=300 +target11.y=290 +target12.height=50 +target12.name=StringArrayUtils +target12.showInterface=false +target12.type=ClassTarget +target12.width=130 +target12.x=170 +target12.y=10 +target13.height=50 +target13.name=IsPalindromicTest +target13.showInterface=false +target13.type=UnitTestTargetJunit4 +target13.width=140 +target13.x=620 +target13.y=280 +target14.height=50 +target14.name=ContainsTest +target14.showInterface=false +target14.type=UnitTestTargetJunit4 +target14.width=110 +target14.x=360 +target14.y=190 +target15.height=50 +target15.name=GetSecondElementTest +target15.showInterface=false +target15.type=UnitTestTargetJunit4 +target15.width=180 +target15.x=500 +target15.y=130 +target2.height=50 +target2.name=StringArrayUtilsTest +target2.showInterface=false +target2.type=UnitTestTargetJunit4 +target2.width=160 +target2.x=10 +target2.y=120 +target3.height=50 +target3.name=GetFirstElementTest +target3.showInterface=false +target3.type=UnitTestTargetJunit4 +target3.width=160 +target3.x=0 +target3.y=190 +target4.height=50 +target4.name=GetNumberOfOccurrencesTest +target4.showInterface=false +target4.type=UnitTestTargetJunit4 +target4.width=230 +target4.x=60 +target4.y=310 +target5.height=50 +target5.name=GetSecondToLastElement +target5.showInterface=false +target5.type=UnitTestTargetJunit4 +target5.width=190 +target5.x=20 +target5.y=450 +target6.height=50 +target6.name=GetLastElementTest +target6.showInterface=false +target6.type=UnitTestTargetJunit4 +target6.width=160 +target6.x=10 +target6.y=520 +target7.height=70 +target7.name=README.md +target7.type=TextTarget +target7.width=120 +target7.x=160 +target7.y=180 +target8.height=50 +target8.name=RemovePackDuplicatesTest +target8.showInterface=false +target8.type=UnitTestTargetJunit4 +target8.width=210 +target8.x=450 +target8.y=440 +target9.height=50 +target9.name=RemoveValueTest +target9.showInterface=false +target9.type=UnitTestTargetJunit4 +target9.width=140 +target9.x=190 +target9.y=120