diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..184d918 Binary files /dev/null and b/.DS_Store differ diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..2aba511 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000..94cdb99 Binary files /dev/null and b/src/main/.DS_Store differ diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store new file mode 100644 index 0000000..834a489 Binary files /dev/null and b/src/main/java/.DS_Store differ diff --git a/src/main/java/io/.DS_Store b/src/main/java/io/.DS_Store new file mode 100644 index 0000000..94c1667 Binary files /dev/null and b/src/main/java/io/.DS_Store differ diff --git a/src/main/java/io/zipcoder/StringsAndThings.java b/src/main/java/io/zipcoder/StringsAndThings.java index 073467a..86881d6 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -1,48 +1,93 @@ package io.zipcoder; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * @author tariq */ -public class StringsAndThings { +public class StringsAndThings { /** * 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 Integer countYZ(String input) { + + Integer count = 0; + + // f e z d a y + // 0 1 2 3 4 5 6 + for (int i = 0; i < input.length(); i++) { + if (input.charAt(i) == 'y' || input.charAt(i) == 'z') { + if (i == input.length() - 1) { + count++; + } else if (!Character.isLetter(input.charAt(i + 1))) { + 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) { + + String removeString = base.replace(remove, ""); + System.out.println(removeString); + + return removeString; + } + /** * 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 Boolean containsEqualNumberOfIsAndNot(String input) { + + Pattern isP = Pattern.compile("is"); + Matcher isM = isP.matcher(input); + Pattern notP = Pattern.compile("not"); + Matcher notM = notP.matcher(input); + + int matchIs = 0; + int matchNot = 0; + + while (isM.find()) { + matchIs++; + } + + while (notM.find()) { + matchNot++; + } + + return (matchIs == matchNot); + } + + + /** * 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. @@ -51,7 +96,17 @@ public Boolean containsEqualNumberOfIsAndNot(String input){ * gHappy("xxggyygxx") // Should return false */ public Boolean gIsHappy(String input){ - return null; + String gIsHappy = ""; + for (int i = 0; i < input.length(); i++ ) { + if (input.charAt(i) == 'g') { + if (input.charAt(i-1) == 'g' || input.charAt(i+1) == 'g') { + gIsHappy = "happy"; + } else { + gIsHappy = "sad"; + } + } + } + return (gIsHappy == "happy"); } @@ -63,6 +118,24 @@ public Boolean gIsHappy(String input){ * countTriple("a") // Should return 0 */ public Integer countTriple(String input){ - return null; + int count = 0; +// int length = input.length(); +// int lengthMinusOne = input.length()-1; + for (int i = 0; i < input.length()-1; i++){ +// int lengthMinusFour = input.length() - 3; + if (i < input.length() - 3) { +// char charAti = input.charAt(i); +// char charAtiPlusOne = input.charAt(i + 1); +// char charAtiPlusTwo = input.charAt(i + 2); + if (input.charAt(i) == input.charAt(i + 1)) { + if (input.charAt(i + 1) == input.charAt(i + 2)) { + count++; + } + } + + } + + } + return count; } } diff --git a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java index 020cd3d..9c150d9 100644 --- a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java +++ b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java @@ -33,7 +33,7 @@ public void gIsHappyTest2(){ @Test public void gIsHappyTest3(){ Boolean actual = stringsAndThings.gIsHappy("xxggyygxx"); - Assert.assertTrue(actual); + 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..be9af00 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..3bc2f6c 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