Question
What to do At a high-level, your next steps are to: Open the file bridge_functions.py. Make sure that the file is in the same directory
What to do
At a high-level, your next steps are to:
- Open the file bridge_functions.py.
- Make sure that the file is in the same directory as a2_checker.py, and pyta.
- Complete the function definitions in bridge_functions.py.
- Test your Python file by using the Python shell, running the doctest examples, and running the a2_checker.py.
Unlike Assignment 1, we have provided you with the function headers, docstring description, and doctest examples for the functions you need to implement; you do not need to add or change them. The focus of Assignment 2 is implementing the bodies of these functions and testing them.
This assignment is divided into four parts. In parts 1 to 3, you will implement functions and test them using sample data that is already in bridge_functions.py. In part 4, you will implement functions that allow us to clean the data from the original dataset files so that we can use them in Python. Once you are done part 4, you will be able to test your functions from parts 1 to 3 with real data!
Starter Files
Please download the Assignment 2 Files and extract the zip archive. After you extract the zip file, you should see a similar directory structure to:
a2/ pyta/ many pyta files... a2_checker.py bridge_functions.py bridge_data_small.csv bridge_data_large.csv
In total, we have provided you with four files and a directory called pyta. We briefly discussed bridge_data_small.csv and bridge_data_large.csv above. The other two files are Python files:
- bridge_functions.py
This is the file where you will write your solution. Your job is to complete the file by implementing all the required functions. See below for more details.
- a2_checker.py
This is a checker program that you should use to check your code. You will not modify this file. See below for more information about a2_checker.py.
Part 1
In this part, you will focus on working with the data by searching through it. You should not mutate any of the list inputs to these functions. You should refer to the section Indexing with Constants for help.
- find_bridge_by_id(List[list], int) -> list
Notes:
- This function will be very useful in the rest of your program to get the data about one specific bridge. You should complete it first, then practice using it when you need a specific bridge in the other functions.
- You must implement the body of this function according to its description.
- You must not mutate the list argument(s).
- find_bridges_in_radius(List[list], float, float, int, List[int]) -> List[int]
Notes:
- This function helps us find all the bridges within a certain area. It becomes very useful when you reach Part 3.
- You must implement the body of this function according to its description.
- You must not mutate the list argument(s).
- You should use the calculate_distance function in the body. See the Calculating Distance section for help.
- get_bridge_condition(List[list], int) -> float
Notes:
- You must implement the body of this function according to its description.
- You must not mutate the list argument(s).
- See the Storing BCI Scores section for help.
- calculate_average_condition(list, int, int) -> float
Notes:
- Be careful; the years are stored as strings, not integers, in our Python lists.
- You must implement the body of this function according to its description.
- You must not mutate the list argument(s).
- See the Storing BCI Scores section for help.
- Review the Week 6 PCRS module on Parallel Lists and Strings for help.
Part 2
In this part, you will focus on mutating the data. Notice how the wording of the docstring descriptions has changed (the descriptions do not begin with the word Return). Notice also that the return type for these functions is None, so nothing is returned.
- inspect_bridge(List[list], int, str, float) -> None
Notes:
- You must implement the body of this function according to its description.
- Remember that the years a bridge has been inspected are stored in decreasing order (note: You should be adding an element to this list, not changing the value of an existing element.). See the Storing BCI Scores section for help.
- Review the Week 6 PCRS modules on "List Methods" and Mutability and Aliasing for help.
- rehabilitate_bridge(List[list], List[int], str, bool) -> None
Notes:
- You must implement the body of this function according to its description.
- See the Fixing Bridges section for help.
- Review the Week 6 PCRS module on Mutability and Aliasing for help.
Part 3
In this part, you will implement an algorithm to help inspectors pick the sequence of bridges they should inspect. You will do this in two parts. First, by implementing a function that finds the bridge (from a subset of bridges) that is in the worst condition. Second, by implementing a function that targets the worst bridge within a certain radius, then moving on to the next bridge, until the desired number of bridges have been inspected. These functions will take time - make sure you start early and, if you are stuck, visit us in office hours.
- find_worst_bci(List[list], List[int]) -> int
Notes:
- You must implement the body of this function according to its description.
- You must not mutate the list argument(s).
- See the Storing BCI Scores section for help.
- map_route(List[list], float, float, int, int) -> List[int]
Notes:
- You must implement the body of this function according to its description.
- You must not mutate the list argument(s).
- Hint: use a while loop
Part 4
In this part, we will finally start working with the real data files. The clean_data function is already implemented for you. However, it wont work correctly until you implement the functions below. Once you are done Part 4, then you can start loading the dataset files we have provided you. After that, test your functions using real data instead of just the docstring examples.
- clean_length_data(str) -> float
Notes:
- You must implement the body of this function according to its description.
- trim_from_end(list, int) -> None
Notes:
- You must implement the body of this function according to its description.
- clean_span_data(str) -> List[float]
Notes:
- You must implement the body of this function according to its description.
- See the Bridge Spans section for help.
- clean_bci_data(list) -> None
Notes:
- You must implement the body of this function according to its description.
- See the Storing BCI Scores section for help.
Testing Your Solutions
The last step in the Function Design Recipe is to test your function. You can use the a2_checker.py file to check for style and type contracts. You can use the doctest module to test the examples we have provided you in the docstrings. If you pass all of these tests, it does not mean that your function is 100% correct! You must do your own additional testing (e.g., by calling your functions with different arguments in the shell).
Using a2_checker.py
We are providing a checker module (a2_checker.py) that tests three things:
- Whether your code follows the Python style guidelines,
- Whether your functions are named correctly, have the correct number of parameters, and return the correct types
- Whether your functions are appropriately mutating their inputs (some functions should mutate, others should not)
To run the checker, open a2_checker.py and run it. Note: the checker file should be in the same directory as bridge_functions.py and pyta, as provided in the starter code zip file. After running the checker, be sure to scroll up to the top of the shell and read all the messages!
Using doctests
In this assignment, we have provided you with several doctest examples and sample data. These can be used as a quick test to see if your function works for a specific example. However, please note that being correct for one example does NOT mean your function is 100% correct. Be sure to test your code in other ways as with Assignment 1, our own tests that evaluate your solution are hidden.
A quick way to run all the doctest examples automatically is by importing the doctest module and calling one of its functions. We have already included the code to do this for you at the bottom of the starter file:
if __name__ == '__main__': # Automatically run all doctest examples to see if any fail import doctest doctest.testmod()
Step by Step Solution
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