# Solve this Programming Task in language of preference

Imagine a customized computer keyboard with following layout :
ERTYU1
DFGHJ2
CVBNM4
The keyboard only has above set of keys in the panel , For a given set on input word you need to output the path taken to press them , for example :
Consider the input word is : RFGM
the output will be :
PRESS R
GO DOWN
PRESS F
GO RIGHT
PRESS G
GO DOWN
GO RIGHT
GO RIGHT
PRESS M

#### vinod378 Profile Answers by vinod378

• May 31st, 2015

```Code#include<stdio.h>
main()
{
char disc[3][6]={"ERTYU1","DFGHJ2","CVBNM4"};
char input[10];
scanf("%s",input);
int i,j,k,l,pos[4],p;
printf("PRESS %c
",input[0]);
for(p=0;input[p]!=;p++);
for(i=0;i<(p-1);i++)
{
for (l=0;l<2;l++)
{
for(j=0;j<3;j++)
{
for(k=0;k<6;k++)
{
if(input[i+l]==disc[j][k])
{
if (l==0)
{
pos[l]=j;
pos[l+1]=k;
}
else
{
pos[l+1]=j;
pos[l+2]=k;
}
break;
}
}
}
}
/*for (k=0;k<4;k++)
printf("%d ",pos[k]);
printf(" ");*/
if(pos[0]>pos[2])
{
for(j=0;j<(pos[0]-pos[2]);j++)
printf("GO UP
");
}
else if(pos[0]<pos[2])
{
for(j=0;j<(pos[2]-pos[0]);j++)
printf("GO DOWN
");
}
if(pos[1]>pos[3])
{
for(j=0;j<(pos[1]-pos[3]);j++)
printf("GO LEFT
");
}
if(pos[1]<pos[3])
{
for(j=0;j<(pos[3]-pos[1]);j++)
printf("GO RIGHT
");
}
printf("PRESS %c
",input[i+1]);

}

}
```

#### S VISHNUVARDHAN

• Aug 14th, 2015

```Code#include<iostream>
using namespace std;
#include<cstring>
#include<string.h>
char a[4][7] = {"ERTYU1","DFGHJ2","CVBNM4"};
class trace
{
//protected:
char input[100];

public:
trace()
{
//a[3][6] = {"ERTYU1","DFGHJ2","CVBNM4"};
}
void getdata();
void putdata();
void traceit();
};
/* trace::trace()
{
a[3][6] = {"ERTYU1","DFGHJ2","CVBNM4"};
}*/
void trace::getdata()
{
/* cout<<"enter strings:";
for(int i=0;i<3;i++)
cin>>a[i];*/
cout<<"enter input string:";
//for(int i=0;i<n;i++)
cin>>input;
//cout<<"do you want to continue?
";
}
inline void trace::putdata()
{
cout<<"Trace pattern for your entry  "<<input<<"   is as follows:
";
}
void trace::traceit()
{
int i,j,l,prevc=0,prevr=0,presntr=0,presntc=0,rmov=0,cmov=0;
//      char mov[5];
for(i=0;i<3;i++)
for(j=0;j<6;j++)
if(input[0]==a[i][j])
{
prevr=i;prevc=j;
cout<<"press "<<input[0]<<"
";
}
int k=1;
for(int z=0;;z++)
for(i=0;i<=3;i++)
{

for(j=0;j<=6;j++)
{
if(input[k]==a[i][j])
{
presntr=i;presntc=j;
//for row movment
//two cases if presntr > prevr and viceversa
//same thing in case of columns
if(presntr>prevr)
{
//clear(mov);
rmov= -prevr+presntr;
//mov={"down"};
for(int r=0;r<rmov;r++)
{
cout<<"move Down
";//<<mov;
}

}
if(presntr<prevr)
{
//clear(mov);
rmov=prevr-presntr;
//mov={"up"};
for(int r=0;r<rmov;r++)
{
cout<<"move up
";//<<mov;
}
}
if(presntc>prevc)
{
//clear(mov);
cmov=presntc-prevc;
//mov={"Right"};
for(int c=0;c<cmov;c++)
{
cout<<"move Right
";//<<mov;

}

}
if(presntc<prevc)
{
//clear(mov);
cmov= -presntc+prevc;
//mov={"Left"};
for(int c=0;c<cmov;c++)
{
cout<<"move Left
";//<<mov;

}
}
cout<<"press "<<input[k]<<"
";
i=0;j=0;k++;
prevr=presntr;
prevc=presntc;
//presntr=i;
//presntc=j;

}
if(k==strlen(input))
{
//cout<<"iM STOPPING THIS PROGRAM. IM SORRY:";

return;
}
}
}

}
main()
{
trace d;
char ch;

d.getdata();
d.putdata();
d.traceit();

cin>>ch;

{
d.getdata();
d.putdata();
d.traceit();
}*/

}
```

#### prasobh

• Jan 20th, 2016

#Python solution
# E R T Y U 1
# D F G H J 2
# C V B N M 4
#NJUE2C1M
keys=[E,R,T,Y,U,1,D,F,G,H,J,2,C,V,B,N,M,4]
key_board_width = 6
string = input("Provide pattern")
for i in range(len(string)-1):
#print("i is"+str(i))
if (string[i] in keys):
print("Press"+ string[i])

current_alpha_index = keys.index(string[i])
next_alpha_index = keys.index(string[i+1])
#print(cuurent alpha index =+str(current_alpha_index)+next alpha index +str(next_alpha_index))
if (((current_alpha_index+1)%key_board_width) == 0):#Since intiger division can create a mess we have to do this
current_row = (current_alpha_index+1)/key_board_width
else:
current_row = int((current_alpha_index+1)/key_board_width)+1

if (((next_alpha_index+1)%key_board_width) == 0):
next_row = (next_alpha_index+1)/key_board_width
else:
next_row = int((next_alpha_index+1)/key_board_width)+1
vertical_step = next_row-current_row
#print("vertical step is "+str(int(vertical_step)))

if(vertical_step > 0):
for j in range(abs(int(vertical_step))) :
print("Down")

if(vertical_step < 0):
for j in range(abs(int(vertical_step))) :
print("Up")
current_alpha_index = current_alpha_index +(key_board_width* int(vertical_step))
#print(updated current alpha index +str(current_alpha_index))
horizontal_step = next_alpha_index -current_alpha_index
#print("horizontal step is "+str(int(horizontal_step)))
if(horizontal_step >0):
for j in range(abs(int(horizontal_step))) :
print("right")

if(horizontal_step <0):
for j in range(abs(int(horizontal_step))) :
print("left")
print("Press"+ string[len(string)-1])