Thursday, 28 May 2015

Hamming distance between two integers using Java...

Here is the code.......


       

public class Hamming {

 public static int distance(int a, int b) {
  int k = 0;
  k = a ^ b;
  int sum = 0;
  int t = requiredbits(k); //it returns the required no. of bits to represent k in binary 
  int[] arr = new int[k];

  for (int f = t - 1; f >= 0; f--) //in this loop we are converting the no. k into binary 
  {
   arr[f] = k % 2;
   k = k / 2;

  }

  for (int r = 0; r < t; r++) // here counting number of 1's in the binary representation of k
  {

   if (arr[r] == 1)
    sum++;

  }
  return sum;
 }

 public static int requiredbits(int w) {
  int i = 0;
  while ((2 << i) <= w)
   i++;
  i++;
  return i;
 }

 public static void main(String args[]) throws Exception {

  int a = 3, b = 2;
  System.out.println("hamming distance between " + a + " and " + b + " is " + distance(a, b));
 }

}
       
 

Sunday, 24 May 2015

Number of bits required to represent a number in binary using Java....

Here is the code ....


       

import java.io.*;
import java.util.*;
public class Bitsrequired {

 public static int getRequiredNumberOfBits(int N) {
  int i = 0;
  while ((2 << i) <= N) {
   i++;
  }
  return ++i;
 }



 public static void main(String args[]) throws Exception {

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter the number");
  int me = Integer.parseInt(br.readLine());

  getRequiredNumberOfBits(me);
  System.out.println(getRequiredNumberOfBits(me));


 }

}
       
 

Java program to reverse a string using recursion ....

Here is the code....


       
import java.io.*;
import java.util.*;
class reverse_string {

 public static String recursion_string(String name, int t) throws Exception {

  if (name.length() == 0) {
   return "";
  }
  if (name.length() == 1) {
   return name;
  }

  String start = name.substring(t);
  //System.out.println(start);

  String remainder = name.substring(0, t);
  t--;
  //System.out.println(remainder);
  //System.out.println(remainder);
  return (start + recursion_string(remainder, t));


 }

 public static void main(String args[]) throws Exception {
  String s = "cobra123";
  int l = s.length() - 1;
  //System.out.println(l);
  System.out.println(recursion_string(s, l));
 }

}

       
 

Convert decimal to binary using Java..........

here is the code ......


       
import java.io.*;
import java.util.*;
import java.math.*;
class decimal_to_binary {


 public static void main(String args[]) {
  int k = 10, i = 1;

  while ((2 << i) <= k) {
   i++;
  }
  i++;

  int[] a = new int[i];

  for (int n = i - 1; n >= 0; n--) {
   a[n] = k % 2;
   k = k / 2;
  }

  for (int g: a)
   System.out.print(g);

 }

}

       
 

Java program to convert binary to decimal ....

here is the code ......


       
import java.io.*;
import java.util.*;
class binary_to_decimal {

 public static void main(String args[]) {
  int[] a = {
   0,
   0,
   0,
   1,
   1,
   1
  };
  int sum = 0;

  for (int k = a.length - 1, h = 0; k >= 0; k--, h++) {
   sum = sum + a[k] * (1 << h);
  }
  System.out.println(sum);
 }

}

       
 

Saturday, 23 May 2015

Java program to print all the permutations of a string ...

here  is the code 


       
 import java.io.*;
 import java.util.*;
 class permute_string {
  public static ArrayList < String > getPerms(String s) {
   ArrayList < String > permutations = new ArrayList < String > ();
   if (s == null) { // error case
    return null;
   } else if (s.length() == 0) {
    permutations.add("");
    return permutations;
   }

   char first = s.charAt(0); // get the first character
   String remainder = s.substring(1); // remove the first character
   ArrayList < String > words = getPerms(remainder);
   for (String word: words) {
    for (int j = 0; j <= word.length(); j++) {
     permutations.add(insertCharAt(word, first, j));
    }
   }
   return permutations;
  }

  public static String insertCharAt(String word, char c, int i) {
   String start = word.substring(0, i);
   String end = word.substring(i);
   return start + c + end;
  }


  public static void main(String args[]) {


   System.out.println(getPerms(args[0]));
  }
 }

       
 

Wednesday, 20 May 2015

Sort an array of strings in a way so that all the anagrams will be adjacents in the sorted array using Java....

here is the code ..


       
import java.io.*;
import java.util.*;

class sortanagram {

 public static boolean anagram(String arg1, String arg2) {
  int f;
  int[] arr = new int[256];
  if (arg1.length() == arg2.length()) {
   for (int i = 0; i < arg1.length(); i++) {
    f = arg1.charAt(i);
    arr[f] = arr[f] + 1;
   }

   for (int k = 0; k < arg2.length(); k++) {
    f = arg2.charAt(k);
    arr[f] = arr[f] - 1;
   }
   for (int d = 0; d < 256; d++) {
    if (arr[d] != 0) {
     return false;
    }

   }
   return true;
  } else {
   return false;
  }

 }

 public static void main(String args[]) {

  String[] args3 = {
   "abcd",
   "save",
   "dcba",
   "dog",
   "easv",
   "god",
   "free"
  };
  String temp;
  int p;

  for (int i = 0; i < args3.length; i++) {
   p = i;
   for (int l = i + 1; l < args3.length; l++) {
    if (anagram(args3[i], args3[l]) == true) {


     p++;
     temp = args3[l];
     args3[l] = args3[p];
     args3[p] = temp;

    }

   }

  }

  for (int r = 0; r < args3.length; r++)
   System.out.println(args3[r]);

 }

}