TCS Coding Question 1 | Our hoary culture had several great persons ….

Problem Statement

Our hoary culture had several great persons since time immemorial and king vikramaditya’s nava ratnas (nine gems) belongs to this ilk.They are named in the following shloka: Among these, Varahamihira was an astrologer of eminence and his book Brihat Jataak is recokened as the ultimate authority in astrology.

He was once talking with Amarasimha,another gem among the nava ratnas and the author of Sanskrit thesaurus, Amarakosha.

Amarasimha wanted to know the final position of a person, who starts from the origin 0 0 and travels per following scheme. Scheme

• He first turns and travels 10 units of distance
• His second turn is upward for 20 units
• Third turn is to the left for 30 units
• Fourth turn is the downward for 40 units
• Fifth turn is to the right(again) for 50 units

… And thus he travels, every time increasing the travel distance by 10 units.

Test Cases

Case 1

• Input : 3
• Expected Output :-20 20

Case 2

• Input: 4
• Expected Output: -20 -20

Case 3

• Input : 5
• Expected Output : 30 -20

Case 4

• Input : 7
• Expected Output : 90 -20 #include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
scanf("%d", &n);

char c = 'R';
int x = 0, y = 0;
int distance = 10;

while(n)
{
switch(c)
{
case 'R':
x = x + distance;
c = 'U';
distance = distance + 10;
break;

case 'U':
y = y + distance;
c = 'L';
distance = distance + 10;
break;

case 'L':
x = x - distance;
c = 'D';
distance = distance + 10;
break;

case 'D':
y = y - distance;
c = 'A';
distance = distance + 10;
break;

case 'A':
x = x + distance;
c = 'R';
distance = distance + 10;
break;
}
n--;
}

printf("%d %d",x,y);
return 0;
}
#include <iostream>

using namespace std;
int main()
{
int n;
cin >> n;

char c = 'R';
int x = 0, y = 0;
int distance = 10;

while(n)
{
switch(c)
{
case 'R':
x = x + distance;
c = 'U';
distance = distance + 10;
break;

case 'U':
y = y + distance;
c = 'L';
distance = distance + 10;
break;

case 'L':
x = x - distance;
c = 'D';
distance = distance + 10;
break;

case 'D':
y = y - distance;
c = 'A';
distance = distance + 10;
break;

case 'A':
x = x + distance;
c = 'R';
distance = distance + 10;
break;
}
n--;
}

cout << x << " " << y <<endl;
return 0;
}
import java.util.Scanner;

public class Main {

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
getDistance(testCase);
}

public static void getDistance(int a) {
int distance = 10;
int x = 0;
int y = 0;
char ch = 'R';
while(a > 0)
{
switch(ch)
{
case 'R':
x = x + distance;
ch = 'U';
distance = distance+10;
break;

case 'U':
y = y + distance;
ch = 'L';
distance = distance + 10;
break;

case 'L':
x = x - distance;
ch = 'D';
distance = distance + 10;
break;

case 'D':
y = y - distance;
ch = 'A';
distance = distance + 10;
break;

case 'A':
x = x + distance;
ch = 'R';
distance = distance + 10;
break;
}
a--;
}
System.out.println(x+ " , "+y);
}

}
n = int(input())
c = 'R'
dis = 10
x,y=0,0
for i in range(n):
if c=='R':
x=x+dis
c='U'
dis=dis+10
elif c=='U':
y=y+dis
c='L'
dis=dis+10
elif c=='L':
x=x-dis
c='D'
dis=dis+10
elif c=='D':
y=y-dis
c='A'
dis=dis+10
elif c=='A':
x=x+dis
c='R'
dis=dis+10
print(x,y)
\$n=<>;
\$n=\$n*1;
\$c=0;
\$dis=10;
\$x=0;
\$y=0;
for(my \$i=0;\$i<\$n;\$i++)
{
if(\$c==0)
{
\$x=\$x+\$dis;
\$c=1;
\$dis=\$dis+10;
}
elsif(\$c==1)
{
\$y=\$y+\$dis;
\$c=2;
\$dis=\$dis+10;
}
elsif(\$c==2)
{
\$x=\$x-\$dis;
\$c=3;
\$dis=\$dis+10;
}
elsif(\$c==3)
{
\$y=\$y-\$dis;
\$c=4;
\$dis=\$dis+10;
}
elsif(\$c==4)
{
\$x=\$x+\$dis;
\$c=0;
\$dis=\$dis+10;
}
}
print \$x;
print " ";
print \$y;

22 comments on “TCS Coding Question 1 | Our hoary culture had several great persons ….”

• Syed

public static void main(String args[]{
System.out.println(“Enter input”);
(This is missing in java program) 0
• anmolshrivastav.08

C++ implementation
#include
using namespace std;

int main(){
int t;
cin>>t;
int n = 5, dis = 10;
int x = 0 , y = 0, j = 0;
for(int i=0; i<t; i++, j++){

if(j == 5){
j = 0;
}

if(j == 0){
x += dis;
dis += 10;
}else if(j == 1){
y += dis;
dis += 10;
}else if(j == 2){
x -= dis;
dis += 10;
}else if(j == 3){
y -= dis;
dis += 10;
}else{
x += dis;
dis += 10;
}

}
cout<<x<<" "<<y;
return 0;
} 0
• #include
using namespace std;

int main()
{
int d, n;
int x, y;

cin >> n;

for( x = y = 0, d = 10 ; d <= n*10 ; d += 10 )
{
switch( d%40 )
{
case 10 : x += d; break;
case 20 : y += d; break;
case 30 : x -= d; break;
case 0 : y -= d; break;
}
}

cout << x << ' ' << y;

return 0;
} 0
• Daipayan

Much Simpler way
/**
* prepCodin1
*/
import java.util.Scanner;

public class prepCodin1 {
public static void getDistance(int a) {
int x = 0;
int y = 0;
for (int i = 0; i < a; i++) {
if ((i) % 5 == 0) {
x = x + (10 * (i + 1));
} else if ((i) % 5 == 1) {
y = y + (10 * (i + 1));
} else if ((i) % 5 == 2) {
x = x – (10 * (i + 1));
} else if ((i) % 5 == 3) {
y = y – (10 * (i + 1));
} else {
x = x + (10 * (i + 1));
}

}
System.out.println(x + " , " + y);
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
getDistance(testCase);
}
} 0
• Debparna

import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x=0;int y=0;int d=10;
for(int i=1;i<=n;i++){
if(i%4==1){
x=x+d;
d=d+10;
}else if(i%4==2){
y=y+d;
d=d+10;
}else if(i%4==3){
x=x-d;
d=d+10;
}else if(i%4==0){
y=y-d;
d=d+10;
}
}
System.out.println(x+" "+y);
}
} 0
• W

python code is wrong 0