TCS NQT - Coding Ability

Test Number 3


1 .Problem Statement

We want to estimate the cost of painting a property. Interior wall painting cost is Rs.18 per sq.ft. and exterior wall painting cost is Rs.12 per sq.ft.

Take input as
1. Number of Interior walls
2. Number of Exterior walls
3. Surface Area of each Interior 4. Wall in units of square feet
Surface Area of each Exterior Wall in units of square feet

If a user enters zero as the number of walls then skip Surface area values as User may don’t want to paint that wall.

Calculate and display the total cost of painting the property
Example 1:

6
3
12.3
15.2
12.3
15.2
12.3
15.2
10.10
10.10
10.00
Total estimated Cost : 1847.4 INR

Note: Follow in input and output format as given in above example

   

#include<stdio.h>
int main()
{
int ni,ne,i=0;
float int_p=18,ext_p=12,cost=0,temp;
scanf("%d %d",&ni,&ne);
if(ni<0 || ne<0 )
{
printf("INVALID INPUT");
}
else if(ni==0 && ne==0)
{
printf("Total estimated Cost : 0.0");
}
else
{
for(i=0;i<ni;i++)
{
scanf("%f",&temp);
cost+= int_p*temp;
}
for(i=0;i<ne;i++)
{
scanf("%f",&temp);
cost+= ext_p*temp;
}
printf("Total estimated Cost : %.1f",cost);
}
return 0;
}

  

#include<iostream>
using namespace std;
int main()
{
int ni,ne,i=0;
float int_p=18,ext_p=12,cost=0,temp;
scanf("%d %d",&ni,&ne);
if(ni<0 || ne<0 )
{
cout<<"INVALID INPUT";
}
else if(ni==0 && ne==0)
{
cout<<"Total estimated Cost : 0.0";
}
else
{
for(i=0;i<ni;i++)
{
cin>>temp;
cost+= int_p*temp;
}
for(i=0;i<ne;i++)
{
cin>>temp;
cost+= ext_p*temp;
}
cout<<"Total estimated Cost : "<<cost;
}
return 0;
}

  

import java.util.Scanner;
class Main {
public static void main(String[] args) {
int ni, ne, i = 0;
float intP = 18, extP = 12, cost = 0, temp;
Scanner sc = new Scanner(System.in);
ni = sc.nextInt();
ne = sc.nextInt();
if(ni < 0 || ne < 0) {
System.out.print("INVALID INPUT");
} else if(ni == 0 && ne == 0) {
System.out.print("Total estimated Cost : 0.0");
} else {
for(i = 0; i < ni; i++) {
temp = sc.nextFloat();
cost += intP * temp;
}
for(i = 0; i < ne; i++) {
temp = sc.nextFloat();
cost += extP * temp;
}
System.out.printf("Total estimated Cost : %.1f", cost);
}
}
}

  

interior_walls = int(input())
exterior_walls = int(input())
if interior_walls:
int_walls = []
for i in range(interior_walls):
int_walls.append(float(input()))

if exterior_walls:
ext_walls = []
for i in range(exterior_walls):
ext_walls.append(float(input()))
if exterior_walls < 0 or interior_walls < 0:
print(“Invalid Input”)
exit()
if exterior_walls and interior_walls:
print("Total estimated Cost : ",(sum(int_walls)*18+sum(ext_walls)*12),"INR")
else:
if exterior_walls:
print("Total estimated Cost : ",sum(ext_walls)*12,"INR")
elif interior_walls:
print("Total estimated Cost : ",sum(int_walls)*18,"INR")
else:
print("Total estimated Cost : 0.0 INR")





";

2. Problem Statement

A City Bus is a Ring Route Bus which runs in circular fashion.That is, Bus once starts at the Source Bus Stop, halts at each Bus Stop in its Route and at the end it reaches the Source Bus Stop again.
If there are n number of Stops and if the bus starts at Bus Stop 1, then after nth Bus Stop, the next stop in the Route will be Bus Stop number 1 always.
If there are n stops, there will be n paths.One path connects two stops. Distances (in meters) for all paths in Ring Route is given in array Path[] as given below:
Path = [800, 600, 750, 900, 1400, 1200, 1100, 1500]
Fare is determined based on the distance covered from source to destination stop as Distance between Input Source and Destination Stops can be measured by looking at values in array Path[] and fare can be calculated as per following criteria:

If d =1000 metres, then fare=5 INR
(When calculating fare for others, the calculated fare containing any fraction value should be ceiled. For example, for distance 900n when fare initially calculated is 4.5 which must be ceiled to 5)
Path is circular in function. Value at each index indicates distance till current stop from the previous one. And each index position can be mapped with values at same index in BusStops [] array, which is a string array holding abbreviation of names for all stops as-
“THANERAILWAYSTN” = ”TH”, “GAONDEVI” = “GA”, “ICEFACTROY” = “IC”, “HARINIWASCIRCLE” = “HA”, “TEENHATHNAKA” = “TE”, “LUISWADI” = “LU”, “NITINCOMPANYJUNCTION” = “NI”, “CADBURRYJUNCTION” = “CA”

Given, n=8, where n is number of total BusStops.
BusStops = [ “TH”, ”GA”, ”IC”, ”HA”, ”TE”, ”LU”, ”NI”,”CA” ]

Write a code with function getFare(String Source, String Destination) which take Input as source and destination stops(in the format containing first two characters of the Name of the Bus Stop) and calculate and return travel fare.

Example 1:
Input Values
ca
Ca

Output Values
INVALID OUTPUT

Example 2:
Input Values
NI
HA
Output Values
23.0 INR

Note: Input and Output should be in format given in example.
Input should not be case sensitive and output should be in the format INR

          
       

#include <bits/stdc++.h>
using namespace std;
int main() {
string s , d;
cin>>s>>d;
transform(s.begin(),s.end() , s.begin(),::toupper);
transform(d.begin(),d.end() , d.begin(),::toupper);
string arrs[8] = {"TH" , "GA", "IC" , "HA" , "TE", "LU" ,"NI","CA"};
float arr[8]={800,600,750,900,1400,1200,1100,1500};
float res=0;
int st ,ed;
for(int i=0;i<8;i++)
{
if(s==arrs[i])
st=i;

if(d==arrs[i])
ed=i;
}
if(st==ed)
{
cout<<" INVALID INPUT";
return 0;
}
else
{
int i=st+1;
cout<<i;
while(i!=ed+1)
{
res+=(arr[i]);
i=(i+1)%8;
}
cout<<(ceil)(res*0.005);
return 0;
}
}

          
     

import math
def getFare(source,destination):
route=[ [ "TH", "GA", "IC", "HA", "TE", "LU", "NI", "CA"],
[800,600,750,900,1400,1200,1100,1500]
]
fare = 0.0
if not (source in route[0] and destination in route[0]):
print("Invalid Input")
exit()
if route[0].index(source) < route[0].index(destination):
for i in range(route[0].index(source),route[0].index(destination)+1):
fare+=route[1][i]
elif route[0].index(destination) < route[0].index(source):
for i in range(route[0].index(source)+1,len(route[0])):
fare+=route[1][i]
for i in range(0,route[0].index(destination)+1):
fare+=route[1][i]
return float(math.ceil(fare*0.005))

source = input()
destination = input()
fare = getFare(source,destination)
if fare == 0:
print("Invalid Input")
else:
print(fare)