Sunday, October 7, 2012

Write a function that takes three integers corresponding to the lengths of sides and returns what kind of triangle can be made out of those 3 sides.


/**
* Validates given values for 3 sides of triangle.
* @param side1 Side1.
* @param side2 Side2.
* @param side3 Side3.
* @return 1=scalene, 2=isosceles, 3=equilateral, 4=error.
*/
public int validateTriangle(final int side1, final int side2, final int side3)
{
// Check for validity of all sides
if ((side1 + side2 > side3) && (side2 + side3 > side1) && (side3 + side1 > side2))
{
//  Equilateral
if (side1 == side2 && side2 == side3 && side1 == side3)
{
return 3;
}
// Isosceles
if (side1 == side2 || side2 == side3 || side1 == side3)
{
return 2;
}
// Scalene

return 1;
}
else
{
return 4;
}
}

Find the first index of the substring.


public int findIndex(String mainString, String subString)
{
int result = -1;
 if (mainString == null || subString == null)
                         return result;
int mainLength= mainString.length();
int subLength = subString.length();

                 if(mainLength < subLength)
return result;
for(int i =0 ;i < mainLength; i++)
{
char cur = mainString.charAt(i);
if (cur == subString.charAt(0))
{
if (i + subLength <= mainLength)
{
if ((mainString.substring(i, subLength + i).equals(subString)))
return i;
}
else
return result;

}
}
return result;
}

write a function fib(n,k) which gives you first n numbers of a Fibonacci series and k is the number of previous numbers you have to add.


public int fibonacci(int n, int k)
{
if (n <= 0)
return 0;
if (n==1)
return 1;
int result =0 ;
for (int i = 1; i <= k; i++)
{
result += fibonacci(n-i, k);
}
return result;
}

Given a string generate permutations of all possible lengths and print them in any order.


public void permutations(String data)
{
for( int i =0 ;i < data.length();i++)
{
for(int j=i+1; j<= data.length() ; j++)
{
String subsring = data.substring(i, j);
System.out.println(subsring);
if (subsring.length() > 1)
{
System.out.println(reverse(subsring.toCharArray()));
}
}
}
}

public String reverse(char[] array)
{
int length = array.length;
for (int i =0 ;i < length/2; i++)
{
char temp = array[i];
array[i] = array[length - 1 - i];
array[length - 1 - i] = temp;
}
return new String(array);
}

Friday, August 24, 2012

Find the longest common sequence

Longest Common Sequence

Java Code:

/**
* Find the longest common sequence from two strings.
* @param seq1 String sequence 1.
* @param seq2 String sequence 1.
* @return  String of longest common sequence.
*/
public String getLCS(final String seq1, final String seq2)
{
// Check for empty.
if (!seq1.isEmpty() && !seq2.isEmpty())
{
// Separate 1st char & remaining part of 1st string.
String seq1B = seq1.substring(0,1);
String seq1E = seq1.substring(1);
// Separate 1st char & remaining part of 2nd string.
String seq2B = seq2.substring(0,1);
String seq2E = seq2.substring(1);

// Check for equality of start characters of both strings.
if (seq1B.equalsIgnoreCase(seq2B))
{
// If equal append character and check for rest part
return seq1B + getLCS(seq1E, seq2E);
}
else
{
// Check for both strings and return the greater results.
String ret1 = getLCS(seq1, seq2E);
String ret2 = getLCS(seq1E, seq2);
if (ret1.length() > ret2.length())
{
return ret1;
}
else
{
return ret2;
}
}
}
else
{
return "";
}
}

Example:
getLCS("HUMAN", "CHIMPANZEE") ==> HMAN

How to check if the given string is palindrome?

Algorithm:
Approach1 : Start comparing characters from start and end of the string and continue until len(string)/2 .
Approach2: Start from front and back and compare the characters if not equal exit and return false else return true

Java Code:

public boolean isPalindrom(String str)
{
int length = str.length();
for(int i=0; i< length/2 ;i++)
{
if(str.charAt(i) != str.charAt(length - 1 - i))
{
System.out.println(str + " is not a palindrom." );
return false;
}
}
System.out.println(str + " is a palindrom." );
return true;
}

Example:
isPalindrom("abqba") ==> true
isPalindrom("abba") ==> true
isPalindrom("abstba")  ==> false
isPalindrom("absa")  ==> false

Ruby Code:

def palindrom(str)
  start = 0
  last = str.size - 1
  array_str = str.chars
  is_palindrom = true
  while (start < last)
    if array_str[start] != array_str[last]
      is_palindrom = false
      break
    end
    start+=1
    last-=1
  end
  is_palindrom
end

p palindrom("radar")
p palindrom("r")
p palindrom("123456")
p palindrom("rr")
p palindrom("radbar")

p palindrom("1radar1")