Search This Blog

Sunday, January 22, 2012

Mathematical Combination Problem - JAVA



/** This is a program showing an example of 'combination'.
  * In this example, every number (0-9) have been assigned some characters.
  * For example, 0 : p,q,r
  * 1 : a,b,c
  * 2 : x,y,z
  * 3 : d,e,f
  * 4 : b,h,s,d
  * 5 : z,o,p,a,q
  * 6 : k,o,l,p,d,a,q,w
  * 7 : u,i,y,c,x,s
  * 8 : d,o,n
  * 9 : NULL
  *
  * Now, if a user enters a number, say 120, this will print the strings -
  * axp,axq,axr,ayp,ayq,ayr,azp,azq,azr,bxp,bxq,bxr,byp,byq,byr,bzp,bzq,bzr,cxp,cxq,cxr,cyp,cyq,cyr,czp,czq,czr
  * onto the screen.
  *
  * NOTE : The total number of strings ought to be printed is equal to product of number of characters written above
  * in front of the input numbers. (If number of characters is 0 i.e. NULL is written in front of a number, take
  * number of characters as 1).
  * In our example, it is equal to 3*3*3 = 27.
  *
  */


import java.util.HashMap;
import java.util.Scanner;

public class MathematicalCombinationProblem {

static Key[] Key;
static int i=1;
static String str = "";
static int inputLength;

public static void main(String[] args){

// I am using hashmap to store the 'number-value' data.

HashMap<Integer, String> key = new HashMap<Integer, String>();
key.put(0, "pqr");
key.put(1, "abc");
key.put(2, "xyz");
key.put(3, "def");
key.put(4, "bhsd");
key.put(5, "zopaq");
key.put(6, "kolpdaqw");
key.put(7, "uiycxs");
key.put(8, "don");
key.put(9, "");

// Scanner is used to get the user input

Scanner s = new Scanner(System.in);
System.out.println("Enter a number :");
String input = s.nextLine();
inputLength = input.length();
Key = new Key[inputLength];
int p=0;
int ind;
while(p<inputLength){
ind = Integer.parseInt(input.substring(p, p+1));
Key[p] = new Key(ind,key.get(ind));
p++;
}
p=0;
while(p<inputLength-1){
if(p<inputLength-1)
Key[p].nextKey = Key[p+1];
p++;
}
Key[p].nextKey = null;
getCode(Key[0]);
}

/*
* We will be using the 'RECURSION' concept in solving this problem.
* Starting from the first key i.e. keys[0], we will be traversing the last key i.e. keys[inputLength-1].
* After reaching at the last key, we will be using the 'getStringAt' method to get the character as a
* string at index=keys[n].counter where n runs from 0 to inputLength-1.
*
*/
private static void getCode(Key k){
if(k.value.length() == 0){
for(k.counter=0 ; k.counter<1; k.counter++){
if(k.nextKey != null){
getCode(k.nextKey);
}
else{
str = "";
for(int j=0 ; j<inputLength ; j++){
str +=Key[j].getStringAt(Key[j].counter);
}
System.out.println(str);
}
}
}
else{
for(k.counter=0 ; k.counter<k.value.length() ; k.counter++){
if(k.nextKey != null){
getCode(k.nextKey);
}
else{
str = "";
for(int j=0 ; j<inputLength ; j++){
str +=Key[j].getStringAt(Key[j].counter);
}
System.out.println(str);
}
}
}
}
}
/*
 * Class Key has got number,value,counter and nextKey as the attributes.
 * Suppose, user enters 52 as input then, two objects of type Key will be created keys[0] and keys[1].
 * keys[0].number=5, keys[0].value="zopaq", keys[0].counter=0, keys[0].nextKey=keys[1] and
 * keys[1].number=2, keys[1].value="xyz", keys[1].counter=0, keys[1].nextKey=null.
 * It has got one method 'getStringAt' which returns a single character as a string at a given index.
 */

class Key {

int number;
String value;
int counter;
Key nextKey;
public Key(int number , String value){
this.number = number;
this.value = value;
counter = 0;
}


/*
* This method takes an integer value as index and returns the character at that index as a string
*/

public String getStringAt(int index){
if(this.value.length() == 0)
return "";
else if(index != value.length()-1)
return value.substring(index,index+1);
else
return value.substring(index);
}
}

No comments:

Post a Comment