diff --git a/src/main/java/io/zipcoder/StringsAndThings.java b/src/main/java/io/zipcoder/StringsAndThings.java index 073467a..3cbbdf7 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -11,25 +11,63 @@ public class StringsAndThings { * 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 Integer countYZ(String input) { + + + String[] words = input.split(" "); + Integer numOfWords = 0; + + for (String word : words) { + if (word.charAt(word.length() - 1) == 'y' || word.charAt(word.length() - 1) == 'z') { + numOfWords++; + } + } + return numOfWords; } + + + /*int count = 0; + String[] theArray = input.split(" "); + for (int i = 0; i < theArray.length; i++) { + String words = theArray[i]; + int numCharact = words.length(); // THIS ALSO WORKS + int lastIndex = numCharact - 1; + char lastChar = words.charAt(lastIndex); + if (lastChar == 'y' || lastChar == 'z') { + + 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 String removeString(String base, String remove) { + + return base.replace(remove, ""); + + + + } /** * Given a string, return true if the number of appearances of "is" anywhere in the string is equal @@ -40,7 +78,20 @@ public String removeString(String base, String remove){ * containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true */ public Boolean containsEqualNumberOfIsAndNot(String input){ - return null; +Integer theIs = 0; +Integer theNot = 0; + +for (int theChar = 0; theChar < input.length() -1; theChar ++ ) { + + if (input.charAt(theChar) == 'i' && input.charAt(theChar + 1) == 's') { // esas letras tiene "is" (la i y s) + theIs++; + } + if (input.charAt(theChar) == 'n' && input.charAt(theChar + 1) == 'o' && // estas tiene "not" (n y o y t) + input.charAt(theChar + 2) == 't') { + theNot++; + } +} + return theIs.equals(theNot); } /** @@ -50,10 +101,29 @@ public Boolean containsEqualNumberOfIsAndNot(String input){ * gHappy("xxgxx") // Should return false * gHappy("xxggyygxx") // Should return false */ - public Boolean gIsHappy(String input){ - return null; + public Boolean gIsHappy(String input) { + + Boolean gIsHappy = false; + for (int i = 0; i < input.length() - 1; i++) { + if (input.charAt(i) == 'g' && input.charAt(i + 1) == 'g') { + gIsHappy = true; + //break; + } + } + return gIsHappy; } + /*return input.contains("gg"); + int counter = 0; + int gIsHappy = 0; + for (int i = 0; i < input.length() -1; i++){ + if (input.charAt(i) == 'g' && input.length() -1 ) + //if (input.charAt(i) == 'g' && input.charAt(i + 1) == 'g') { + + + + return input.contains("gg");*/ + /** * We'll say that a "triple" in a string is a char appearing three times in a row. @@ -62,7 +132,27 @@ 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 counter = 0; + input = input.toLowerCase(); + for (int i = 0; i <= input.length() - 1; i++) { + if (input.charAt(i) == 'x' && input.charAt(i+1) == 'x' && input.charAt(i+2) == 'x' || + input.charAt(i) == 'y' && input.charAt(i+1) == 'y' && input.charAt(i+2) == 'y' ) { + counter++; + } + } + return counter; } } + + /// + + + + + + + + + + diff --git a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java index 020cd3d..b411442 100644 --- a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java +++ b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java @@ -26,7 +26,7 @@ public void gIsHappyTest1(){ @Test public void gIsHappyTest2(){ - Boolean actual = stringsAndThings.gIsHappy("xxgxx"); + Boolean actual = stringsAndThings.gIsHappy("gxxgxx"); Assert.assertFalse(actual); } diff --git a/target/classes/io/zipcoder/StringsAndThings.class b/target/classes/io/zipcoder/StringsAndThings.class new file mode 100644 index 0000000..e0919c5 Binary files /dev/null and b/target/classes/io/zipcoder/StringsAndThings.class differ 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 0000000..556ec64 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/ContainsEqualNumberOfIsAndNotTest.class differ 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 0000000..29d3cfe Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/CountTripleTest.class differ 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 0000000..27178e4 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/CountYZTest.class differ 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 0000000..2e15e58 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/GIsHappyTest.class differ diff --git a/target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class b/target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class new file mode 100644 index 0000000..715f4d3 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class differ