Thursday, 25 July 2013

Cryptography Tool (Cipher Wheel)



A cipher is a set of rules for converting between plain text and cipher-text. These rules often use a secret key.
  

A Virtual Cipher Wheel  : http://invpy.com/cipherwheel 

How to Encrypt with the Cipher Wheel
First, write out your message in English on paper. For this example we will encrypt the message, “The secret password is Rosebud.” Next, spin the inner wheel around until its letters match up with letters in the outer wheel. Notice in the outer wheel there is a dot next to the letter A. Look at the number in the inner wheel next to the dot in the outer wheel. This number is known the encryption key.
The encryption key is the secret to encrypting or decrypting the message


So the steps to encrypt a letter are:
1. Decide on a key from 1 to 25. Keep this key secret!
2. Find the plain-text letter’s number.
3. Add the key to the plain-text letter’s number.
4. If this number is larger than 26, subtract 26.
5. Find the letter for the number you’ve calculated. This is the cipher-text letter.
6. Repeat steps 2 to 5 for every letter in the plain-text message.


To decrypt, subtract the key instead of adding it. For the cipher-text letter B, the number is 1. Subtract 1 – 13 to get -12. Like our “subtract 26” rule for encrypting, when we are decrypting and the result is less than 0, we have an “add 26” rule. -12 + 26 is 14. So the cipher-text letter B decrypts back to letter O.

Code:
#include<iostream>
#include<cstring>
//#include<ctype>
char * encript(int k,char *text);
char * decript(int k,char *text);
using namespace std;

int main()
{
char text[50];
int key,x;
cout<<"Enter the text:";
cin>>text;
cout<<"Enter encryption key:";
cin>>key;
cout<<"Press 1 for Encryption\nPress 2 for Decryption\n";
cin>>x;
if(x==1)
cout<<"Encripted text:"<<encript(key,text)<<endl;
else
cout<<"Decripted text:"<<decript(key,text)<<endl;

return(0);
}


char * encript(int key,char *text)
{
int ti;
for(int i=0;i<strlen(text);i++)
{
  if(isalpha(text[i])&&(isupper(text[i])))
   {
        ti=(int)text[i];
        ti+=key;
       if(ti>90)
        ti-=26;
       text[i]=(char)ti;

   }

 if(isalpha(text[i])&&(islower(text[i])))
   {
        ti=(int)text[i];
        ti+=key;
       if(ti>122)
        ti-=26;
       text[i]=(char)ti;

   }



}
return(text);
}

char * decript(int key,char *text)
{

int ti;
for(int i=0;i<strlen(text);i++)
{
  if(isalpha(text[i])&&(isupper(text[i])))
   {
        ti=(int)text[i];
        ti-=key;
       if(ti<65)
        ti+=26;
       text[i]=(char)ti;

   }

 if(isalpha(text[i])&&(islower(text[i])))
   {
        ti=(int)text[i];
        ti-=key;
       if(ti<97)
        ti+=26;
       text[i]=(char)ti;

   }



}
return(text);

}





 

Tuesday, 25 June 2013

A matchstick game



Q.Matchstick game being played between the computer and a user. Your program should ensure that the
computer always wins. Rules for the game are as follows:
− There are 21 matchsticks.
− The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
− After the person picks, the computer does its picking.
− Whoever is forced to pick up the last matchstick loses the game. 


   #include<stdio.h>
   #include<conio.h>
void main()
{
int ms=21,ply,sys=0;
clrscr();
fflush(stdin);
printf("Select between 1-4\n otherwise computer wins\n");
while(ms>=1)
{
   if(ms==1)
    {
       printf("Computer wins\n");
       break;
    }
printf("Avilable Matchsticks = %d\n",ms);
printf("Player= ");
scanf("%d",&ply);
printf("\nComputer= %d\n",sys=5-ply);
ms=ms-sys-ply;
}
getch();
}

Monday, 24 June 2013

How to shuffle the array elements..?

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int size,i;

cout<<"Enter the size of array:";
cin>>size;

int a[20];
cout<<"\nEnter the elements:";

for(i=0;i<size;i++)
cin>>a[i];

srand(time(0));
int k,t;


for(i=0;i<size;i++)
{
k=rand()%size;
t=a[i];        //a[i]=a[i]*a[k];
a[i]=a[k];     //a[k]=a[i]/a[k];
a[k]=t;        //a[i]=a[i]/a[k];
}

cout<<"\nShuffeled array:";
for(i=0;i<size;i++)
cout<<a[i]<<"\t";
cout<<endl;
return 0;
}

Wednesday, 8 May 2013

Sort elements by frequency

Given an array of integers, sort the array according to frequency of elements. For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}.

**************************************
*         C++ implementation:        *
**************************************
#include<iostream>
#include<stdlib.h>
using namespace std;
struct tree
{
int freq,info;
struct tree *lchild,*rchild;
};
typedef struct tree* TREE;
TREE creat(TREE,int);
void sort();
void inorder(TREE);

int e[20],c[20],l=0;

int main()
{
cout<<"Enter the number of elements to be inserted:";
int n;cin>>n;
TREE root=NULL;

cout<<"Enter the elements:";
for(int i=0;i<n;i++)
{
  int ele;cin>>ele;
  root=creat(root,ele);
}

inorder(root);
sort();
cout<<"Sorted elements:";
for(int i=0;i<=l;i++)
{
  for(int j=0;j<c[i];j++)
  cout<<e[i]<<"\t";
}
cout<<endl;
return(0);
}


void inorder(TREE root)
{
  if(root!=NULL)
  {
    inorder(root->lchild);
    e[++l]=root->info;
    c[l]=root->freq;
    inorder(root->rchild);
  }
}

void sort()
{
int temp;
for(int i=0;i<l-1;i++)
for(int j=0;j<l-i-1;j++)
{
  if(c[j]<c[j+1])
   {
      temp=c[j];
      c[j]=c[j+1];
      c[j+1]=temp;
      temp=e[j];
      e[j]=e[j+1];
      e[j+1]=temp;
   }
}

}

TREE creat(TREE node,int key)
{
TREE nnode,x,p;
nnode=(TREE)malloc(sizeof(struct tree));
nnode->lchild=NULL;
nnode->rchild=NULL;
nnode->info=key;
nnode->freq=1;

if(node==NULL)
return(nnode);

x=node;
while(x!=NULL)
{
   p=x;
   if(key>x->info)
   x=x->rchild;
   else if(key<x->info)
   x=x->lchild;
   else
   {
        x->freq++;
        return(node);
   }
}
if(key>p->info)
p->rchild=nnode;
else
p->lchild=nnode;
return(node);
}    

Tuesday, 30 April 2013

Tic-Tac-Toe-Tomek(Qualification Round code jam-2013 )

Problem

Tic-Tac-Toe-Tomek is a game played on a 4 x 4 square board. The board starts empty, except that a single 'T' symbol may appear in one of the 16 squares. There are two players: X and O. They take turns to make moves, with X starting. In each move a player puts her symbol in one of the empty squares. Player X's symbol is 'X', and player O's symbol is 'O'.
After a player's move, if there is a row, column or a diagonal containing 4 of that player's symbols, or containing 3 of her symbols and the 'T' symbol, she wins and the game ends. Otherwise the game continues with the other player's move. If all of the fields are filled with symbols and nobody won, the game ends in a draw. See the sample input for examples of various winning positions.
Given a 4 x 4 board description containing 'X', 'O', 'T' and '.' characters (where '.' represents an empty square), describing the current state of a game, determine the status of the Tic-Tac-Toe-Tomek game going on. The statuses to choose from are:
  • "X won" (the game is over, and X won)
  • "O won" (the game is over, and O won)
  • "Draw" (the game is over, and it ended in a draw)
  • "Game has not completed" (the game is not over yet)
If there are empty cells, and the game is not over, you should output "Game has not completed", even if the outcome of the game is inevitable.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of 4 lines with 4 characters each, with each character being 'X', 'O', '.' or 'T' (quotes for clarity only). Each test case is followed by an empty line.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is one of the statuses given above. Make sure to get the statuses exactly right. When you run your code on the sample input, it should create the sample output exactly, including the "Case #1: ", the capital letter "O" rather than the number "0", and so on.

Limits

The game board provided will represent a valid state that was reached through play of the game Tic-Tac-Toe-Tomek as described above.

Small dataset

1 ≤ T ≤ 10.

Large dataset

1 ≤ T ≤ 1000.


Sample

Input
 

Output
 
6
XXXT
....
OO..
....

XOXT
XXOO
OXOX
XXOO

XOX.
OX..
....
....

OOXX
OXXX
OX.T
O..O

XXXO
..O.
.O..
T...

OXXX
XO..
..O.
...O

Case #1: X won
Case #2: Draw
Case #3: Game has not completed
Case #4: O won
Case #5: O won
Case #6: O won



code:

#include <stdio.h>
#include <stdlib.h>
char A[16];
int sta(int a,int b,int c,int d);
 int main()
 {
 char msg[8]="Case #",ch=':',chr,win;
 FILE *fp,*fp1;
 int tc,i,j,k;
 char file[10];
 printf("Enter the input file name : ");
 scanf("%s",file);
 fp=fopen(file,"r");
 fp1=fopen("output2.txt","w");
 fscanf(fp,"%d",&tc);
 for(i=0;i<tc;i++)
 {
   k=0;
   while(k<16)
   {
     fscanf(fp,"%c",&chr);
     if(chr=='.'||chr=='T'||chr=='X'||chr=='O')
     A[k++]=chr;
   }
/*1*/  if(sta(0,1,2,3))
       {
    win=A[0];
    if(win=='T')
    win=A[1];
    fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
    }

/*2*/  else if(sta(4,5,6,7))
       {
       win=A[4];
       if(win=='T')
       win=A[5];
       fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
       }

/*3*/  else if(sta(8,9,10,11))
       {
       win=A[8];
       if(win=='T')
       win=A[9];
       fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
       }

/*4*/  else if(sta(12,13,14,15))
       {
    win=A[12];
       if(win=='T')
       win=A[13];
       fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
       }

/*5*/  else if(sta(0,4,8,12))
      {
       win=A[0];
       if(win=='T')
       win=A[4];
       fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
      }

/*6*/  else if(sta(1,5,9,13))
       {
       win=A[1];
       if(win=='T')
       win=A[5];
       fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
       }

/*7*/  else if(sta(2,6,10,14))
       {
    win=A[2];
    if(win=='T')
    win=A[6];
    fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
       }
/*8*/  else if(sta(3,7,11,15))
       {
    win=A[3];
    if(win=='T')
    win=A[7];
    fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
    }

/*9*/  else if(sta(0,5,10,15))
       {
    win=A[0];
    if(win=='T')
    win=A[5];
    fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
    }

/*10*/ else if(sta(12,9,6,3))
     {
     win=A[12];
     if(win=='T')
     win=A[3];
     fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
     }
     else if((A[0]=='.')||(A[1]=='.')||(A[2]=='.')||(A[3]=='.')||(A[4]=='.')||(A[5]=='.')||(A[6]=='.')||(A[7]=='.')||(A[8]=='.')||(A[9]=='.')||(A[10]=='.')||(A[11]=='.')||(A[12]=='.')||(A[13]=='.')||(A[14]=='.')||(A[15]=='.'))
     {
     fprintf(fp1,"%s%d%c %s\n",msg,i+1,ch,"Game has not completed");
     }
     else
     {
      fprintf(fp1,"%s%d%c %s\n",msg,i+1,ch,"Draw");
     }
 }
 fclose(fp);
 fclose(fp1);
 return(0);
 }


 int sta(int a,int b,int c,int d)
 {
 if(A[a]=='.'||A[b]=='.'||A[c]=='.'||A[d]=='.')
 return(0);

 if(A[a]==A[b]&&A[b]==A[c]&&A[c]==A[d])
 return(1);

 if(A[a]==A[b]&&A[b]==A[c]&&A[d]=='T')
 return(1);

 if(A[a]==A[b]&&A[b]==A[d]&&A[c]=='T')
 return(1);

 if(A[d]==A[b]&&A[b]==A[c]&&A[a]=='T')
 return(1);

 if(A[a]==A[d]&&A[d]==A[c]&&A[b]=='T')
 return(1);

 return(0);
 }