TCS NQT - Coding Ability

Test Number 10


1 .Given a maximum of four digit to the base 17(10 -> A, 11 -> B, 12 -> C, 16 -> G) as input, output its decimal value.

 

Input: 23GF

Output: 10980

        

#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;
int main(){
char hex[17];
long long decimal;
int i = 0, val, len;
decimal = 0;
cin>> hex;
len = strlen(hex);
len--;

for(i = 0;hex[i]!='\0';i++)
{
if(hex[i]>='0'&& hex[i]<='9'){
val = hex[i] - 48;
}
else if(hex[i]>='a'&& hex[i]<='g'){
val = hex[i] - 97 + 10;
}
else if(hex[i]>='A'&& hex[i]<='G'){
val = hex[i] - 65 + 10;
}
decimal = decimal + val * pow(17,len);
len--;
}

cout<< decimal;

return 0;
}

       

import java.util.*;
class Main
{
public static void main(String[] args) {
HashMap<Character,Integer> hmap = new HashMap<Character,Integer>();
hmap.put('A',10);
hmap.put('B',11);
hmap.put('C',12);
hmap.put('D',13);
hmap.put('E',14);
hmap.put('F',15);
hmap.put('G',16);
hmap.put('a',10);
hmap.put('b',11);
hmap.put('c',12);
hmap.put('d',13);
hmap.put('e',14);
hmap.put('f',15);
hmap.put('g',16);
Scanner sin = new Scanner(System.in);

String s = sin.nextLine();
long num=0;
int k=0;

for(int i=s.length()-1;i>=0;i--)
{
if((s.charAt(i)>='A'&&s.charAt(i)<='Z')||(s.charAt(i)>='a' &&s.charAt(i)<='z'))
{
num = num + hmap.get(s.charAt(i))*(int)Math.pow(17,k++);
}
else
{
num = num+((s.charAt(i)-'0')*(int)Math.pow(17,k++));
}
}
System.out.println(num);
}
}

num = str(input())
print(int(num,17))





";

2.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 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.

 

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.

 

Constraints:

2<=n<=1000

 

Input: 3

Output: -20 20

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int n;
cin>>n;
char c = 'R';
int x = 0, y = 0;
while(n){
switch(c){
case 'R':
x = abs(x) + 10;
y = abs(y);
c ='U';
break;
case 'U':
y = y + 20;
c = 'L';
break;
case 'L':
x = -(x + 10);
c = 'D';
break;
case 'D':
y = -(y);
c = 'R';
break;
}
n--;
}
cout<< x<< " " << y;
}

import java.util.*;
import java.lang.*;

class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
char c = 'R';
int x = 0, y = 0;
while(n>0){
switch(c){
case 'R':
x = Math.abs(x) + 10;
y = Math.abs(y);
c ='U';
break;
case 'U':
y = y + 20;
c = 'L';
break;
case 'L':
x = -(x + 10);
c = 'D';
break;
case 'D':
y = -(y);
c = 'R';
break;
}
n--;
}
System.out.println(x+" "+y);
}
}

n = int(input())
c = 'R'
x,y=0,0
for i in range(n):
if c=='R':
x = abs(x) + 10;
y = abs(y);
c ='U';
elif c=='U':
y = y + 20;
c = 'L';
elif c=='L':
x = -(x + 10);
c = 'D';
elif c=='D':
y = -(y);
c = 'R';
print(x,y)