CSC108H Assignment 2: Bike-Share

Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due


CSC108H Assignment 2: Bike-Share

Due Date: Tuesday, October 22nd before 4:00 pm

Goals of this Assignment

· Develop code that uses loops, conditionals, and other earlier course concepts.

· Practice with lists, including looping over lists, using list methods, and list mutation.

· Practice reading problem descriptions written in English, together with provided docstring examples, and implementing function bodies to solve the problems.

· Continue to use Python 3, Wing 101, provided starter code, a checker module, and other tools.

Bike-Share

Toronto's bike share networkLinks to an external site. debuted in 2011, offering rental bikes to Torontonians and visitors in the downtown core. This network consists of hundreds of docking stations scattered around downtown. Bikes can be rented from any docking station and returned to any docking station in the city. In this assignment, you will write several functions to help manage and track bike rentals across this network. Using real data from Toronto's bike share system, your functions will simulate bike rentals and returns as well as keep track of the current state of the network and even provide directions to riders.

The data that you will work with is provided by the Toronto bike share network. The data contains information about the docking stations, such as the location of the station and how many bikes are currently available. More information about the data provided is given later in this handout.

The purpose of this assignment is to give you practice using the programming concepts that you have seen in the course so far, including (but not limited to) strings, lists and list methods, and loops.

The problems that you are to solve and the tasks you need to complete for the assignment are explained in this handout. Please read it carefully.

Files to Download

Please click here Download click hereto download the Assignment 2 Starter Files and then extract the files in the zip archive. A description of each of the files that we have provided is given in the paragraphs below:

Starter code: bike_share.py

The bike_share.py file contains some constants, and three complete helper functions that you may use. You must not modify the provided constants or the helper functions.

The bike_share.py file also contains function headers and docstrings for the Assignment 2 functions for which you are required to add function bodies. For each function, read the header and docstring (especially the examples) to learn what task the function performs. Doing so may 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 more examples to the docstrings.

Data: stations.csv

The stations.csv file contains bike share data in Comma-Separated Values (CSV) format. See below for detailed information on the file 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 and CSC108 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 stations.csv. Each row of this file contains the following information about a single bike rental station, in the order listed:

· station ID: the unique identification (ID) number of the station

· station name: the name of the station (not necessarily unique)

· 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

· latitude: the latitude of the station location

· longitude: the longitude of the station location

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 rental through an app. Stations that are app-only (that is, that do not have a kiosk) have the string 'SMART' somewhere in their station name. (The bike share system could later change 'SMART' to some other phrase, so, when coding, use the constant NO_KIOSK that we provide instead of the string 'SMART'.)

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 bike_share.py for example usage.

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.

The functions that you complete will fall into three categories: a function for data conversion, functions for data queries, and functions for data modification.

Data conversion

As mentioned earlier, we have provided a function named csv_to_list that reads data from a CSV file and returns it in a list[list[str]]. Here is an example of the type of list returned by csv_to_list:

[['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 convert_data, which should make modifications to the inner lists according to the following rules:

· If and only if a string represents a whole number (ex: '3' or '3.0'), convert the string to an int.

· If and only if a string represents a number that is not a whole number (ex: '3.14'), convert the string to a float.

· Otherwise, leave the string as a str.

After applying the convert_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 body of the convert_data function, please note:

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

· this function is one of the more challenging functions in Assignment 2, because it mutates a list. We suggest that you start with some of the other functions, and come back to this one later.

Data conversion function to implement in bike_share.py

Function name:
(Parameter types) -> Return type

Full Description (paraphrase to get a proper docstring description)

convert_data
(list[list[str]]) -> None

The parameter represents a list of list of strings. The list could have the format of stations data, but it should not be assumed that the list has this format. That is, don't assume that, for example, the first string in an inner list represents a whole number. See the starter code docstring for some examples.

Modify the given 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 to be left as is.

HINT: The provided helper function is_number may be used.

Data queries

Once the data has been converted, you can use the following functions to extract information from the data.

You can work on these functions even before you have completed the convert_data function by working with the provided sample data in the starter code (see SAMPLE_STATIONS or HANDOUT_STATIONS), and by creating your own small lists of converted 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 converted list that represents a single station. That is, you should interpret "Station" in a type contract as: list[int, str, int, int, int, float, float]

List of data query functions to implement in bike_share.py.

Function name:
(Parameter types) -> Return type

Full Description (paraphrase to get a proper docstring description)

has_kiosk
("Station") -> bool

The parameter represents converted 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 without a kiosk contains the string that the constant NO_KIOSK refers to as part of its station name.

get_station_info
(int, list["Station"]) -> list

The first parameter represents a station ID number and the second parameter represents converted stations data.

The function should return a list containing the station name, the number of bikes available, the number of docks available, and whether or not the station has a kiosk (in this order), for the station with the given station ID.

get_column_sum
(int, list["Station"]) -> int

The first parameter represents an inner list index and the second parameter represents converted stations data.

The function should return the sum of the values at the given index from each inner list of the converted 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_kiosks
(list["Station"]) -> list[int]

The parameter represents converted stations data.

The function should return a list of the station IDs of stations that have kiosks. The station IDs should appear in the same order as in the given stations data list.

get_nearest_station
(float, float, list["Station"]) -> int

The first and second parameter represent the latitude and longitude of a location, and the third parameter represents converted stations data.

The function should return the ID of the nearest station. In the case of a tie, the function should return the ID of the nearest station that appears last in the given list of converted stations.

You can assume there is at least one station in the given data list.

HINT: The provided helper function get_lat_lon_distance may be used.

Data modification

The functions that we have described up to this point have allowed us to convert 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.

List of data modification functions to implement in bike_share.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 converted 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.

Precondition: the station ID will appear in the converted stations data.

return_bike
(int, list["Station"]) -> bool

The first parameter represents a station ID and the second represents converted stations data.

A bike can be returned to a station if and only if there is at least one dock available at that station. If the condition is met, this function successfully returns a single bike to the station. A successful bike return requires updating the bikes available count and the docks available count as if a single bike was added to the station. Return True if the bike return is successful, and False otherwise.

Precondition: the station ID will appear in the converted stations data.

upgrade_stations
(int, int, list["Station"]) -> int

The first parameter represents a capacity threshold. The second parameter represents the number of bikes to add to low capacity stations. The third parameter represents converted stations data.

Modify the converted stations data by adding the given number of bikes and docks to each station that has a capacity that is less than the given capacity threshold. Each bike that is added comes with a new dock so this increases each modified station's bikes available and capacity. Notice that it will not change the docks available count which represents the number of empty docks. Modify each station at most once.

The function should return the total of the number of new bikes that were added to the bike share network.

Precondition: the number of bikes to add will not be negative.

Using Constants

As in Assignment 1, your code should make use of the provided constants for the indexes in your stations data. This will not only make your code easier to read, but if the columns in the data were moved around by the evil Data Mangler, your code would still work after a small update to the constants.

Additional requirements

· Do not add statements that call print, input, or open, or use an import statement.

· Do not use any break or continue statements. We are imposing this restriction (and we have not even taught you these statements) because they are very easy to abuse, resulting in terrible, hard to read code.

· Do not modify or add to the import statements provided in the starter code.

Testing your Code

We strongly recommend that you test each function as you write it. As usual, follow the Function Design Recipe (we've done the first couple of steps for you). Once you've implemented a function, run it on the examples in the docstring, as well as some other examples that you come up with, to convince yourself that the function body works correctly.

Here are a few tips:

· Be careful that you test the right thing. Some functions return values; others modify the 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? Test each function carefully.

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

Remember to run the Assignment 2 checker that is described below!

Marking

These are the aspects of your work that may be marked for Assignment 2:

· Coding style (20%):

o Make sure that you follow Python style guidelines that we have introduced and the Python coding conventions that we have been using throughout the semester. Although we don't provide an exhaustive list of style rules, the checker tests for style are complete, so if your code passes the checker, then it will earn full marks for coding style with one exception: docstrings for any helper functions you add may be evaluated separately. For each occurrence of a PyTA error, one mark (out of 20) deduction will be applied. For example, if a C0301 (line-too-long) error occurs 3 times, then 3 marks will be deducted.

o All functions you design and write on your own (helper functions), should have complete docstrings including preconditions when you think they are necessary.

· Correctness (80%):

o Your functions should perform as specified. Correctness, as measured by our tests, will count for the largest single portion of your marks. Once your assignment is submitted, we will run additional tests not provided in the checker. Passing the checker does not mean that your code will earn full marks for correctness.

How should you test whether your code works

First, run the checker and review ALL output — you may need to scroll. Remember that the checker ONLY shows you style feedback, and that your functions take and return the correct types. Passing the checker does not tell you anything about the correctness of your code.

Assignment 2 Checker

We are providing a checker module (a2_checker.py) that tests two things:

· whether your code follows the Python style guidelines, and

· whether your functions are named correctly, have the correct number of parameters, and return the correct types.

To run the checker, open a2_checker.py and run it. Note: the checker file should be in the same directory as your bike_share.py, as provided in the starter code zip file. When you run your own checker, be sure to scroll up to the top and read all messages.

If the checker passes for both style and types:

· Your code follows the style guidelines.

· Your function names, number of parameters, and return types match the assignment specification. This does not mean that your code works correctly in all situations. We will run a different set of tests on your code once you hand it in, so be sure to thoroughly test your code yourself before submitting.

If the checker fails, carefully read the message provided:

· It may have failed because your code did not follow the style guidelines. Review the error description(s) and fix the code style. Please see the PyTA documentationLinks to an external site. for more information about errors.

· It may have failed because:

o you are missing one or more function,

o one or more of your functions is misnamed,

o one or more of your functions has the incorrect number or type of parameters, or

o one of more of your function return types does not match the assignment specification, or

o your .py file is misnamed or in the wrong place.

Read the error message to identify the problematic function, review the function specification in the handout, and fix your code.

Make sure the checker passes before submitting.

Running the checker program on Markus

In addition to running the checker program on your own computer, run the checker on MarkUs. You will be able to run the checker program on MarkUs up to 8 times every 24 hours. This can help to identify issues such as uploading the incorrect file.

First, submit your work on MarkUs. Next, click on the "Automated Testing" tab and then click on "Run Tests". Wait for a minute or so, then refresh the webpage. Once the tests have finished running, you'll see results for the Style Checker and Type Checker components of the checker program. Follow the same steps that you use on MarkUs Perform exercises. Note that these are not actually marks -- just the checker results. This is the same checker that we have provided to you in the starter code. If there are errors, edit your code, run the checker program again on your own machine to check that the problems are resolved, resubmit your assignment on MarkUs, and rerun the checker on MarkUs.

No Remark Requests

No remark requests will be accepted. A syntax error could result in a grade of 0 on the assignment. Before the deadline, you are responsible for running your code and the checker program to identify and resolve any errors that will prevent our tests from running.

What to Hand In

The very last thing you do before submitting should be to run the checker program one last time.

Otherwise, you could make a small error in your final changes before submitting that causes your code to receive zero for correctness.

Submit bike_share.py on MarkUs by following the familiar instructions from MarkUs Perform exercises. Remember that spelling of filenames, including case, counts: your file must be named exactly as above.




发表评论

电子邮件地址不会被公开。 必填项已用*标注