diff --git a/src/main/java/io/zipcoder/StringsAndThings.java b/src/main/java/io/zipcoder/StringsAndThings.java index 073467a..8a25e9e 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -1,5 +1,6 @@ package io.zipcoder; +import java.util.regex.Pattern; /** * @author tariq @@ -11,58 +12,99 @@ 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) { + //Count the letters need counter variable + int count = 0; + // Split word in two + String[] parts = input.split(" "); + // Place in a loop to iterate the characters + for (int i = 0; i < parts.length; i++) { + // Check to see if words ends with y or z + if (parts[i].trim().endsWith("y") || parts[i].trim().endsWith("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" - */ - public String removeString(String base, String remove){ - return null; - } - - /** - * 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 - */ - public Boolean containsEqualNumberOfIsAndNot(String input){ - return null; - } - - /** - * 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 + * removeString("Hello there", "e") // Should return "Hllo thr" + * removeString("Hello there", "x") // Should return "Hello there" */ - public Boolean gIsHappy(String input){ - return null; + public String removeString(String base, String remove) { + String one = base + remove; + one = (one.replace(remove, "")); + return one; } + /** + * 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 + */ + public Boolean containsEqualNumberOfIsAndNot (String input){ + // Place input into a split string variables + int one = (input.split("is", -1).length) - 1; + int two = (input.split("not", -1).length) - 1; + // If string equals "is" and "not" return true + if (one == two) { + return true; + } else { + return false; + } + } + /** + * 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 + */ + public Boolean gIsHappy (String input) { + // Check if input contains consecutive string + if (input.contains("gg")) { + // If true return true + return true; + } else { + // or return false + 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. - * example : countTriple("abcXXXabc") // Should return 1 - * countTriple("xxxabyyyycd") // Should return 3 - * countTriple("a") // Should return 0 - */ - public Integer countTriple(String input){ - return null; - } -} + /** + * 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 + */ + public Integer countTriple (String input) { + // Setup counter to count result + int counter = 0; + // Setup char variable + char c = 0; + // Setup for loop to iterate through characters + for (int i = 0; i < input.length() - 1; i++) { + // Find characters + c = input.charAt(i); + System.out.println(c); + // If character equals another characters run counter + if (c == input.charAt(i + 1) && c == input.charAt(i + 2)) { + counter++; + } + } + return counter; + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java index 020cd3d..daa9c16 100644 --- a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java +++ b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java @@ -8,32 +8,33 @@ /** * @author leon on 29/01/2019. */ -public class GIsHappyTest { + public class GIsHappyTest { private StringsAndThings stringsAndThings; @Before - public void setup(){ + public void setup() { stringsAndThings = new StringsAndThings(); } @Test - public void gIsHappyTest1(){ + public void gIsHappyTest1() { Boolean actual = stringsAndThings.gIsHappy("xxggxx"); Assert.assertTrue(actual); } @Test - public void gIsHappyTest2(){ + public void gIsHappyTest2() { Boolean actual = stringsAndThings.gIsHappy("xxgxx"); Assert.assertFalse(actual); } @Test - public void gIsHappyTest3(){ + public void gIsHappyTest3() { Boolean actual = stringsAndThings.gIsHappy("xxggyygxx"); Assert.assertTrue(actual); - } + + } } diff --git a/target/classes/io/zipcoder/StringsAndThings.class b/target/classes/io/zipcoder/StringsAndThings.class new file mode 100644 index 0000000..da1789c 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..fd51ca8 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..20658c5 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..00174b6 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..65ae362 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..ba429b6 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class differ