Hello, I need help in all of these for coding in python. I know its really long
ID: 3827075 • Letter: H
Question
Hello, I need help in all of these for coding in python. I know its really long and I am so sorry but I would really really appreciate it if you can help me with it!
Train class Instance variables: the name of the train String name the maximum number of passengers the train can transport max passengers the number of passengers currentl on the train num passengers speed fps how fast the train travels (in feet per second) Methods name, max passengers, speed fps) constructor, initializes all instance variables, init (self, num passengers should default to 0 when a train is created with the constructor o Assumptions speed fps and max passengers will always be a non-negative number name will be a non-empty string (self) returns string representation with this format str Train named Orient Express with 5 passengers will travel at 10.20mph Notes: The quotes in the example are just our string boundaries! The first character in the above example is T This method reports num passengers (NOT max passengers) Train speed is converted to mile per hour and always presented with 2 digits after the decimal point. time to travel(self, distance feet) Returns how long (in whole seconds) this train will take to travel distance feet (distance in feet) o Notes and Assumptions This always returns an integer- you could use integer division or normal division followed by int conversion to truncate the fractional part. You can assume that distance feet is always a non-negative integer load passengers(self, num people) Attempts to put num people onto the train and update num passengers. If the number of people supplied would not fit on the train, raise a TraincapacityException and do not change the number of passengers currently on the train. [You may imagine this as "the passengers get into a fight and no one is allowed on the train".J o Note and Assumptions: You can assume that num people is always a non-negative integer. When raising the exception, the number to raise is the number of people that cannot fit on the train (not the number of people that try to board) This function should return None. Remember: None is the default return if you don't return anything! You don't need to explicitly return None unload passengers(self, num people) Attempts to remove num people from the train. If the number of people supplied is larger than the number of people on the train currently, raise a TraincapacityException and do not change the number of passengers currently on the train. [You may imagine this as the passengers are so confused by the request that they just stay where they are looking confused" o Note and Assumptions: You can assume that num people is always a non-negative integer When raising the exception, the number to raise is the number of people that cannot unload, not the number of people that try to exit the train This function should return None. Remember: None is the default return if you don't return anything! You don't need to explicitly return NoneExplanation / Answer
import math
class TrainCapacityException(Exception):
def __init__(self, number, issue="full"):
self.number = number
self.issue = issue
def __str__(self):
if self.issue == "full":
return str(self.number) + " passengers cannot be loaded because the train is full!"
else:
return str(self.number) + " passengers cannot be unloaded because the train is empty!"
class Train:
def __init__(self, name, max_passengers, speed_fps):
self.name = name
self.max_passengers = max_passengers
self.num_passengers = 0
self.speed_fps = speed_fps
def __str__(self):
train = "Train named " + self.name + " with " + str(self.num_passengers)
speed_mps = 0.681818*self.speed_fps
train += " passengers will travel at " + "{0:.2f}".format(speed_mps) + "mph"
return train
def time_to_travel(self, distance_feet):
return distance_feet//self.speed_fps
def load_passengers(self, num_people):
if self.num_passengers + num_people <= self.max_passengers:
self.num_passengers += num_people
else:
over_capacity = (self.num_passengers + num_people) - self.max_passengers
raise TrainCapacityException(over_capacity)
def unload_passengers(self, num_people):
if self.num_passengers - num_people >= 0:
self.num_passengers -= num_people
else:
under_capacity = num_people - self.num_passengers
raise TrainCapacityException(under_capacity, "empty")
class City:
def __init__(self, name, loc_x, loc_y, stop_time):
self.name = name
self.loc_x = loc_x
self.loc_y = loc_y
self.stop_time = stop_time
def __str__(self):
result = self.name + " (" + str(self.loc_x) + "," + str(self.loc_y) + "). "
minutes = self.stop_time/60.0
result += "Exchange time: " + "{0:.2f}".format(minutes) + " minutes"
return result
def __eq__(self, other):
return (self.loc_x == other.loc_x) and (self.loc_y == other.loc_y)
def distance_to_city(self, city):
distance = math.sqrt((self.loc_x - city.loc_x)**2 + (self.loc_y - city.loc_y)**2)
return distance
# this is incomplete as student didn't provide complete link
class Journey:
def __init__(self, train, destinations=None, start_time=0):
if not isinstance(train, Train):
raise TypeError("train should be instance of Train")
self.train = train
if not destinations:
self.destinations = []
else:
if not type(destinations) is list:
raise TypeError("destinations should be list")
for city in destinations:
if not isinstance(city, City):
raise TypeError("city should be instance of City")
self.destinations = destinations
self.start_time = start_time
def city_departure_time(self, city):
if not city in self.destinations:
return
time = self.start_time
departure_time = time
for i in range(1, len(self.destinations)):
time = time + self.destinations[i-1].stop_time
if city == self.destinations[i-1]:
departure_time = time
time = time +int((self.destinations[i].distance_to_city(self.destinations[i-1]))/self.train.speed_fps)
if self.destinations[-1] == city:
departure_time = time + city.stop_time
return departure_time
def city_arrival_time(self, city):
if not city in self.destinations:
return
time = self.start_time
arrival_time = time
for i in range(1, len(self.destinations)):
if self.destinations[i-1] == city:
return arrival_time
arrival_time = arrival_time + int((self.destinations[i].distance_to_city(self.destinations[i-1]))/self.train.speed_fps) + self.destinations[i-1].stop_time
if self.destinations[-1] == city:
return arrival_time
return
def add_destination(self,c):
self.destinations.append(c)
def city_in_journey(self,c):
return c in self.destinations
def check_journey_includes(self, start_city, dest_city):
found_start = False
for dest in self.destinations:
if found_start and dest == dest_city:
return True
if dest==start_city:
found_start = True
return False
def total_journey_time(self):
if not self.destinations:
return 0
return self.city_departure_time(self.destinations[-1]) - self.start_time
def total_journey_distance(self):
if len(self.destinations) < 2:
return 0
distance = 0
for i in range(1, len(self.destinations)):
distance += self.destinations[i].distance_to_city(self.destinations[i-1])
return distance
def all_passengers_accommodated(self, unload_list, load_list):
for i in range(0, len(self.destinations)):
if unload_list[i] != 0:
try:
self.train.unload_passengers(unload_list[i])
except:
return False
if load_list[i] != 0:
try:
self.train.load_passengers(load_list[i])
except:
return False
return True
def __str__(self):
result = "Journey with " + str(len(self.destinations)) + " stops: "
for destination in self.destinations:
result += " " + str(destination) + " "
result += "Train Information: " + str(self.train) + " "
return result
# pastebin link for code : https://pastebin.com/rc817r8D
...............................................................................................
----------------------------------------------------------------------
Ran 95 tests in 0.002s
OK
Test cases: 95/95 passed
Score based on test cases: 95.00/100
Please give a thumbs up.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.