CSC108H-Python代写-Assignment 2
时间:2022-03-14
2022/3/14 上午1:39Assignment 2
⻚码:1/10https://q.utoronto.ca/courses/249645/assignments/821773
Assignment 2
Due Thursday by 5pm Points 10
CSC108H Assignment 2: Bike-Share
Due Date: Thursday, March 17, 2022 before 5:00pm
This assignment must be completed alone (no partners). Please see the syllabus for information
about academic offenses.
Late policy: There are penalties for submitting the assignment after the due date. These penalties
depend on how late your submission is. Please see the Syllabus for more information.
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 network (https://en.wikipedia.org/wiki/Bike_Share_Toronto) 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.
2022/3/14 上午1:39Assignment 2
⻚码:2/10https://q.utoronto.ca/courses/249645/assignments/821773
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 (https://q.utoronto.ca/courses/249645/files/19668675/download?download_frd=1)
to 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 a couple of complete helper functions that
you may use. You must not modify the provided constants or 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)
2022/3/14 上午1:39Assignment 2
⻚码:3/10https://q.utoronto.ca/courses/249645/assignments/821773
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
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:
2022/3/14 上午1:39Assignment 2
⻚码:4/10https://q.utoronto.ca/courses/249645/assignments/821773
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 int s, and strings that represent numbers that are not whole
numbers are converted to float s. 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
2022/3/14 上午1:39Assignment 2
⻚码:5/10https://q.utoronto.ca/courses/249645/assignments/821773
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, float, float,
int, int, int]
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)
is_app_only
("Station") -> bool
The parameter represents converted station data for a single station.
The function should return True if and only if the given station
requires the use of an app because it does NOT have a kiosk. Recall
that a station requires the use of an app if and only if the string that
the constant NO_KIOSK refers to is 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"]) ->
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
2022/3/14 上午1:39Assignment 2
⻚码:6/10https://q.utoronto.ca/courses/249645/assignments/821773
(List["Station"]) ->
List[int] 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.
2022/3/14 上午1:39Assignment 2
⻚码:7/10https://q.utoronto.ca/courses/249645/assignments/821773
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 to each station that has a capacity that is less than the
given capacity threshold. Increase each modified station's
bikes available and capacity accordingly. 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
2022/3/14 上午1:39Assignment 2
⻚码:8/10https://q.utoronto.ca/courses/249645/assignments/821773
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 recommended 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%):
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.
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%):
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
2022/3/14 上午1:39Assignment 2
⻚码:9/10https://q.utoronto.ca/courses/249645/assignments/821773
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 documentation
(http://www.cs.toronto.edu/~david/pyta/) for more information about errors.
It may have failed because:
you are missing one or more function,
one or more of your functions is misnamed,
one or more of your functions has the incorrect number or type of parameters, or
one of more of your function return types does not match the assignment specification, or
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.
2022/3/14 上午1:39Assignment 2
⻚码:10/10https://q.utoronto.ca/courses/249645/assignments/821773
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 as
well. You will be able to run the checker program on MarkUs once every 12 hours (note: we may
have to revert to every 24 hours if MarkUs has any issues handling every 12 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
(if time permits) after the 12 hour period has elapsed, 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.
essay、essay代写