Question
Please help with Problem C in Python. I included the code for problem B that will need for problem C. Thanks! #Problem B: Boat Race
Please help with Problem C in Python. I included the code for problem B that will need for problem C. Thanks!
#Problem B: Boat Race
class BoatRace:
'''
Purpose:
The BoatRace class represents information about the race and racers.
Instance variables:
self.race_name: name of the race
self.race_id: ID number for the race
self.distance: distance for the race
self.racers: a list of Boats objects
Methods:
'''
def __init__(self, fname):
self.fname = fname
with open(self.fname, 'r') as file:
lines = file.readlines()
self.race_name = (lines[0].strip().split(','))[1]
self.race_id = int(((lines[1].strip()).split(','))[1])
self.distance = int((lines[2].strip().split(','))[1])
print (self.race_id)
self.distance = int((lines[2].strip().split(','))[1])
self.racers = []
for i in range(3, len(lines)):
boat_info = lines[i].strip().split(',')
self.racers.append(Boat(boat_info[0], int(boat_info[1])))
#Getter
def get_race_name(self):
return self.race_name
def get_race_id(self):
return self.race_id
def get_distance(self):
return self.distance
def get_racers(self):
return self.racers
#Problem C: ???
Add the following methods to your Boat Race class:
- a method calledadd_racer which takes in aBoat object and adds it to the end of the racers list. The function does not return anything.
- a method calledprint_racers which loops throughracers and prints theBoatobjects. This function takes in no parameters (other thanself) and returns nothing.
- a method calledcount that returns the number of racers.
- a method calledrace.
- The race function calls the move function for all of the racers in theBoatRace.
- Once all the racers have moved, call theprint_racers method to display information about the progress of each boat.
- Then, check if any of the racer'scurrent_progress is greater than or equal to the race'sdistance.
- If so, then return a list of all of the racers whosecurrent_progress is greater than or equal todistance.
- If no racer has finished the race then repeat the calls to move and check until at least one racer has finished the race.
Examples:
Copy the following if __name__ == "__main__" block into your hw11.py file, and comment out tests for parts of the class you haven't implemented yet. The lines that have output include the expected value next to them as a comment (note that some lines are random and your output will not match exactly).
The first race example in the code below should finish within a few turns, and unless you get extremely unlucky, The Leaf should win a solo victory almost every time due to its top speed. The second race, on the other hand, should almost always finish in a single round with three or four boats tied for victory.
if __name__ == '__main__':
the_race = BoatRace('the_big_one.csv')
the_race.add_racer(Boat("Late", 2))
the_race.print_racers() #The Fire Ball: 0
#The Leaf: 0
#Late: 0
print(the_race.count()) #3
result = the_race.race() #The Fire Ball: 5
#The Leaf: 39
#Late: 2
#The Fire Ball: 17
#The Leaf: 56
#Late: 4
#The Fire Ball: 19
#The Leaf: 145
#Late: 4
print(result) #[<__main__.Boat object at 0x03A2E4F0>]
print(result[0].get_boat_name()) #The Leaf
print(result[0].get_current_progress()) #145
second_race = BoatRace('very_short.csv')
second_result = second_race.race() #Runner: 84
#Whirlwind: 10
#Boaty McBoatFace: 4
#Greased Lightning: 78
#Much Speed: 43
print(len(second_result)) #4
Step by Step Solution
3.45 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started