Solve this Programming Task in language of preference

TASK 1: 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

Showing Answers 1 - 9 of 9 Answers

vinod378

  • May 31st, 2015
 

Code
  1. #include<stdio.h>

  2. main()

  3. {

  4.         char disc[3][6]={"ERTYU1","DFGHJ2","CVBNM4"};

  5.         char input[10];

  6.         scanf("%s",input);

  7.         int i,j,k,l,pos[4],p;

  8.         printf("PRESS %c

  9. ",input[0]);

  10.         for(p=0;input[p]!=;p++);

  11.         for(i=0;i<(p-1);i++)

  12.         {

  13.                 for (l=0;l<2;l++)

  14.                 {

  15.                         for(j=0;j<3;j++)

  16.                         {

  17.                                 for(k=0;k<6;k++)

  18.                                 {

  19.                                         if(input[i+l]==disc[j][k])

  20.                                         {

  21.                                                 if (l==0)

  22.                                                 {

  23.                                                 pos[l]=j;

  24.                                                 pos[l+1]=k;

  25.                                                 }

  26.                                                 else

  27.                                                 {

  28.                                                         pos[l+1]=j;

  29.                                                         pos[l+2]=k;

  30.                                                 }

  31.                                                 break;

  32.                                         }

  33.                                 }

  34.                         }

  35.                 }

  36.                 /*for (k=0;k<4;k++)

  37.                         printf("%d ",pos[k]);

  38.                         printf(" ");*/

  39.                         if(pos[0]>pos[2])

  40.                         {

  41.                                 for(j=0;j<(pos[0]-pos[2]);j++)

  42.                                         printf("GO UP

  43. ");

  44.                         }

  45.                         else if(pos[0]<pos[2])

  46.                         {

  47.                                 for(j=0;j<(pos[2]-pos[0]);j++)

  48.                                         printf("GO DOWN

  49. ");

  50.                         }

  51.                         if(pos[1]>pos[3])

  52.                         {

  53.                                 for(j=0;j<(pos[1]-pos[3]);j++)

  54.                                         printf("GO LEFT

  55. ");

  56.                         }

  57.                         if(pos[1]<pos[3])

  58.                         {

  59.                                 for(j=0;j<(pos[3]-pos[1]);j++)

  60.                                         printf("GO RIGHT

  61. ");

  62.                         }

  63.                         printf("PRESS %c

  64. ",input[i+1]);

  65.                

  66.        

  67.         }

  68.        

  69. }

  70.  

  Was this answer useful?  Yes

S VISHNUVARDHAN

  • Aug 14th, 2015
 

Code
  1. #include<iostream>

  2. using namespace std;

  3. #include<cstring>

  4. #include<string.h>

  5. char answer[5];

  6. char a[4][7] = {"ERTYU1","DFGHJ2","CVBNM4"};

  7. class trace

  8. {

  9.     //protected:

  10.           char input[100];

  11.  

  12.         public:

  13.          trace()

  14.          {

  15.              //a[3][6] = {"ERTYU1","DFGHJ2","CVBNM4"};

  16.         }

  17.                 void getdata();

  18.                 void putdata();

  19.                 void traceit();

  20. };

  21. /* trace::trace()

  22. {

  23.         a[3][6] = {"ERTYU1","DFGHJ2","CVBNM4"};

  24. }*/

  25. void trace::getdata()

  26. {

  27.    /* cout<<"enter strings:";

  28.     for(int i=0;i<3;i++)

  29.     cin>>a[i];*/

  30.         cout<<"enter input string:";

  31.         //for(int i=0;i<n;i++)

  32.         cin>>input;

  33.          //cout<<"do you want to continue?

  34. ";

  35.       // cin>>answer;

  36. }

  37. inline void trace::putdata()

  38. {

  39.         cout<<"Trace pattern for your entry  "<<input<<"   is as follows:

  40. ";

  41. }

  42. void trace::traceit()

  43. {

  44.         int i,j,l,prevc=0,prevr=0,presntr=0,presntc=0,rmov=0,cmov=0;

  45. //      char mov[5];

  46.         for(i=0;i<3;i++)

  47.         for(j=0;j<6;j++)

  48.         if(input[0]==a[i][j])

  49.         {

  50.                 prevr=i;prevc=j;

  51.                 cout<<"press "<<input[0]<<"

  52. ";

  53.         }

  54.         int k=1;

  55.         for(int z=0;;z++)

  56.         for(i=0;i<=3;i++)

  57.         {

  58.  

  59.                 for(j=0;j<=6;j++)

  60.                 {

  61.                         if(input[k]==a[i][j])

  62.                         {

  63.                                 presntr=i;presntc=j;

  64.                                 //for row movment

  65.                                 //two cases if presntr > prevr and viceversa

  66.                                 //same thing in case of columns

  67.                                 if(presntr>prevr)

  68.                                 {

  69.                                         //clear(mov);

  70.                                         rmov= -prevr+presntr;

  71.                                         //mov={"down"};

  72.                                         for(int r=0;r<rmov;r++)

  73.                                         {

  74.                                                 cout<<"move Down

  75. ";//<<mov;

  76.                                         }

  77.  

  78.                                 }

  79.                if(presntr<prevr)

  80.                                 {

  81.                                         //clear(mov);

  82.                                         rmov=prevr-presntr;

  83.                                         //mov={"up"};

  84.                                         for(int r=0;r<rmov;r++)

  85.                                         {

  86.                                                 cout<<"move up

  87. ";//<<mov;

  88.                                         }

  89.                                 }

  90.                                 if(presntc>prevc)

  91.                                 {

  92.                                         //clear(mov);

  93.                                         cmov=presntc-prevc;

  94.                                         //mov={"Right"};

  95.                                         for(int c=0;c<cmov;c++)

  96.                                         {

  97.                                                 cout<<"move Right

  98. ";//<<mov;

  99.  

  100.                                         }

  101.  

  102.                                 }

  103.                                 if(presntc<prevc)

  104.                                 {

  105.                                         //clear(mov);

  106.                                         cmov= -presntc+prevc;

  107.                                         //mov={"Left"};

  108.                                         for(int c=0;c<cmov;c++)

  109.                                         {

  110.                                                 cout<<"move Left

  111. ";//<<mov;

  112.  

  113.                                         }

  114.                                 }

  115.                                 cout<<"press "<<input[k]<<"

  116. ";

  117.                                 i=0;j=0;k++;

  118.                                 prevr=presntr;

  119.                                 prevc=presntc;

  120.                                 //presntr=i;

  121.                                 //presntc=j;

  122.  

  123.  

  124.                         }

  125.                         if(k==strlen(input))

  126.                         {

  127.                             //cout<<"iM STOPPING THIS PROGRAM. IM SORRY:";

  128.  

  129.                return;

  130.                         }

  131.                 }

  132.         }

  133.  

  134. }

  135. main()

  136. {

  137.         trace d;

  138.         char ch;

  139.  

  140.  

  141.        d.getdata();

  142.        d.putdata();

  143.        d.traceit();

  144.  

  145.    cin>>ch;

  146.  

  147.          /*  while(answer=="yes")

  148.            {

  149.                d.getdata();

  150.        d.putdata();

  151.        d.traceit();

  152.            }*/

  153.  

  154. }

  Was this answer useful?  Yes

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])

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions