Saturday, February 02, 2008

A few lines of code vs Happiness

Today, I cleared my first MGPT (Machine Graded Programming Test). These were a few lines of code which made me happy. It was my 5th attempt for MGPT. For clearing FPGDST course at CDAC I need to clear atleast 2. Now, I have 2 MGPT left, out of them I need to clear 1 atleast to get certificate. Strange, how a few lines of code made me happy. After all those were a few lines of code that work. Here is my program which gives me all 'Y'.

import ncst.pgdst.*;
public class mgpt{

public static void main(String[] args)throws Exception{
SimpleInput si = new SimpleInput();
String[] str = new String[83682];
int i=0,j=0,k=0,l=0,m=0,n=26
,invalid=0;
String[] alpha = new String[27];
alpha[1] = "a";
alpha[2] = "b";
alpha[3] = "c";
alpha[4] = "d";
alpha[5] = "e";
alpha[6] = "f";
alpha[7] = "g";
alpha[8] = "h";
alpha[9] = "i";
alpha[10]= "j";
alpha[11]= "k";
alpha[12]= "l";
alpha[13]= "m";
alpha[14] = "n";
alpha[15] = "o";
alpha[16] = "p";
alpha[17] = "q";
alpha[18] = "r";
alpha[19] = "s";
alpha[20] = "t";
alpha[21] = "u";
alpha[22] = "v";
alpha[23] = "w";
alpha[24] = "x";
alpha[25] = "y";
alpha[26]="z";

int count =1;
String inp = si.readWord();
//System.out.println("Your input is: - "+inp);
for(i=1;i<=n;i++)
str[count++] = alpha[i];

//System.out.println(str.length);

for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
str[count++] = alpha[i] + alpha[j];

for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
for(k=j+1;k<=n;k++)
str[count++]=alpha[i]+alpha[j]+alpha[k];

for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
for(k=j+1;k<=n;k++)
for(l=k+1;l<=n;l++)
str[count++] = alpha[i]+alpha[j]+alpha[k]+alpha[l];

for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
for(k=j+1;k<=n;k++)
for(l=k+1;l<=n;l++)
for(m=l+1;m<=n;m++)
str[count++] = alpha[i] + alpha[j] + alpha[k] + alpha[l] + alpha[m];


//System.out.println(str.length);
boolean flag = false;
for(i=1;i<83682;i++)
{
if(inp.equals(str[i]))
{
System.out.println(i);
flag = true;
}
}

if(flag == false){
System.out.println(invalid);
}
}
}




The problem was to take a combination of characters no longer than 5 length. Check whether its in lexographic order or not. If it is then print its position, if it is not then print 0.
e.g.
a -- 1
b -- 2
..
..
z -- 26
ab -- 27
..
..
az -- 51
bc -- 52
..
..
vwxyz -- 83681