Python代写 | CSC108 Assignment 2: Rent-a-Bike

本次Python代写是完成自行车共享平台

CSC108 Assignment 2: Rent-a-Bike

Rent-a-Bike
The Data
For this assignment, you will use data from a Comma Separated Value (CSV) file named stations.csv. Each row of this file contains the following information about a single bike rental station:
• station ID: the unique identification (ID) number of the station
• name: the name of the station (not necessarily unique)
• latitude: the latitude of the station location
• longitude: the longitude of the station location
• capacity: the total number of bike docks (empty or with bike) at the station
• bikes available: the number of bikes currently available to rent at the station
• docks available: the number of empty and working docks at the station
Note: While the sum of the number of bikes available at a station and the number of docks available at a station will usually equal the station’s capacity, this need not be the case. When a bike or a dock is broken, the sum of the two availability numbers will not match the capacity.
Another feature of a bike rental station is whether or not it has a kiosk. A kiosk allows a renter to pay for their bike rental using a credit card. Without a kiosk, renters can only pay for their bike through an app. Stations that are app-only (that is, that do not have a kiosk) have SMART somewhere in their name.
We have provided a function named csv_to_list, which reads a CSV file and returns its contents as a List[List[str]]. As you develop your program, you can use the csv_to_list function to produce a larger data set for testing your code. See the main block at the end of bikes.py for an example.
Your Tasks
Imagine that it is your job to manage Toronto’s bike share system. As the manager, you need to know everything about the system. But, there are hundreds of docking stations, 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 system.
Your functions will fall into three categories: functions for data cleaning, functions for data queries, and functions for data modification.
Data cleaning
We provided a function named csv_to_list 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:
[[‘7000’, ‘Ft. York / Capreol Crt.’, ‘43.639832’, ‘-79.395954′, ’31’, ’20’, ’11’],
[‘7001’, ‘Lower Jarvis St SMART / The Esplanade’, ‘43.647992’, ‘-79.370907′, ’15’, ‘5’, ’10’]]
Notice that all of the data in the inner lists are represented as strings. You are to write the function clean_data, which should make modifications to the list according to the following rules:
• If and only if a string represents a whole number (ex: ‘3’ or ‘3.0’), convert it to an int.
• If and only if a string represents a number that is not a whole number (ex: ‘3.14’), convert it to a float.
• Otherwise, leave it as a str.
After applying the clean_data function to the example list, it should look like this:
[[7000, ‘Ft. York / Capreol Crt.’, 43.639832, -79.395954, 31, 20, 11],
[7001, ‘Lower Jarvis St SMART / The Esplanade’, 43.647992, -79.370907, 15, 5, 10]]
Before you write the clean_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, because it mutates a list. We suggest that you start with some of the other functions, and come back to this one later.
Data cleaning function to implement in bikes.py
Function name:
(Parameter types) -> Return type Full Description (paraphrase to get a proper docstring description)
clean_data
(List[List[str]]) -> None The parameter represents a list of list of strings. The list could have the format of stations data, but is not required to. See the starter code docstring for some examples.
Modify the input list so that strings that represent whole numbers are converted to ints, and strings that represent numbers that are not whole numbers are converted to floats. Strings that do not represent numbers are left as is.
Data queries
Once the data has been cleaned, you can use the following functions to extract information from the data. All the examples shown below assume that you are calling the function on the cleaned example list shown above.
You can work on these functions even before you have completed the clean_data function by working with the provided sample data in the starter code, and by creating your own small lists of clean station data for testing. See the docstrings in the starter code for examples of how to work with that data.
We will use ‘Station’ in the type contracts to represent a cleaned list that represents a single station.
List of data query functions to implement in bikes.py.
Function name:
(Parameter types) -> Return type Full Description (paraphrase to get a proper docstring description)
has_kiosk
(‘Station’) -> bool The parameter represents cleaned station data for a single station.
The function should return True if and only if the given station has a kiosk. Recall that a station has a kiosk if and only if SMART is not part of its name.
get_station_info
(int, List[‘Station’]) -> list The first parameter represents a station ID number and the second parameter represents cleaned stations data.
The function should return a list containing the name, the number of bikes available, the number of docks available, and if the station has a kiosk (in this order), for the station with the given station ID.
get_total
(int, List[‘Station’]) -> int The first parameter represents an index and the second parameter represents cleaned stations data.
The function should return the sum of the values at the given index in each inner list of the cleaned stations data.
You can assume that the given index is valid for the given data, and that the items in the list at the given index position are integers.
get_stations_with_n_docks
(int, List[‘Station’]) -> List[int] The first parameter represents a required minimum number of available docks and the second parameter represents cleaned stations data.
The function should return a list of the station IDs of stations that have at least the required minimum number of available docks . The station IDs should appear in the same order as in the given stations data list.
You can assume that the given number is non-negative.
get_nearest_station
(float, float, bool, List[‘Station’]) -> int The first and second parameter represent the latitude and longitude of a location, the third parameter represents whether or not to search for a station with a kiosk, and the fourth parameter represents cleaned stations data.
If the third argument is True, the function should return the ID of the station that is nearest to the given lat/lon location that has a kiosk. If the third argument is False, the function should return the ID of the nearest station, which may or may not have a kiosk. In the case of a tie, the function should return the ID of a station that appears first in the given list.
You can assume there is at least one station in the input data list. Furthermore, you can assume that if the third argument in True, then there is at least one station with a kiosk in the given data.
Data modification
The functions that we have described up to this point have allowed us to clean our data and extract specific information from it. Now we will describe functions that let us change the data.
Notice that each of these functions mutates the given list. We recommend getting your rent_bike and return_bike functions working correctly first before attempting redistribute_bikes.
List of data modification functions to implement in bikes.py.
Function name
(Parameter types) -> Return type Full Description
rent_bike
(int, List[‘Station’]) -> bool The first parameter represents a station ID and the second represents cleaned stations data.

A bike can be rented from a station if and only if there is at least one bike available at that station. If the condition is met, this function successfully rents a single bike from the station. A successful bike rental requires updating the bikes available count and the docks available count as if a single bike was removed from the station. Return True if the bike rental is successful, and False otherwise.