From abb229bfc0df3cd410eab5ee7293887de1188e39 Mon Sep 17 00:00:00 2001 From: TatianaDeAngelo <85692272+TatianaDeAngelo@users.noreply.github.com> Date: Thu, 1 Jul 2021 16:56:06 -0400 Subject: [PATCH] Tests all passed! Lots of comments for explanation --- .../java/io/zipcoder/StringsAndThings.java | 80 +++++++++++++++--- .../stringsandthings/CountYZTest.java | 1 + .../io/zipcoder/StringsAndThings.class | Bin 0 -> 2021 bytes .../ContainsEqualNumberOfIsAndNotTest.class | Bin 0 -> 1323 bytes .../stringsandthings/CountTripleTest.class | Bin 0 -> 1321 bytes .../stringsandthings/CountYZTest.class | Bin 0 -> 1372 bytes .../stringsandthings/GIsHappyTest.class | Bin 0 -> 1210 bytes .../stringsandthings/RemoveStringTest.class | Bin 0 -> 1308 bytes 8 files changed, 71 insertions(+), 10 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..6d3a45a 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -14,10 +14,23 @@ public class StringsAndThings { * countYZ("day fez"); // Should return 2 * countYZ("day fyyyz"); // Should return 2 */ - public Integer countYZ(String input){ - return null; + public Integer countYZ(String input) { + int count = 0; //counts the number of times the word ends in a 'y' or 'z' + String[] numberOfWords = input.split(" "); // this splits where there is a space + for (String word : numberOfWords) { + int charactersInWord = word.length(); //this is checking the length of characters in a word + char lastChar = word.charAt(charactersInWord - 1); // we are looking for last character, because we count starting + // at zero, we have to subtract 1 for it to give us the + // correct last character + if (lastChar == 'y' || lastChar == 'z') { //this is saying if the last character is either y or z then it will + // return our desired return + 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. @@ -27,10 +40,15 @@ public Integer countYZ(String input){ * 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 String removeString(String base, String remove) { + + return base.replace(remove, ""); //remove the characters that were passed through 'remove' + // because it replaces the characters with "" which is an empty string } + + + /** * 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) @@ -39,10 +57,29 @@ public String removeString(String base, String remove){ * containsEqualNumberOfIsAndNot("This is notnot") // Should return true * containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true */ + + public Boolean containsEqualNumberOfIsAndNot(String input){ - return null; + int is = 0; + int not = 0; + + for (int i =0; i <= input.length() - 3; i++) { // this is initiating at the beginning and running until 3 less than + // the length of the String "input" + if (input.startsWith("is", i)) { // IntelliJ recommended that I use the .startsWith instead of a substring. + // This allows it to check for "is" + is++; // This is allowing it to go to the next "is" + } else if (input.startsWith("not", i)) { // Similar to line 65 except I have "else if" because it is + // saying if it isn't "is", then look for "not" + not++; // Continues to look for other "not"s + } + } + + return is == not; // Finally we compare "is" and "not", this will check to see + // if the appearances are equal } + + /** * 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. @@ -50,11 +87,26 @@ public Boolean containsEqualNumberOfIsAndNot(String input){ * gHappy("xxgxx") // Should return false * gHappy("xxggyygxx") // Should return false */ - public Boolean gIsHappy(String input){ - return null; + // g must be lowercase + // g is only happy when directly next to another g + // all g's in the string must be happy for it to return true + // probably need to use char to detect a specific character, need lowercase to make sure it isn't a Gg or gG scenario + public Boolean gIsHappy(String input) { + for (int i = 0; i < input.length(); i++) { // the iterates and increments through + if (input.charAt(i) == 'g' && input.charAt(i + 1) == 'g') { // this says that I have a character 'g' and + // if there exists another 'g' next to that g + // then gishappy and will return true. + // (i + 1) just makes sure that the g's are next to each other. + // We didn't need to add an (i-1) + // because the left right part doesn't matter + // because when it finds 'g' it will read from + // left to right, so that first 'g' will always be the left g. + return true; + } + } + return false; } - /** * 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. @@ -62,7 +114,15 @@ public Boolean gIsHappy(String input){ * countTriple("xxxabyyyycd") // Should return 3 * countTriple("a") // Should return 0 */ - public Integer countTriple(String input){ - return null; + public Integer countTriple(String input){ + int count = 0; //counts number of times + + for (int i = 0; i <= input.length() - 3; i++) { // the - 3 is there because we are looking for a three character string inside of a larger string + if (input.charAt(i) == input.charAt(i + 1) && input.charAt(i) == input.charAt(i+2)) + count++; + + } + return count; } + } diff --git a/src/test/java/io/zipcoder/stringsandthings/CountYZTest.java b/src/test/java/io/zipcoder/stringsandthings/CountYZTest.java index 35538ef..44bd40b 100644 --- a/src/test/java/io/zipcoder/stringsandthings/CountYZTest.java +++ b/src/test/java/io/zipcoder/stringsandthings/CountYZTest.java @@ -25,6 +25,7 @@ public void countYZTest1(){ Assert.assertEquals(expected, actual); } + @Test public void countYZTest2(){ String input = "day fez"; diff --git a/target/classes/io/zipcoder/StringsAndThings.class b/target/classes/io/zipcoder/StringsAndThings.class new file mode 100644 index 0000000000000000000000000000000000000000..b353a52cc4c6d1b37e95cfbd4777888efd15f9e4 GIT binary patch literal 2021 zcmai!O;g)e6o%hx$&zeL0|wKQ7)nB#V9aMi2!%lS1~+_U1}2%Ni$oX!N*vjdWg3|2 zj&=84l}Q(D+YQNdksr`$H|?&!B-3f3@0EoyDQy`>y7%aubD#5`bN$oNk8c4C;C>Vz z;D&*lQM6+~K5og!U=+78WZ+Y|88*-*-y;UPqqu`n9b-D~3TSt1$M(hql#bK`0d*?B zZV6miupMiu^nA^7SIxB?E%AkX*33OHU0d!0qv~zhMS<1@JKwu&7qX0XdsjTyb~cL> z&id+>T#N|l*?h_IK7S-|y<_32xo!65OlLFj97$D~GLC0$S}tSivRkHWW3@k=r6`}z3}M?Tlsvxu(*`fu*Lt4cw#vB$hR!MDpUJ8Vt5B(HW|4Yo<%9w)*}UVKwo{yWQ8II7 z&6hVaQop4<>Hkl$$$UO%nGWj}wm?YsLvduFv6(5(n}x!TK#i}!s%sZyE2Asx3npBOHnjv3oxn80KVQ<#on2D3WmVweZ}fEYf*y%-j-sAEYQmj#mlH3w|z zY3rBQo?2N#R+&z;4HpYJ``-#bw`E$+ayGp!e};*s$;p{u2DKxT;*8Xv9vh4oCvw}& zm8|6rfhIatweEBPb=NB7%q%sUuI{#+y%p<4$#SyRNLAn&dv(T2=c7f>biLw3TcS2q z=L}pjDc+DO+Mu=4`B8Pola}K$YI*hLp9Wr9)bUx*T`%8k5yG9XA%5T@gm~Zt=xE?N zR(3!m65R0!e8?w*MF!) z7(g>7afyT0hYG3~Ur@GD7!D&(q_HeLfP;bTUG=sAZr z3Z*atxCZr@&bp3T9qmHLRsIV~b%btd5bqFrhcGjILf^8Ovy4?3Yoxp4tk;lrRO{L8 zbGDUzDkKK3`y`A*2Uj6xOQAE!`3t?n%rm2mDr3r^no!eJQd|EH9qD)^p&lao4I-59 z0QKp3O<>UW6tSmL&)R4;zO{DdU*t(X*6l4n4@ApSIgr^nnKm zpra3I%A`t^3i?b%#3^+7BC7lq=;o>{VjZ+2=t77~wSORNjLTW;sg(9Ly`)@y%4kv+ z^$J(hNzO*y<~|zJN%cvvHdKi&OFaJlY!ywuG>4h2IhnQNf@1yzX7GISqJA@u(Z DjU$j@ 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..556ec648ddd0173f22453fac00953e644e289eef GIT binary patch literal 1323 zcmb7DZBG+H5Pr5Tv|JA;MG+Jb3PLH$$&0TTA`nSPnvzJ!0H(EUm(>8~*) zi*q%G)FuTo^p~X)mEfc<+$yiz-UP zUTZ@fIIf_?exXsfy$EWNZp0ftxflk^$NU4gY_1Ni4aczsSCo)zLy5>QVJ!WJy3Ang zI)U2|TT&Ne=Ef^6XlfHV3^0sd*8&rL=x4}M!@LRl=)NW%V=;{-6U$gh95es` literal 0 HcmV?d00001 diff --git a/target/test-classes/io/zipcoder/stringsandthings/CountTripleTest.class b/target/test-classes/io/zipcoder/stringsandthings/CountTripleTest.class new file mode 100644 index 0000000000000000000000000000000000000000..29d3cfe89a19ec698f6f56f4b5783c566258b309 GIT binary patch literal 1321 zcmbW0YflqF6o%jFZA-gKDHr7`1(dcRtALt_F+rjUNtHxMgP+WFJCr45w{Ewj`man> 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..5f6c9334539b948900cdec1b8e2529f4a930fb33 GIT binary patch literal 1372 zcmbtTYfsZ)7(MT9Eu+c+7Vsj}r5XI8W7)TC?d{#RcY$h@*_%YQA9ES4$) zsZBNt43sTfZU-j^(%mx;8q5rpovPWWn64F`<7CP|vPhy|B8f$qc8yhvFgBgQ_V+*T zNzW%?+LM0J6d3=&taTzQmS+)h!?qpY^ex9GAj#aiaU4*Y@kSmxuH>Lr?cl5s$FiG& zANC%br>4;`?RvZ)dsX?lDXYG$2@H4Dw`^b5r5mnoR{g+i(6cV8SYbEO>Sf+Ff$Xjm zxK;Vi3SG%{?Y121&@h0Uz~oh~Yq*8m0?MI0%h$}7f)x#`SkrjvPZT`W@C?scW+~4W zfxakewOWje-N08lO>G@tb`Fl|hQMGa6{K`dKY?}DOFJ5x3BS^5d7@v9HTU2`poAbPX^y2 z%0bJW*Lhx$NynEoJuh_I+QM_FOYe!e?@BHJCyCkr3)6d=DXg*AX&Js5}V@6f#30vo{n{=piI~ Y9U(;uDN%?{Aqy0;ctasG^d*7WzxvM(0{{R3 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..97d69c05d93af48f51dd343f570612e004b2bfd8 GIT binary patch literal 1210 zcmb7@YflqV5Qg8=7FxCov_%n=i`=9w%F4wn#-x~_CJiRiG?8zo<+vQO>^6Hbt^dkI z!D!+S@JAWvY!`RIK*E>ZnK?7>Gc$Yk_n#lX0Bqu!hGEQSv7jM^`61|7%wb76_cSb{ ztf7Kx4lC+?KZgfdtY%>_6g=ORw&~UEgT|pU7-s6yF+R&q+c^-fv3KEoi(&AEw59)& zAziLC88WXZkzuqhZLu31w}ji^EsG+hy3^)Xle<#+Q842lN`kDMgG5bm`+!cV3FGx! z?+x#CdJW%yhA9NTd-v_oYk3~~@k|b-k?fBf6j!lY0b4}wYpw^6S@zHSw6%LyT zp*3oqxBY-y3}f{p{)rnFw@qW)aV)`YN-rcdBJ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