import sys
# Function that will return the arrival of train at HYB station excluding all the bogies that lies before HYB
def arrivingHYB(Train, station_dict):
# making the list of arrival train now contains only ["ARRIVAL", "TRAIN_A" or "Train_B", "ENGINE"]
arrival = ["ARRIVAL"] + Train[:2]
HYB = station_dict["HYB"]
# Inserting only all the bogies that travel to HYB and after HYB
for bogie in Train[2:]:
if bogie not in station_dict or station_dict[bogie] >= HYB:
arrival.append(bogie)
return arrival
# Function that will return the assembled train that depart from HYB
def departHYB(Train_A, Train_B, station_after_HYB):
# list to store the boggies of Train_A and Train_B that depart HYB excluding HYB bogie or bogies,
# deleting individual HYB bogies of from Train_A and Train_B variable will take more time than storing all bogies that depart from HYB
# excluding HYB
bogies_that_depart_HYB = list()
# Example list of Train_A = ["ARRIVAL", "TRAIN_A", "ENGINE", "NDL", "NDL", "GHY", "NJP", "NGP"]
# iterating only boggies of Train_A that is from index 3 excluding unnecessary iteration
for bogie in Train_A[3:]:
if bogie != "HYB":
bogies_that_depart_HYB.append(bogie)
# iterating only boggies of Train_B that is from index 3 excluding unnecessary iteration
for bogie in Train_B[3:]:
if bogie != "HYB":
bogies_that_depart_HYB.append(bogie)
# Checking if there is no boggies to travel after HYB than the function will return None
if len(bogies_that_depart_HYB) < 1:
return None
# sorting all the boggies in descending order
departure = sorted(bogies_that_depart_HYB, key= lambda x : station_after_HYB[x], reverse=True)
# returns the output value
return (["DEPARTURE", "TRAIN_AB", "ENGINE", "ENGINE"]+departure)
def main():
# sys.argv[1] should give the absolute path to the input file
input_file = sys.argv[1]
# parse the file and process the command
input_data = open(input_file, 'r')
Train_A, Train_B = input_data.read().split("\n")
Train_A = Train_A.strip().split()
Train_B = Train_B.strip().split()
# ALL station names with their distance from source station
station_dictA = {"CHN": 0, "SLM": 350, "BLR": 550, "KRN": 900, "HYB": 1200,
"NGP": 1600, "ITJ": 1900, "BPL": 2000, "AGA": 2500, "NDL": 2700}
station_dictB = {"TVC": 0, "SRR": 300, "MAQ": 600, "MAO": 1000, "PNE": 1400, "HYB": 2000,
"NGP": 2400, "ITJ": 2700, "BPL": 2800, "PTA": 3800, "NJP": 4200, "GHY": 4700}
# ALL station names with their distance from HYB station till BPL and after BPL the station and distance are from their source station
# so that the sorting can be done properly
station_after_HYB = {"HYB": 0, "NGP": 400, "ITJ": 700, "BPL": 800,
"AGA": 2500, "NDL": 2700, "PTA": 3800, "NJP": 4200, "GHY": 4700}
# print the output
Train_A = arrivingHYB(Train_A, station_dictA)
Train_B = arrivingHYB(Train_B, station_dictB)
# printing values for arrival of Train_A in HYB
print(" ".join(Train_A))
# printing values for arrival of Train_B in HYB
print(" ".join(Train_B))
departure = departHYB(Train_A, Train_B, station_after_HYB)
# Not sure what to print if there is no boggies to travel after HYB so therefore printing nothing for None value
if departure!=None:
# printing values for departure of both train from HYB
print(" ".join(departure))
if __name__ == "__main__":
main()
program description
All Geektrust challenges are meant to be solved offline. Your score will be determined based on factors such as OOPS, readability, and scalability.
For the Train problem, do give attention to using the right data structures.
You should read what we look for in your solution before you start coding.
For this train problem, do make sure you use the right data structures in your solution.
It is NOT about just getting the right output.
Please go through the build instructions here
The challenge
Train A and Train B are two limited stop super fast trains that travel from Chennai to New Delhi and Trivandrum to Guwahati.
The uniqueness of these trains are that for some part of the journey they follow the same route and travel as one train - Train AB.
Also passengers can board this train only at the source stations, but they can get down at any station enroute.
There are only reserved bogies and each bogie will only have passengers to a specific station.
When the train arrives in the station enroute, instead of waiting for the passengers to get down, the entire bogie is removed from the train, and the train continues its journey.
These train’s reservation systems are designed in a unique way where Train A can have passengers in the route for Train B and vice versa.
For e.g. people can board from Chennai in Train A and travel to Guwahati.
Trains start from their respective source stations and meet at Hyderabad on Wednesdays every week at exactly the same time
and travel till Bhopal as a single train, from where it again travels as two independent trains.
At Hyderabad both trains are merged as single train, Train AB. However the merging is done based on the distance each remaining bogie in both the trains has to travel.
First both the engines are attached and then the bogies are attached in the descending order of distances they have to travel further from Hyderabad.
This is done so that the last bogie can be immediately detached at the respective stations and the train can continue its journey quickly.
The routes and distances of each train are as given. Station codes can be used for ease of inputs and outputs.
The distances are also given.
Train A |
---|
STATION (CODE) - DISTANCE |
CHENNAI (CHN) - 0
SALEM (SLM) - 350
BANGALORE (BLR) - 550
KURNOOL (KRN) - 900
HYDERABAD (HYB) - 1200
NAGPUR (NGP) - 1600
ITARSI (ITJ) - 1900
BHOPAL (BPL) - 2000
AGRA (AGA) - 2500
NEW DELHI (NDL) - 2700
|Train B|
|STATION (CODE) - DISTANCE|
TRIVANDRUM (TVC) -0
SHORANUR (SRR) - 300
MANGALORE (MAQ) - 600
MADGAON (MAO) - 1000
PUNE (PNE) - 1400
HYDERABAD (HYB) - 2000
NAGPUR (NGP) - 2400
ITARSI (ITJ) - 2700
BHOPAL (BPL) - 2800
PATNA (PTA) - 3800
NEW JALPAIGURI (NJP) - 4200
GUWAHATI (GHY) - 4700|
Given initial bogie order of both trains your program should print the bogie order of arrival of each train at Hyderabad,
Train AB’s departure bogie order from Hyderabad.
Your program should take as input:
- The order of bogies for train A while departing from Chennai.
- The order of bogies for train B while departing from Trivandrum.
The output should be:
- The order of bogies for train A while arriving at Hyderabad.
- The order of bogies for train B while arriving at Hyderabad.
- The order of bogies for train AB (merged train) while departing from Hyderabad.
Input Commands
There are 2 input commands defined to separate out the actions. Your input format will start with either of these commands i.e TRAIN_A, TRAIN_B
TRAIN_A
The TRAIN_A command receives the initial order of bogies (seperated by space) for train A while departing from Chennai.
Format - TRAIN_A ENGINE BOGIE_1 BOGIE_2 BOGIE_3 … BOGIE_N
Example- TRAIN_A ENGINE NDL NDL KRN GHY SLM NJP NGP BLR
TRAIN_B
The TRAIN_B command receives the initial order of bogies (seperated by space) for train B while departing from Trivandrum.
Format - TRAIN_B ENGINE BOGIE_1 BOGIE_2 BOGIE_3 … BOGIE_N
Example- TRAIN_B ENGINE NJP GHY AGA PNE MAO BPL PTA
Assumptions
-
The passengers board only from the source station.
-
If there are no passenger bogies to travel from Hyderabad station, then train should stop there (Need to output differently).
-
The distances are in Kilometers.
-
If there are multiple bogies with same station as its destination, then they can be arranged next to each other when the Train AB leaves Hyderabad.
SAMPLE INPUT-OUTPUT 1
INPUT:
TRAIN_A ENGINE NDL NDL KRN GHY SLM NJP NGP BLR
TRAIN_B ENGINE NJP GHY AGA PNE MAO BPL PTA
OUTPUT:
ARRIVAL TRAIN_A ENGINE NDL NDL GHY NJP NGP
ARRIVAL TRAIN_B ENGINE NJP GHY AGA BPL PTA
DEPARTURE TRAIN_AB ENGINE ENGINE GHY GHY NJP NJP PTA NDL NDL AGA BPL NGP
SAMPLE INPUT-OUTPUT 2
INPUT:
TRAIN_A ENGINE SLM BLR KRN HYB SLM NGP ITJ
TRAIN_B ENGINE SRR MAO NJP PNE PTA
OUTPUT:
ARRIVAL TRAIN_A ENGINE HYB NGP ITJ
ARRIVAL TRAIN_B ENGINE NJP PTA
DEPARTURE TRAIN_AB ENGINE ENGINE NJP PTA ITJ NGP