Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task 2 ] - At The Races ( 2 5 % ) You are NOT allowed to use try / except blocks in Task 2
Task At The Races You are NOT allowed to use tryexcept blocks in Task Task Setup Now that we have our runners, we will turn our attention to the races. In the scaffold, you will see a somewhat complete code for the races. There are two types of races Short and Marathon. What has been given with errors There are certain features and attributes that the races share in common, however, some of them are different. As was the case in the previous task, here are the details of the attributes used in these classes: racetype : A string that contains the type of the race. If its a short race, this will contain the string "short" whereas if the race is a marathon, then this string will contain "long". distance : A positive floating value that contains in kilometres the distance of the race. Distances vary even within marathons and short races. runners : A list of runner objects from the runner class in the previous task maximumparticipants : An integer that defines the maximum number of participants that can participate in the race. This value is for short races and for marathons. timemultiplier : Our calculations consider the runners running at the highest speed for the entire duration of a short race. To cater for the error, we apply a timemultiplier which fixes the time calculated for a runner to run the short race. The value of this multiplier is currently set to for all short races. This value doesn't exist for marathon races. For example if a runner's speed is x meters per second, and the distance is y meters, then the time should be calculated as time time x y energyperkm : An integer set to for all marathons that contains the amount of energy consumed per kilometer run of a marathon. When the race is conducted for a marathon, energy is consumed per km and the racers that run out of energy before the marathon finishes don't qualify as a finisher. This value doesn't exist for short races. The classes given to you also has the following methods: initself distance, runners None : this is the constructor for the RaceABC class that sets up the Race. As you can see, runners is an optional parameter. If the value of runners is passed as None or if no value for runners is provided, you should initialise the runners as an empty list. However, if there are values provided, then they should only be objects of the Runner class. addrunnerself : this method adds a runner to the race. However, if the runner already exists, then this method should raise a RunnerAlreadyExistsError, which has been imported for you already. You should also make sure that the number of runners don't exceed the maximumparticipants. In case adding the runner will exceed the maximum participants, you should raise a RaceIsFull error. removerunnerself : this method removes a runner from the race. However, if the runner does not exist then this method should raise a RunnerDoesntExistError, which has been imported for you already. conductraceself : this method should conduct a race based on the type of race that we are considering. For a short race: We call the runrace method for each runner and calculate the time taken to run the race. For a marathon race: We call the runrace method once for each runner for each kilometre of the marathon race. We also reduce the runner's energy according to the value set in the class. For this purpose, feel free to round up the distance to the nearest integer. If the runner runs out of energy, their finish time is set to DNFDid Not Finish After the conclusion of the conductrace method, a list of tuples should be returned this code is already done for you For You To Do We definitely need to find better TAs because these ones keep making mistakes! You are tasked with the following: Copy and replace the code for runner.py that is given in the scaffold with your pristine code from the previous task. Fix the errors that have been made in the class, variables, and methods based on the descriptions provided above. There may be cases where you need to remove some of the methods if you are implementing inheritance. NOTE: You may need to moveremove some of the variables and methods in between the three classes once you implement proper inheritance. Please ensure that the names of the methodsvariables DO NOT CHANGE from the ones given to you. Check the preconditions and postconditions for each of the methods. Make sure to raise the relevant errors if incorrect values are passed to the functions. Document your code including typehinting for all variables and arguments for functions and methods, as well as include relevant docstrings for each method and class. Build on the unit tests that have already been given to you in the file testraces.py You are expected to create at least three tests for each method. You
Task At The Races
You are NOT allowed to use tryexcept blocks in Task
Task Setup
Now that we have our runners, we will turn our attention to the races. In the scaffold, you will see a somewhat complete code for the races. There are two types of races Short and Marathon.
What has been given with errors
There are certain features and attributes that the races share in common, however, some of them are different. As was the case in the previous task, here are the details of the attributes used in these classes:
racetype : A string that contains the type of the race. If its a short race, this will contain the string "short" whereas if the race is a marathon, then this string will contain "long".
distance : A positive floating value that contains in kilometres the distance of the race. Distances vary even within marathons and short races.
runners : A list of runner objects from the runner class in the previous task
maximumparticipants : An integer that defines the maximum number of participants that can participate in the race. This value is for short races and for marathons.
timemultiplier : Our calculations consider the runners running at the highest speed for the entire duration of a short race. To cater for the error, we apply a timemultiplier which fixes the time calculated for a runner to run the short race. The value of this multiplier is currently set to for all short races. This value doesn't exist for marathon races.
For example if a runner's speed is
x meters per second, and the distance is
y meters, then the time should be calculated as
time
time
x
y
energyperkm : An integer set to for all marathons that contains the amount of energy consumed per kilometer run of a marathon. When the race is conducted for a marathon, energy is consumed per km and the racers that run out of energy before the marathon finishes don't qualify as a finisher. This value doesn't exist for short races.
The classes given to you also has the following methods:
initself distance, runners None : this is the constructor for the RaceABC class that sets up the Race. As you can see, runners is an optional parameter. If the value of runners is passed as None or if no value for runners is provided, you should initialise the runners as an empty list. However, if there are values provided, then they should only be objects of the Runner class.
addrunnerself : this method adds a runner to the race. However, if the runner already exists, then this method should raise a RunnerAlreadyExistsError, which has been imported for you already. You should also make sure that the number of runners don't exceed the maximumparticipants. In case adding the runner will exceed the maximum participants, you should raise a RaceIsFull error.
removerunnerself : this method removes a runner from the race. However, if the runner does not exist then this method should raise a RunnerDoesntExistError, which has been imported for you already.
conductraceself : this method should conduct a race based on the type of race that we are considering.
For a short race: We call the runrace method for each runner and calculate the time taken to run the race.
For a marathon race: We call the runrace method once for each runner for each kilometre of the marathon race. We also reduce the runner's energy according to the value set in the class. For this purpose, feel free to round up the distance to the nearest integer. If the runner runs out of energy, their finish time is set to DNFDid Not Finish
After the conclusion of the conductrace method, a list of tuples should be returned this code is already done for you
For You To Do
We definitely need to find better TAs because these ones keep making mistakes!
You are tasked with the following:
Copy and replace the code for runner.py that is given in the scaffold with your pristine code from the previous task.
Fix the errors that have been made in the class, variables, and methods based on the descriptions provided above. There may be cases where you need to remove some of the methods if you are implementing inheritance.
NOTE: You may need to moveremove some of the variables and methods in between the three classes once you implement proper inheritance. Please ensure that the names of the methodsvariables DO NOT CHANGE from the ones given to you.
Check the preconditions and postconditions for each of the methods. Make sure to raise the relevant errors if incorrect values are passed to the functions.
Document your code including typehinting for all variables and arguments for functions and methods, as well as include relevant docstrings for each method and class.
Build on the unit tests that have already been given to you in the file testraces.py You are expected to create at least three tests for each method. You
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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