Sunday 25 September 2016

repeatFront

Home
Goto Problem
Given a string and an int n, return a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the length of the string, inclusive (i.e. n >= 0 and n <= str.length()).
repeatFront("Chocolate", 4) → "ChocChoChC"
repeatFront("Chocolate", 3) → "ChoChC"
repeatFront("Ice Cream", 2) → "IcI"

public String repeatFront(String str, int n) 
{
     String res=str.substring(0,n);
     for(int i=1;i<n;i++)
     {
            res=res+str.substring(0,n-i);
     }
     return res;
}

Saturday 10 September 2016

loneSum

Goto Problem

Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.loneSum(1, 2, 3) → 6
loneSum(3, 2, 3) → 2
loneSum(3, 3, 3) → 0

public int loneSum(int a, int b, int c) {
if(a==b) { if(a==c) return 0; else return c; }
if(a==c) { return b; } if(b==c) return a; else return a+b+c; }

Monday 5 September 2016

repeatSeparator

Home
Goto Problem

Given two strings, word and a separator, return a big string made of count occurences of the word, separated by the separator string.
repeatSeparator("Word", "X", 3) → "WordXWordXWord"
repeatSeparator("This", "And", 2) → "ThisAndThis"
repeatSeparator("This", "And", 1) → "This"

 public String repeatSeparator(String word, String sep, int count) 
 {
    String res=word;
    if(count==0) 
       return "";
   for(int i=1;i<count;i++)
   {
      res=res+sep;
      res=res+word;
   }
   return res;
}