Skip to content

Instantly share code, notes, and snippets.

@samiullah-dev
Last active April 23, 2019 02:52
Show Gist options
  • Select an option

  • Save samiullah-dev/e9d9b1618b0325768cdee8e67de2a784 to your computer and use it in GitHub Desktop.

Select an option

Save samiullah-dev/e9d9b1618b0325768cdee8e67de2a784 to your computer and use it in GitHub Desktop.
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
________________________________________________________________________________________________________________________________________
package projects;
import java.util.*;
public class FloppyEars {
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
System.out.println("Enter number of bunnies: ");
int numOB = input.nextInt();
input.close();
int floppyEars = numOB + numOB;
System.out.println("Number of Bunnies's Floppy ears: " + floppyEars);
}
________________________________________________________________________________________________________________________________________
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
________________________________________________________________________________________________________________________________________
package projects;
import java.util.*;
public class IsNotRunner {
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
System.out.println("Enter any string: ");
String s = input.nextLine();
input.close();
IsNot iN = new IsNot();
iN.isNot(s);
boolean answer = iN.isNot(s);
System.out.println(answer);
}
}
package projects;
public class IsNot {
public boolean isNot(String b)
{
String a = b + "last";
/* I have used the new variable named "a" and initialized to "b + last" as I have
* used split method which cannot split the last word(according to my need),if user
* inputed not or is at the end my program would not mention that so I need an
* extra word that come at the end.*/
String[] str1 = a.split("is");
String[] str2 = a.split("not");
for (int i = 0; i < str.length; i++) {}
for (int x = 0; x < str2.length; x++) {}
if (str1.length == str2.length)
return true;
else
return false;
}
}
________________________________________________________________________________________________________________________________________
@waqaskhan409
Copy link

Great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment