Ring Route Coding Question
TCS Coding Question Day 2 Slot 1 : Question 2
This question has been asked in the TCS NQT 2020 Day 2 Slot 1
Topic : Array Operations
Time Given : 30 Minutes
Difficulty : 3/5 stars
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 <FLOAT> 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)
Day 2 Slot 1 Question 1
Click on the below button to study Day 2 Slot 2 Question 1 of TCS NQT Coding 2020 exam
TCS Coding Questions
Click on the below button to study more TCS Coding Question
import java.util.*;
public class Busstop {
private static final List route = Arrays.asList(
“TH”, “GA”, “IC”, “HA”, “TE”, “LU”, “NI”, “CA”
);
private static final List fares = Arrays.asList(
800, 600, 750, 900, 1400, 1200, 1100, 1500
);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter source: “);
String source = sc.nextLine();
System.out.print(“Enter destination: “);
String destination = sc.nextLine();
int fare = getFare(source, destination);
if (fare == 0) {
System.out.println(“Invalid Input”);
} else {
System.out.println(fare);
}
sc.close();
}
public static int getFare(String source, String destination) {
double fare = 0.0;
if (!(route.contains(source) && route.contains(destination))) {
fare=0;
}
else if (route.indexOf(source) < route.indexOf(destination)) {
for (int i = route.indexOf(source); i <= route.indexOf(destination); i++) {
fare += fares.get(i);
}
} else if (route.indexOf(destination) < route.indexOf(source)) {
for (int i = route.indexOf(source) + 1; i < route.size(); i++) {
fare += fares.get(i);
}
for (int i = 0; i <= route.indexOf(destination); i++) {
fare += fares.get(i);
}
}
int temp = (int) (fare *0.005);
return temp;
}
}
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
import java.util.*;
import java.util.Arrays;
public class Main
{
public static void main(String[] args)
{
int n,i,start=0,stop=0;
float sum=0;
Scanner sc=new Scanner(System.in);
String s[]= {“TH” , “GA”, “IC” , “HA” , “TE”, “LU” ,”NI”,”CA”};
int d[]={800, 600, 750, 900, 1400, 1200, 1100, 1500};
String str1=sc.next();
String str2=sc.next();
str1=str1.toUpperCase();
str2=str2.toUpperCase();
for(i=0;istop)
{
for(i=start+1;i<8;i++)
{
sum+=d[i];
}
for(i=0;i<=stop;i++)
{
sum+=d[i];
}
}
else
{
for(i=start+1;i<=stop;i++)
{
sum+=d[i];
}
}
System.out.print(sum*0.005);
}
}
}
BusStops = [ “TH”, “GA”, “IC”, “HA”, “TE”, “LU”, “NI”,”CA” ]
Path = [800, 600, 750, 900, 1400, 1200, 1100, 1500]
s, d = input(), input()
if s not in BusStops or d not in BusStops:
print(“INVALID OUTPUT”)
else:
dist =sum(Path[BusStops.index(s):BusStops.index(d) + 1])
cost = dist * 0.005
if cost >= 5:
print(cost, ” INR”)
else:
print(“5 INR”)