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
C
C++
Java
Python
Perl
C
#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; }
C++
#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; }
Java
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); } }
Python
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)
Perl
$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;
Login/Signup to comment
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
getdistance(n);
}
public static void getdistance(int n){
int x=0,y=0;
char ch=’R’;
int distance=10;
while(n>0){
switch(ch){
case ‘R’:
x+=distance;
distance+=10;
ch=’U’;
break;
case ‘U’:
y+=distance;
distance+=10;
ch=’L’;
break;
case ‘L’:
x-=distance;
distance+=10;
ch=’D’;
break;
case ‘D’:
y-=distance;
distance+=10;
ch=’A’;
break;
case ‘A’:
x+=distance;
distance+=10;
ch=’R’;
break;
}
n–;
}
System.out.println(x+” “+y);
}
}
Testcase 6 will be 90 -20 and 7 will be 90 50
n = int(input())
x,y=0,0
for i in range(1,n+1):
c=i%5
if c == 1:
x = x+(10*i)
elif c == 2:
y = y+(10*i)
elif c == 3:
x = x-(10*i)
elif c == 4:
y = y-(10*i)
else:
x = x+(10*i)
print(x,y)