From f920bbbe6a1b0d9be92751654b86de1161e88dd8 Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 20:05:31 -0400 Subject: [PATCH] finished lab but one test didn't pass --- .../java/io/zipcoder/StringsAndThings.java | 140 +++++++++++++++--- .../io/zipcoder/StringsAndThings.class | Bin 0 -> 3095 bytes .../ContainsEqualNumberOfIsAndNotTest.class | Bin 0 -> 1326 bytes .../stringsandthings/CountTripleTest.class | Bin 0 -> 1321 bytes .../stringsandthings/CountYZTest.class | Bin 0 -> 1375 bytes .../stringsandthings/GIsHappyTest.class | Bin 0 -> 1213 bytes .../stringsandthings/RemoveStringTest.class | Bin 0 -> 1311 bytes 7 files changed, 118 insertions(+), 22 deletions(-) create mode 100644 target/classes/io/zipcoder/StringsAndThings.class create mode 100644 target/test-classes/io/zipcoder/stringsandthings/ContainsEqualNumberOfIsAndNotTest.class create mode 100644 target/test-classes/io/zipcoder/stringsandthings/CountTripleTest.class create mode 100644 target/test-classes/io/zipcoder/stringsandthings/CountYZTest.class create mode 100644 target/test-classes/io/zipcoder/stringsandthings/GIsHappyTest.class create mode 100644 target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class diff --git a/src/main/java/io/zipcoder/StringsAndThings.java b/src/main/java/io/zipcoder/StringsAndThings.java index 073467a..936353f 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -1,57 +1,143 @@ package io.zipcoder; +import java.util.Arrays; + /** * @author tariq */ public class StringsAndThings { + public static void main(String[] args) { + + } /** * Given a string, count the number of words ending in 'y' or 'z' -- so the 'y' in "heavy" and the 'z' in "fez" count, * but not the 'y' in "yellow" (not case sensitive). We'll say that a y or z is at the end of a word if there is not an alphabetic * letter immediately following it. (Note: Character.isLetter(char) tests if a char is an alphabetic letter.) * example : countYZ("fez day"); // Should return 2 - * countYZ("day fez"); // Should return 2 - * countYZ("day fyyyz"); // Should return 2 + * countYZ("day fez"); // Should return 2 + * countYZ("day fyyyz"); // Should return 2 */ - public Integer countYZ(String input){ - return null; + public static Integer countYZ(String input) { + Integer count = 0; + String[] word = input.split(" "); //Split string by white spaces + String arrayString = Arrays.toString(word); //created new array + String newString; + Character lastLetter; + + //System.out.println(arrayString); + + for (int i = 0; i < word.length; i++) { + newString = word[i]; // assigned each word to newString + lastLetter = newString.charAt(newString.length() - 1); // assigned last letter of each word to lastLetter Character variable + + //System.out.println(lastLetter); + if (lastLetter.equals('y') || lastLetter.equals('z')) { // Adds to count if "y" or "z" is found + count++; + } + + } + + + return count; } /** * Given two strings, base and remove, return a version of the base string where all instances of the remove string have * been removed (not case sensitive). You may assume that the remove string is length 1 or more. * Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x". - * + *

* example : removeString("Hello there", "llo") // Should return "He there" - * removeString("Hello there", "e") // Should return "Hllo thr" - * removeString("Hello there", "x") // Should return "Hello there" + * removeString("Hello there", "e") // Should return "Hllo thr" + * removeString("Hello there", "x") // Should return "Hello there" */ - public String removeString(String base, String remove){ - return null; + public static String removeString(String base, String remove) { + Integer baseLength = base.length(); + Integer removeLength = remove.length(); + char[] charArray = base.toCharArray(); + Integer j = 0; + + + for (int i = 0; i < baseLength; i++) { + + + if (i < baseLength - removeLength + 1 && base.substring(i, i + removeLength).equals(remove) ) { + i += removeLength - 1; + + + } else { + charArray[j] = base.charAt(i); + j++; + } + } + + String str = String.valueOf(charArray); + String returnStr = str.substring(0, j); + + return returnStr; } /** * Given a string, return true if the number of appearances of "is" anywhere in the string is equal * to the number of appearances of "not" anywhere in the string (case sensitive) - * + *

* example : containsEqualNumberOfIsAndNot("This is not") // Should return false - * containsEqualNumberOfIsAndNot("This is notnot") // Should return true - * containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true + * containsEqualNumberOfIsAndNot("This is notnot") // Should return true + * containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true */ - public Boolean containsEqualNumberOfIsAndNot(String input){ - return null; + public static Boolean containsEqualNumberOfIsAndNot(String input) { + Integer countNot = 0; + Integer countIs = 0; + + for (int i = 0; i < input.length(); i++) { + + + if (i < input.length() - 2 && input.substring(i, i + 3).equals("not")) { + countNot++; + i += 2; + + + } else if (i < input.length() - 1 && input.substring(i, i + 2).equals("is")) { + countIs++; + i += 1; + } + } + + return countNot == countIs; } /** * We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. * Return true if all the g's in the given string are happy. * example : gHappy("xxggxx") // Should return true - * gHappy("xxgxx") // Should return false - * gHappy("xxggyygxx") // Should return false + * gHappy("xxgxx") // Should return false + * gHappy("xxggyygxx") // Should return false */ - public Boolean gIsHappy(String input){ - return null; + public static Boolean gIsHappy(String input) { // one of the tests didn't pass don't really know why?? will solve later + Boolean happy = true; + + for (int i = 0; i < input.length(); i++) { + + if (input.length() == 1 && input.charAt(i) == 'g') { + happy = false; + break; + } + else if (i < input.length() - 1 + && input.charAt(i) == 'g' + && input.charAt(i + 1) != 'g' + && input.charAt(i - 1) != 'g') + { + happy = false; + break; + } else if (i == input.length() - 1 + && input.charAt(i) == 'g' + && input.charAt(i - 1) != 'g') { + happy = false; + break; + } + } + return happy; } @@ -59,10 +145,20 @@ public Boolean gIsHappy(String input){ * We'll say that a "triple" in a string is a char appearing three times in a row. * Return the number of triples in the given string. The triples may overlap. * example : countTriple("abcXXXabc") // Should return 1 - * countTriple("xxxabyyyycd") // Should return 3 - * countTriple("a") // Should return 0 + * countTriple("xxxabyyyycd") // Should return 3 + * countTriple("a") // Should return 0 */ - public Integer countTriple(String input){ - return null; + public Integer countTriple(String input) { + + Integer strLength = input.length(); + int count = 0; + for (int i = 0; i < strLength-2; i++) { + + char tmp = input.charAt(i); + if (tmp == input.charAt(i+1) && tmp == input.charAt(i+2)) { + count++; + } + } + return count; } } diff --git a/target/classes/io/zipcoder/StringsAndThings.class b/target/classes/io/zipcoder/StringsAndThings.class new file mode 100644 index 0000000000000000000000000000000000000000..95826497bbde3cdf707788ab705d8b87c789c54d GIT bin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4wrqWAH+-J1`>dmP+h;eszg8D=O>|wGN z?L?w6NqZaM#}xMjbtBmJ!uXGf>?N)DXupU`!^=E!q9gR5by1zEZ(~X-KBlKt{Y6eA zLusA)j~Qu$EyZOW`&36HW39s!Y3R)Kmu^b3sfa%A58w{(Us4fw8Sp>2>Q~%_U*kCY z6WCBmoS>+Cct)S5P7LnXF_n%^-!t_iUs`g+$&Ne8w(PhRYd*lVql4`c#P4H|Y#dEw MLCZ<{$MDL30S=CWbN~PV literal 0 HcmV?d00001 diff --git a/target/test-classes/io/zipcoder/stringsandthings/ContainsEqualNumberOfIsAndNotTest.class b/target/test-classes/io/zipcoder/stringsandthings/ContainsEqualNumberOfIsAndNotTest.class new file mode 100644 index 0000000000000000000000000000000000000000..04fd35c91aee3e7a9f8bfb4f5ce3d0943da2dda0 GIT binary patch literal 1326 zcmb7@`%V)<6vn?PEiJptttf&5auZ5XRxVyKL?DuoG>}MBBmYgyP^MXCaWAI$RwgP$ z6Cc2bGM?G4ZpDBh+04$IIp_PGIcMg_&#&JBtl*h}ZcJt|WgvygE?Ah(;htX17??%D zz#NJ>%N(O5T z9fi3HLwb`O8G1`niJkDcD!ekUI%LV0+!}W(+>?488`Hs&B*@FZAaN309ndZ%VQ;!B z;8OXo-i6$0iS8b5>nwNNpe+1=(#ZHC2u~P>UxzA?$D$&ANt|`1+<*ttRfM4wyt>^8 zDPMa-9J-#Mz+RzMxBXaZkxs;G0ht&EN)7&=+YVQCd&6}d!4(DcwW-AL3mQxRs4g=Y zdv55}#Fo^>nECIK7Bt#IANm#gp-FXPSZgOCd#acn0>7CMgWi*clm3+@QJDa==Xt(u(001*f9m=je#` ztl~GM-kzc3Gt!BMexM*mI) z=^g@u(lIHSJcc6q!B$u-Zz6{~7$LFWpwS34b{VKE0T~xTBLo^H(AYIVcP|5(SAZr7 QG)184Yk zh$j93f0Xgv?Mll{6ZXUGnKN_V=bSS$zkYxJ31AgZQqVD@VK#*bX8MrETncw_FNt~I zlTV?5ViF5UENZx~VM!p<^jyoXHP`Lx&c1I5OqDFh_+&Lqr>b0I``r4PK;oHYTi$bl zXuePuh;4AAK!3@y)tlB~MY%h&Ql}?da!gq-OV{$}p)=;~TZAl~frLp&yUJB2VQe@p z+uL!i2IExIBQ)MrUaKK6`j#u!p(m9k?Re6&9Gjv9x;5jV#q5k1YR_>M12g7X zt%m|h)ov)$Q&oY%(t$jZMqS!9W7GCjO}T!B()3!g&fp%!Fp=|m%NG$V0;z4M<(lf1 z=PIpT z9+&AJ-FmaBTxOulzIgSL7=}vG6 ziBonA<4m%jB~8$Z;4gSG08d>6Cr-hASAxd~9;fI8L6g@4-?<2mo`O?Xf^!7V5Ijrp M+_k{dtUH3-AHtao{r~^~ literal 0 HcmV?d00001 diff --git a/target/test-classes/io/zipcoder/stringsandthings/CountYZTest.class b/target/test-classes/io/zipcoder/stringsandthings/CountYZTest.class new file mode 100644 index 0000000000000000000000000000000000000000..1da6b583441b6b6dfd7d181f523ca082ec5c5019 GIT binary patch literal 1375 zcmbtTYfsZ)7(MT9Eu+c+GTPY-ar`S24MY=v zfIrH3+BLSAg_!WAectDC&Uwz;{`&p>CxAzIo`;4x1^4qvVXhDTc%WiFj}pp3rjo}3 z7F8^%SXQ7bcqmYCJ;$_~?uOOaI|>4Usj6w~XJ)%@H>9KQ{!?EU$h|Zz(|aY5E|qHn znN2nd3{*`^Zu=(((%CZ(TFeYp?Yhyb8IBp8qh!WAGD%XoL=uTGtOlzTp>Nv0Yi`3=vjg)QkW!Kz0A8Nkl(d^ zr!L=`fh&c?Zp(oV4Ff0$OkU->h8wskpd8AxV#DYtSkbVGHI0}4SiutwPw|XpmWpf< z=nJDxr^C3E1is2?YKwi@IXI>p0)ufXvVuIPM$4BwhXNy|t#VJZl*oC3`BI{x`!%ii zo+-pPxZ%3eA-iUT!JAJso|k+HtE+H5fgWYbHO%na*Z5Bee)e>ZQI;`%=5vrIoo^9j zE9Vej7^E=7XD&?kW0>q;&R$c=Ob;db Z|57qfB_%2;Q%QwN7Otsemd>Pb?+*;m4;%mh literal 0 HcmV?d00001 diff --git a/target/test-classes/io/zipcoder/stringsandthings/GIsHappyTest.class b/target/test-classes/io/zipcoder/stringsandthings/GIsHappyTest.class new file mode 100644 index 0000000000000000000000000000000000000000..cb067de752dcdca688531fd1be4e68c754bb1b2a GIT binary patch literal 1213 zcmb7@X-^YT6o%j1g|^dSX{!jzBAc{D8ChI0CWQnwX(W-RM!wy)*K(U-rkTYQ|CNaf z(ZnC%k20P+9h`!JgfBB^yYF+(nLEG#{P+c66VEgZV?Koi4Kd7TpkpzOCFR`Hu#BRH z63S_;sQ3Le9;C3Eg26E6dXBW(ZpCWV4wb<$QW%xF zce}?m;d(?&y2A6j43qDC%acci>3EDMv;xYi&?14Bt^vABbHyFXENWQNu&lw%;7$g21@fNnDtp)4up8B8Xb4PJlw*EU2X&_*U32@)`MN;*v9gu_ zL?Br#)dW({7$`7UQMP;)?C(goYVEY>F)B{oYSk=Ph5Oi<@|%hf^DjtTgk?8)$|B4y zx$hjxHoGc4pVU52`oV#~*j8ZsYG2lrrzpE&+m3Jf%CV_RqPuJE1uV~eD&ISYUMU%tL6OJC28we!D8k-$<-r3`@1a!CSUIZQ%dq>ynDfPN2-npm^&y#K_ZbioP51c#i za=u%WO9wYe`sg6eYus~Ff@f{sK))|W_BjjD+!sQfOz#lqBn%z)e<}XHS!~5{zy4 yHx-c!=a93f