Search

Monday 13 November 2017

String-2 > doubleChar 

Problem: :

Given a string, return a string where for every char in the original, there are two chars.

Test Cases:

doubleChar("The") → "TThhee"
doubleChar("AAbb") → "AAAAbbbb"
doubleChar("Hi-There") → "HHii--TThheerree"

Solution:

public String doubleChar(String str) {
  String s=str,s1="";
for(int i=0;i<s.length();i++)
{
s1+=s.substring(i,i+1)+s.substring(i,i+1);
}
return s1;
}




Tuesday 25 July 2017

intMax

Problem:

Given three int values, a b c, return the largest.

Test Cases:
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) {
 
  return (a>b)?((a>c)?a:c):((b>c)?b:c);
}

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