Search

Monday 12 June 2017

startOz

Problem:
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".


Test Cases: 

startOz("ozymandias") → "oz"
startOz("bzoo") → "z"
startOz("oxx") → "o"

Solution:

public String startOz(String str) {
 String result = "";
 
  if (str.length() >= 1 && str.charAt(0)=='o') {
    result = result + str.charAt(0);
  }
 
  if (str.length() >= 2 && str.charAt(1)=='z') {
    result = result + str.charAt(1);
  }
 
  return result;
}

Sunday 11 June 2017

mixStart

Problem:
Return true if the given string begins with "mix", except the 'm' can be anything, so "pix", "9ix" .. all count.

Test Cases:
mixStart("mix snacks") → true
mixStart("pix snacks") → true
mixStart("piz snacks") → false

Solution:

public boolean mixStart(String str) {
  if(str.length()<3)
  {
 
      return false;
 
   
  }
  if(str.length()>=3)
  {
    if(str.substring(1,3).equals("ix"))
    {
      return true;
    }
  }
  return false;
}

Friday 9 June 2017

delDel

Problem:
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.

Test Cases:

delDel("adelbc") → "abc"
delDel("adelHello") → "aHello"
delDel("adedbc") → "adedbc"

Solution: 

public String delDel(String str) {
  if(str.length()>=4)
  {
  if(str.substring(1,4).equals("del"))
  {
    return str.substring(0,1)+str.substring(4);
  }
  }
  return str;
}

Wednesday 7 June 2017

hasTeen

Problem:
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.


Test Cases:
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>12 && a<20) || (b>12 && b<20) || (c>12 && c<20))?true:false);
}
icyHot

Problem:

Given two temperatures, return true if one is less than 0 and the other is greater than 100.
 
Test Cases:

icyHot(120, -1) → true
icyHot(-1, 120) → true
icyHot(2, 120) → false

Solution:

public boolean icyHot(int temp1, int temp2) {
  /*if((temp1<0 && temp2>100) ||(temp2<0 && temp1>100))
  {
    return true;
  }
  return false;  (compact solution below)*/
  return (((temp1<0 && temp2>100) ||(temp2<0 && temp1>100))?true:false);
}
startHi

Problem:

Given a string, return true if the string starts with "hi" and false otherwise.

Test Cases:

startHi("hi there") → true
startHi("hi") → true
startHi("hello hi") → false

Solution:

public boolean startHi(String str) {
  if(str.startsWith("hi"))
  {
    return true;
  }
  return false;
}
front22

Problem:

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.

Test Case:

front22("kitten") → "kikittenki"
front22("Ha") → "HaHaHa"
front22("abc") → "ababcab"

Solution:

public String front22(String str) {
  if(str.length()>1)
  {
    return str.substring(0,2)+str+str.substring(0,2);
  }
 
  return str+str+str;
}
or35

Problem:
Return true if the given non-negative number is a multiple of 3 or a multiple of 5.

Test Cases:
or35(3) → true
or35(10) → true
or35(8) → false

Solution:

public boolean or35(int n) {
  if((n%3==0) || (n%5==0))
  {
  return true;
  }
  return false;
}
backAround

Problem:

Given a string, take the last char and return a new string with the last char added at the front and back, so "cat" yields "tcatt". The original string will be length 1 or more.

Test Cases:

backAround("cat") → "tcatt"
backAround("Hello") → "oHelloo"
backAround("a") → "aaa"

Solution:

public String backAround(String str) {
  return str.charAt(str.length()-1)+str+str.charAt(str.length()-1);
}
front3

Problem:

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.

Test Cases:
front3("Java") → "JavJavJav"
front3("Chocolate") → "ChoChoCho"
front3("abc") → "abcabcabc"

Solution:

public String front3(String str) {
  String re="";
  if(str.length()>=3)
  {
    String k=str.substring(0,3);
    re=k+k+k;
  }
  else if(str.length()<3)
  {
    re=str+str+str;
  }
  return re;
}



frontBack

Problem:
Given a string, return a new string where the first and last chars have been exchanged.
 
Test Cases:
frontBack("code") → "eodc"
frontBack("a") → "a"
frontBack("ab") → "ba"

Solution:

public String frontBack(String str) {
  if(str.length()>=2)
  {
    return str.charAt(str.length()-1)+str.substring(1,str.length()-1)+str.charAt(0);
  }
  return str;
}

 

Tuesday 6 June 2017

missingChar

Problem:

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).

Test Cases:
missingChar("kitten", 1) → "ktten"
missingChar("kitten", 0) → "itten"
missingChar("kitten", 4) → "kittn"

Solution:

public String missingChar(String str, int n) {
  String result="";

    StringBuilder b=new StringBuilder(str);
    b.deleteCharAt(n);
    result=b.toString();
 
  return result;
}
notString

Problem:
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.

Test Cases:
notString("candy") → "not candy"
notString("x") → "not x"
notString("not bad") → "not bad"

Solution:
 public String notString(String str) {
  if(str.startsWith("not"))
  {
  return str;
  }
  return "not"+" "+str;
}


posNeg

Problem:

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.

Test Cases:

posNeg(1, -1, false) → true
posNeg(-1, 1, false) → true
posNeg(-4, -5, true) → true

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));
  }
}
nearHundred

Problem:

Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number.
 
Test Cases:



nearHundred(93) → true
nearHundred(90) → true
nearHundred(89) → false

Solution:

public boolean nearHundred(int n) {
  if(Math.abs(n-100)<11 || Math.abs(n-200)<11)
  {
    return true;
  }
  return false;
}

 
makes10

Problem:
Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.

Test Cases:
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;
}



parrotTrouble

Problem:

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.

Test Cases:

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;
}
diff21

Problem: 
Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21.

Test Cases: 
diff21(19) → 2
diff21(10) → 11
diff21(21) → 0


Solution: 

public int diff21(int n) {
  if(n>21)
  {
    return 2*(Math.abs(n-21));
  }
  return Math.abs(n-21);
}
 

(NOTE: Math.abc(int n) returns a non-negative integer value.) 
sumDouble

Problem:

Given two int values, return their sum. Unless the two values are the same, then return double their sum.
 
Test Cases:
 
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:

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.

Test Cases:

monkeyTrouble(true, true) → true
monkeyTrouble(false, false) → true
monkeyTrouble(true, false) → false

Solution:

public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
  if((aSmile && bSmile) || (!aSmile && !bSmile))
  {
    return true;
  }
  return false;
}
Problem:
 
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.
 
Test cases:

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;
  
  we just put the return-false last, or could use an if/else.
}
Let's try to solve codingbat problem together !!!