Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Starter code: bridge_functions.py The bridge_functions.py file contains some constants, and a couple of complete helper functions that you may use. You must not modify the

Starter code: bridge_functions.py

The bridge_functions.py file contains some constants, and a couple of complete helper functions that you may use. You must not modify the provided helper functions.

The bridge_functions.py file also contains function headers and docstrings for the A2 functions to which you are required to add function bodies.

Data: bridge_data.csv

The bridge_data.csv file contains bridge data in comma-separated values (CSV) format. You must not modify this file.

Checker: a2_checker.py

We have provided a checker program (a2_checker.py) that tests two things:

whether your functions have the correct parameter and return types, and

whether your code follows the Python style guidelines.

The checker program does not test the correctness of your functions, so you must do that yourself.

The Data

For this assignment, you will use data from a Comma Separated Value (CSV) file named bridge_data.csv. Each row of this file contains the following information about a single bridge:

bridge label: a label representing the bridge

bridge name: the name of the bridge

highway name: the name of the highway (not necessarily unique)

latitude: the latitude of the center of the bridge

longitude: the longitude of the center of the bridge

year built: the year the bridge was built

last major rehab: the year the last major rehabilitation was performed on the bridge

last minor rehab: the year the last minor rehabilitation was performed on the bridge

number of spans: the number of spans of the bridge

span details: the detail of each span.This is in the format:

Total=[total length of all spans] (1)=[the length of the first span];(2)=[the length of the second span];and so on for each span of the bridge.

length: the length of the bridge (this can differ from the total length of all spans)

last inspection date: the date that the bridge was last inspected (in MM/DD/YYYY format).

last inspected index: the bridge condition index (BCI) from the last inspection (Bridge condition indexes (BCI) are numeric scores representing the condition of a bridge. The lower the BCI, the worse the condition of the bridge.)

bridge condition indexes (BCIs): numeric scores representing the condition of the bridge over time

We have provided a function named read_data, which reads a CSV file and returns its contents as a List[List[str]]. As you develop your program, you can use the read_data function to produce a larger data set for testing your code. See the main block at the end of bridge_functions.py for an example.

Your Tasks

Imagine that it is your job to manage Ontario's highway network. As the manager, you need to know everything about the bridges. But, there are hundreds of highways and associated bridges, which is way too many to keep track of in your head. To make your life easier, you will write Python functions to help you manage the information.

Your functions will fall into three categories: functions for data formatting, functions for data queries, and functions for data modification. Here are the functions you are required to implement:

Data formatting functions:

format_data (do not start with this!)

Data query functions:

get_bridge

get_average_bci

get_total_length_on_highway

get_distance_between

find_closest_bridge

find_bridges_in_radius

get_bridges_with_bci_below

assign_inspectors

get_bridges_containing

Data modification functions:

inspect_bridges

add_rehab

For each function, read the header and docstring (especially the examples) in the starter code bridge_functions.pyto learn what task the function performs. Doing so will help you to determine what you need to do for each required function. To gain a better understanding of each function, you may want to add another example to the docstring.

For most functions, the description and examples provided in the starter code gives you enough information to get started. We have provided additional information about the format_data and assign_inspectors functions below.

Formatting the data (format_data)

We provided a function named read_data that reads data from a CSV file and returns it in a List[List[str]]. Here is a sample of the type of list returned (we added spacing here for readability that you won't see in Python):

[

['1 - 32/', 'Highway 24 Underpass at Highway 403', '403', '43.167233', '-80.275567', '1965', '2014', '2009', '4', 'Total=64 (1)=12;(2)=19;(3)=21;(4)=12;', '65', '04/13/2012', '72.3', '', '72.3', '', '69.5', '', '70', '', '70.3', '', '70.5', '', '70.7', '72.9', ''], ['1 - 43/', 'WEST STREET UNDERPASS', '403', '43.164531', '-80.251582', '1963', '2014', '2007', '4', 'Total=60.4 (1)=12.2;(2)=18;(3)=18;(4)=12.2;', '61', '04/13/2012', '71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '', '70.3', '73.3', ''], ['2 - 4/', 'STOKES RIVER BRIDGE', '6', '45.036739', '-81.33579', '1958', '2013', '', '1', 'Total=16 (1)=16;', '18.4', '08/28/2013', '85.1', '85.1', '', '67.8', '', '67.4', '', '69.2', '70', '70.5', '', '75.1', '', '90.1', '']

]

Notice that all of the data in the inner lists are represented as strings. You are to write the function format_data, which should make modifications to the list such that it follows this format:

Format of data to be returned by format_data for A2.
Index Data Type Description
0 ID int

The ID of the bridge.

The first bridge should have an ID of 1, the second should have an ID of 2, and so on. Your program must generate the ID numbers. Note: the original bridge labels are discarded.

1 Name str

The name of the bridge.

2 Highway str

The name of the bridge's highway.

3 Latitude float

The latitude of the bridge.

4 Longitude float

The longitude of the bridge.

5 Year Built str

The year the bridge was built.

If no year is provided, this will be an empty string.

6 Last Major Rehab str

The year of the last major rehab.

If no year is provided, this will be an empty string.

7 Last Minor Rehab str

The year of the last minor rehab.

If no year is provided, this will be an empty string.

8 Number of Spans int

The number of spans on the bridge.

A bridge will always have at least 1 span.

9 Span Details List[float]

The length of each of the spans of the bridge.

Example: If the Span Details in the unformatted data was:

Total=64 (1)=12;(2)=19;(3)=21;(4)=12;

And the bridge had 4 spans, then the formatted version should be the following list:

[12.0, 19.0, 21.0, 12.0]

10 Bridge Length float

The length of the bridge.

If no length is provided, this will be 0.0.

11 Last Inspected Date str

The date of the last inspection (in MM/DD/YYYY format).

If no date is provided, this will be an empty string.

12 BCIs List[float]

A list of the BCIs from the most recent to the least.

If the bridge has no BCIs, then this should be an empty list.

After applying the format_data function to the example list, it should look like:

[

[1, 'Highway 24 Underpass at Highway 403', '403', 43.167233, -80.275567, '1965', '2014', '2009', 4, [12.0, 19.0, 21.0, 12.0], 65.0, '04/13/2012', [72.3, 69.5, 70.0, 70.3, 70.5, 70.7, 72.9]], [2, 'WEST STREET UNDERPASS', '403', 43.164531, -80.251582, '1963', '2014', '2007', 4, [12.2, 18.0, 18.0, 12.2], 61.0, '04/13/2012', [71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]], [3, 'STOKES RIVER BRIDGE', '6', 45.036739, -81.33579, '1958', '2013', '', 1, [16.0], 18.4, '08/28/2013', [85.1, 67.8, 67.4, 69.2, 70.0, 70.5, 75.1, 90.1]]]

]

Before you write the format_data function body, please note:

you must not use the built-in function eval, and

this function is one of the more challenging functions in A2, so we suggest that you don't start with it.

Assigning Inspectors (function assign_inspectors)

Bridge condition indexes (BCI) represent the condition of a bridge on a past inspection, and we use the most recent BCI to prioritize certain bridges. There are three levels of priorities, with their upper limits represented by the constants HIGH_PRIORITY, MEDIUM_PRIORITY, LOW_PRIORITY, constrained by HIGH_PRIORITY < MEDIUM_PRIORITY < LOW_PRIORITY.

For example, if HIGH_PRIORITY is 60, then all bridges with their most recent BCI less than or equal to 60 are considered 'high priority'. Similarly, if MEDIUM_PRIORITY is 70, then all bridges with a BCI less than or equal to 70 (but > 60) are considered 'medium priority'.

When assigning bridges to inspectors, we want to prioritize nearby high priority bridges within a large radius of the inspector over medium priority bridges that are closer, and both of those are prioritized over low priority bridges in an even smaller radius.

The radiuses are specified by the constants HIGH_PRIORITY_RADIUS, MEDIUM_PRIORITY_RADIUS, and LOW_PRIORITY_RADIUS.

You are told the maximum number of bridges to assign per inspector. The way we want to assign bridges to inspectors is as follows:

High priority bridges with a distance <= HIGH_PRIORITY_RADIUS from the inspector.

If (1) assigned fewer than the maximum number of bridges, then we go on to assign medium priority bridges with a distance <= MEDIUM_PRIORITY_RADIUS from the inspector.

If (1) and (2) still assigned fewer than the given maximum number of bridges, then we go on to assign low priority bridges with a distance <= LOW_PRIORITY_RADIUS from the inspector.

One inspector at a time, we assign the maximum number of bridges to each inspector (or fewer than the maxiumum number of bridges, if all bridges have already been assigned). Inspectors should be assigned bridges based on the order they appear in the list (e.g., the first inspector in the list should be assigned up to the maximum number of bridges first, the second inspector should be assigned up to the maximum number of bridges next, and so on).

Bridges are assigned to an inspector using as many bridges that fulfill (1) as possible, followed by (2), and then (3) if there are still fewer than the maximum number of bridges assigned to that inspector. If there are multiple bridges with the same priority and radius, we choose the bridge with the lowest ID (e.g., if there are two low priority bridges with IDs 3 and 4, we would assign the bridge with ID 3 first). Each bridge should be assigned to at most one inspector.

You may want to use the find_bridges_in_radius and get_bridges_with_bci_below functions to help you with assigning inspectors.

Testing your Code

It is strongly recommended that you test each function as you write it. (You might be tempted to just plow ahead and write all of the functions and hope everything works, but then it will be really difficult to determine whether your program is working correctly.)

As usual, follow the Function Design Recipe. Once you've implemented a function, run it on the examples in the docstring. Here are a few tips:

Be careful that you test the right thing. Some functions return values; others modify data in-place. Be clear on what the functions are doing before determining whether your tests work.

Can you think of any special cases for your functions? Will each function always work, or are there special cases to consider? Test each function carefully.

Once you are happy with the behaviour of a function, move to the next function, implement it, and test it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MFDBS 91 3rd Symposium On Mathematical Fundamentals Of Database And Knowledge Base Systems Rostock Germany May 6 9 1991

Authors: Bernhard Thalheim ,Janos Demetrovics ,Hans-Detlef Gerhardt

1991st Edition

3540540091, 978-3540540090

More Books

Students also viewed these Databases questions