Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings and Things Lab #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 72 additions & 8 deletions src/main/java/io/zipcoder/StringsAndThings.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @author tariq
*/
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
Expand All @@ -15,9 +14,20 @@ public class StringsAndThings {
* countYZ("day fyyyz"); // Should return 2
*/
public Integer countYZ(String input){
return null;
int count = 0;
String[] arrayOfWords = input.split(" ");
for(String word : arrayOfWords) {
int numberOfCharInWord = word.length();
int lastIndex = numberOfCharInWord - 1;
char lastChar = word.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.
Expand All @@ -28,7 +38,11 @@ public Integer countYZ(String input){
* removeString("Hello there", "x") // Should return "Hello there"
*/
public String removeString(String base, String remove){
return null;
String deleteString = "";
if(base.contains(remove)){
deleteString = base.replace(remove, "");
} else deleteString = base;
return deleteString;
}

/**
Expand All @@ -40,7 +54,28 @@ public String removeString(String base, String remove){
* containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true
*/
public Boolean containsEqualNumberOfIsAndNot(String input){
return null;
//search for occurrences of "is" and "not" in string
//remove the string of letters so it does not repeat
//tally the number of occurrences for each

int numberOfIs = 0;
int numberOfNot = 0;
String currentString = input;
int indexOf = currentString.indexOf("not");

while(indexOf >= 0){
currentString = currentString.replaceFirst("not", "");
indexOf = currentString.indexOf("not");
numberOfNot++;
}

indexOf = currentString.indexOf("is");
while(indexOf >= 0){
currentString = currentString.replaceFirst("is", "");
indexOf = currentString.indexOf("is");
numberOfIs++;
}
return numberOfIs == numberOfNot;
}

/**
Expand All @@ -51,18 +86,47 @@ public Boolean containsEqualNumberOfIsAndNot(String input){
* gHappy("xxggyygxx") // Should return false
*/
public Boolean gIsHappy(String input){
return null;


boolean indexIsG = false;
for(int i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'g') {
if (input.charAt(i + 1) == 'g' || input.charAt(i -1) == 'g') {
indexIsG = true;
} else if (input.charAt(i+1) != 'g'){
indexIsG = false;
break;
} else if (input.charAt(i - 1) != 'g'){
indexIsG = false;
break;
}
}
}
return indexIsG;
}


/**
/**
* 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;
public Integer countTriple (String input){
int numberOfTriples = 0;
//loop through string
//find if charAt(i) == charAt(i+1) and (i+2)
//increase counter by 1

for(int i = 0; i < input.length() - 3; i++){
if(input.charAt(i) == input.charAt(i+1)){
if(input.charAt(i+1) == input.charAt(i+2)){
numberOfTriples++;
}
}
}

return numberOfTriples;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void gIsHappyTest2(){
@Test
public void gIsHappyTest3(){
Boolean actual = stringsAndThings.gIsHappy("xxggyygxx");
Assert.assertTrue(actual);
Assert.assertFalse(actual);
}

}
Binary file added target/classes/io/zipcoder/StringsAndThings.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.