Problem -1 The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we’re on vacation. Return true if we sleep in.
Example:
sleepIn(false, false) → true
sleepIn(true, false) → false
sleepIn(false, true) → true
Solution public boolean sleepIn(boolean weekday, boolean vacation) {
if (!weekday || vacation){
return true;
}
return false;
}
**!weekday is the same as weekday==false (if it isn’t weekday).
Problem -2 We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
Example:
monkeyTrouble(true, true) → true
monkeyTrouble(false, false) → true
monkeyTrouble(true, false) → false
Problem -3 Given two int values, return their sum. Unless the two values are the same, then return double their sum.
Example:
sumDouble(1, 2) → 3
sumDouble(3, 2) → 5
sumDouble(2, 2) → 8
Solution public int sumDouble(int a, int b) {
if (a==b){
return 2*(a+b);
}
return a+b;
}
Problem -4 Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21.
Example:
diff21(19) → 2
diff21(10) → 11
diff21(21) → 0
Solution public int diff21(int n) {
if (n>21){
return 2*(n-21);
}
return 21-n;
}
Problem -5 We have a loud talking parrot. The “hour” parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble.
Example:
parrotTrouble(true, 6) → true
parrotTrouble(true, 7) → false
parrotTrouble(false, 6) → false
Solution public boolean parrotTrouble(boolean talking, int hour) {
if (talking && (hour <7 || hour>20)){
return true;
}
return false;
}
Problem-6 Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.
Exmaple:
makes10(9, 10) → true
makes10(9, 9) → false
makes10(1, 9) → true
Solution public boolean makes10(int a, int b) {
if (a==10 || b==10 || a+b==10){
return true;
}
return false;
}
Problem-7 Given 2 int values, return true if one is negative and one is positive. Except if the parameter “negative” is true, then return true only if both are negative.
Example:
posNeg(1, -1, false) → true
posNeg(-1, 1, false) → true
posNeg(-4, -5, true) → true
Solution-1 public boolean posNeg(int a, int b, boolean negative) {
if (a<0 && b>0 && negative==false|| a>0 && b<0 && negative==false || negative == true && a <0 && b <0){
return true;
}
return false;
}
Shorter Solution: public boolean posNeg(int a, int b, boolean negative) { if (negative) { return (a < 0 && b < 0); } else { return ((a < 0 && b > 0) || (a > 0 && b < 0));
}
}
Problem-8 Given a string, return a new string where “not ” has been added to the front. However, if the string already begins with “not”, return the string unchanged. Note: use .equals() to compare 2 strings.
Example:
notString(“candy”) → “not candy”
notString(“x”) → “not x”
notString(“not bad”) → “not bad”
Solution public String notString(String str) {
if (str.length()>=3 && str.substring(0,3).equals(“not”)){
return str;
}
return “not “+ str;
}
//str.substring(0,3) ; begins at 0 and ends at 2 (doesn’t include 3).
Problem-9 Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive).
Example:
missingChar(“kitten”, 1) → “ktten”
missingChar(“kitten”, 0) → “itten”
missingChar(“kitten”, 4) → “kittn”
Solution public String missingChar(String str, int n) {
String x=str.substring(0,n); // the front of the string up to n
String y=str.substring(n+1); //the rest of the string without including n
return x+y; //concatenate the front and the rest of the string
}
Problem 10 Given a string, return a new string where the first and last chars have been exchanged.
Example:
frontBack(“code”) → “eodc”
frontBack(“a”) → “a”
frontBack(“ab”) → “ba”
Solution public String frontBack(String str) {
if (str.length()<=1){
return str;
}
String a=str.substring(0,1); // first charof the string
String b=str.substring(1,str.length()-1); // mid chars of the string
String z=str.substring(str.length()-1); // last char of the string
return z+b+a; // concatenate last char+ mid chars + first char
}
Problem -11 Given a string, we’ll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.
Example:
front3(“Java”) → “JavJavJav”
front3(“Chocolate”) → “ChoChoCho”
front3(“abc”) → “abcabcabc”
Problem-13
Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back, so “kitten” yields”kikittenki”. If the string length is less than 2, use whatever chars are there.
Example
front22(“kitten”) → “kikittenki”
front22(“Ha”) → “HaHaHa”
front22(“abc”) → “ababcab”
Solution
public String front22(String str) {
if (str.length()<2){
return str+str+str;
}
String a=str.substring(0,2);
return a+str+a;
}
Problem-14 Given a string, return true if the string starts with “hi” and false otherwise.
Example
startHi(“hi there”) → true
startHi(“hi”) → true
startHi(“hello hi”) → false
Solution
public boolean startHi(String str) {
if (str.length()<2){
return false;
}
return (str.substring(0,2).equals(“hi”));
}
Problem -15 Given two temperatures, return true if one is less than 0 and the other is greater than 100.
Example
icyHot(120, -1) → true
icyHot(-1, 120) → true
icyHot(2, 120) → false
Solution
public boolean icyHot(int temp1, int temp2) {
return(temp1<0 && temp2>100 || temp1>100 && temp2<0);
}
Problem-16
Given 2 int values, return true if either of them is in the range 10..20 inclusive.
Example
in1020(12, 99) → true
in1020(21, 12) → true
in1020(8, 99) → false
Solution
public boolean in1020(int a, int b) {
return (a>=10 && a<=20 || b>=10 && b<=20);
}
Problem -17
We’ll say that a number is “teen” if it is in the range 13..19 inclusive. Given 3 int values, return true if 1 or more of them are teen.
Example
hasTeen(13, 20, 10) → true
hasTeen(20, 19, 10) → true
hasTeen(20, 10, 13) → true
Solution
public boolean hasTeen(int a, int b, int c) {
return ((a>=13 && a<=19) || (b>=13 && b<=19) || ( c>=13 && c<=19));
}
Problem -18
We’ll say that a number is “teen” if it is in the range 13..19 inclusive. Given 2 int values, return true if one or the other is teen, but not both.
Example
loneTeen(13, 99) → true
loneTeen(21, 19) → true
loneTeen(13, 13) → false
Solution
public boolean loneTeen(int a, int b) {
if ((a>=13 && a<=19) && (b>=13 && b<=19)){
return false;
}
return ((a>=13 && a<=19) || (b>=13 && b<=19));
} Another way (simplified method)
public boolean loneTeen(int a, int b){
boolean d=(a>=13 && a<=19); // d is true if “a” is between 13 & 19 inclusive
boolean f=(b>=13 && b<=19); // f is true if “b” is between 13 & 19 inclusive
return ((d && !f) || (!d && f));
}
Problem-19
Given a string, if the string “del” appears starting at index 1, return a string where that “del” has been deleted. Otherwise, return the string unchanged.
Example:
delDel(“adelbc”) → “abc”
delDel(“adelHello”) → “aHello”
delDel(“adedbc”) → “adedbc”
Problem -20
Return true if the given string begins with “mix”, except the ‘m’ can be anything, so “pix”, “9ix” .. all count.
Example:
mixStart(“mix snacks”) → true
mixStart(“pix snacks”) → true
mixStart(“piz snacks”) → false
Solution
public boolean mixStart(String str) {
return(str.length()>=3 && str.substring(1,3).equals(“ix”));
}
Problem -21
Given a string, return a string made of the first 2 chars (if present), however include first char only if it is ‘o’ and include the second only if it is ‘z’, so “ozymandias” yields “oz”.
Example
startOz(“ozymrb”) → “oz”
startOz(“bzoo”) → “z”
startOz(“oxx”) → “o”
Solution
public String startOz(String str) {
String y=””;
if( str.length()>=1 && str.charAt(0)==’o’) // for char use single quotations for string use double
{
y=y+str.charAt(0);
}
if( str.length()>=2 && str.charAt(1)==’z’){
y=y+str.charAt(1);
}
return y;
}
Problem- 22
Given three int values, a b c, return the largest.
Example:
intMax(1, 2, 3) → 3
intMax(1, 3, 2) → 3
intMax(3, 2, 1) → 3
Solution
public int intMax(int a, int b, int c) {
int largest=0;
if (a>b ){
largest=a;
}
else{
largest=b;
}
if (c>largest){
largest=c;
}
return largest;
}// or the longer way you can compare the numbers individually against each other
Problem-23
Given 2 int values, return whichever value is nearest to the value 10, or return 0 in the event of a tie. Note that Math.abs(n) returns the absolute value of a number.
Example
close10(8, 13) → 8
close10(13, 8) → 8
close10(13, 7) → 0
Solution
public int close10(int a, int b) {
if(Math.abs(a-10) < Math.abs(b-10)){
return a;
}
if(Math.abs(a-10) == Math.abs(b-10)){
return 0;
}
return b;
}
Problem-24
Given 2 int values, return true if they are both in the range 30..40 inclusive, or they are both in the range 40..50 inclusive.
Example:
in3050(30, 31) → true
in3050(30, 41) → false
in3050(40, 50) → true
Solution
public boolean in3050(int a, int b) {
if (a>=30 && a<=40 && b>=30 && b<=40){
return true;
}
if(a>=40 && a<=50 && b>=40 && b<=50){
return true;
}
return false;
}
Problem-25 Return true if the given string contains between 1 and 3 ‘e’ chars. Example:
stringE(“Hello”) → true
stringE(“Heelle”) → true
stringE(“Heelele”) → false
Solution
public boolean stringE(String str) {
int count=0;
for (int i=0;i<str.length();i++){
if(str.charAt(i)==’e’) count++;
}
return (count>=1 && count<=3);
}
Problem 26
Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % “mod” operator computes remainders, so 17 % 10 is 7. Example:
lastDigit(7, 17) → true
lastDigit(6, 17) → false
lastDigit(3, 113) → true
Solution
public boolean lastDigit(int a, int b) {
return(a%10==b%10);
}
Problem 27
Given a string, return a new string where the last 3 chars are now in upper case. If the string has less than 3 chars, uppercase whatever is there. Note that str.toUpperCase() returns the uppercase version of a string.
Example:
endUp(“Hello”) → “HeLLO”
endUp(“hi there”) → “hi thERE”
endUp(“hi”) → “HI”
Solution
public String endUp(String str) {
if(str.length()<=3){
return str.toUpperCase();
}
String y=str.substring(str.length()-3); // last three chars
return str.substring(0,str.length()-3) + y.toUpperCase();
}
Problem 28
Given a non-empty string and an int N, return the string made starting with char 0, and then every Nth char of the string. So if N is 3, use char 0, 3, 6, … and so on. N is 1 or more. Example:
everyNth(“Miracle”, 2) → “Mrce”
everyNth(“abcdefg”, 2) → “aceg”
everyNth(“abcdefg”, 3) → “adg”
Solution
public String everyNth(String str, int n) {
String h=””;
for (int i=0;i<str.length();i=i+n) // increments of i+n
{
h=h+str.charAt(i); // concatenate all the chars at positions i
}
return h;
}
** All Problems were taken from the website CodingBat