Skip to content

Instantly share code, notes, and snippets.

@Prince-Shaikh
Created April 18, 2019 12:05
Show Gist options
  • Select an option

  • Save Prince-Shaikh/a77a95c2d22e15d374c35efc758ac63a to your computer and use it in GitHub Desktop.

Select an option

Save Prince-Shaikh/a77a95c2d22e15d374c35efc758ac63a to your computer and use it in GitHub Desktop.
Solution to the 2nd Weakly Programming Task
public class Weak2 {
/*
Q1. We have a number of bunnies and each bunny has two big floppy ears.
We want to compute the total number of ears across all the bunnies
recursively (without loops or multiplication).
bunnyEars(0) → 0
bunnyEars(1) → 2
bunnyEars(2) → 4
bunnyEars(234) → 468
Q2. 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).
equalIsNot("This is not") → false
equalIsNot("This is notnot") → true
equalIsNot("noisxxnotyynotxisi") → true
*/
public static void main(String args[]){
System.out.println(bunnyEars(0));
System.out.println(bunnyEars(1));
System.out.println(bunnyEars(2));
System.out.println( bunnyEars(234));
System.out.println(equalIsNot("This is not"));
System.out.println(equalIsNot("This is notnot"));
System.out.println(equalIsNot("noisxxnotyynotxisi"));
}
/**
* Computes the total number of ears across all the bunnies
* @param num The number of bunnies
* @return total number of ears
*/
public static int bunnyEars(int num){ return num + num; }
/**
*Checks if the string have equal number of "is" and "not"
* @param str The String to check
* @return True or False based on the result
*/
public static boolean equalIsNot(String str){
int index = str.indexOf("is");
int countIs=0,countNot=0;
String copy = str;
while (index !=-1){
countIs++;
str = str.substring(index+1);
index = str.indexOf("is");
}
index = copy.indexOf("not");
while (index !=-1){
countNot++;
copy = copy.substring(index+1);
index = copy.indexOf("not");
}
return countIs == countNot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment