CSE 101: Computer Science Principles
Stony Brook University
Homework Assignment
Learning Outcomes
After completion of this homework assignment, you should be able to:
- Design and implement an algorithm that uses one or more dictionaries to solve a problem.
- Design and implement an algorithm that requires string processing.
- Design and implement an algorithm that involves recursion.
Getting Started
From Blackboard download the fileshomework4.py andhomework4tester.pyonto your computer.
Copy the files into your PyCharm project folder. The two files should automatically be found by PyCharm when you start the program. At the top ofhomework4.pyfill in the following information:
- your first and last name as they appear in Blackboard
- your Net ID (e.g., jsmith)
- your Stony Brook ID # (e.g., 111999999)
Do not, under any circumstances, change the names of the functions or their argument lists. The automated grading software will be looking for exactly those functions provided inhomework4.py.
To test your work, run the providedhomework4tester.pyfile. Note that different test cases will be used during grading.
Submit your finalhomework4.pyfile to Blackboard by the due date and time. Late work will not be graded.
Code that crashes and cannot be graded will earn no credit.
About This Assignment
Since we are all hunkered down while we wait for COVID-19 to run its course, why not work on an assignment about the topic?
You will be working with CSV files based on COVID-19 infection data taken from github.com/CSSEGISandData/COVID-19. A CSV file organizes each “data point” from a data-set on a single line, with the individual variables of the data point ascomma-separated valueson that line. For example here is a line of data from03-25-2020.csvabout COVID-19 deaths and recoveries in Dutchess County, NY:
Dutchess,New York,153,1,0,
These values represent, respectively:
- a region from a state
- the state in question
- number of total confirmed cases of COVID-19 in the region
- number of deaths from COVID-19 in the region
- number of recoveries from COVID-19 in the region
- number of active cases of COVID-19 in the region
Part I: Build a Dictionary of State Regions (20 points)
Complete the functionbuild_dict_regions_by_state(), which takes one argument:
- filename: the name of a CSV file of COVID-19 data
The file is formatted as follows:
Region,State,Cases,Deaths,Recovered,Active
Hampshire,West Virginia,0,0,0,
Walton,Georgia,1,0,0,
Anderson,Kentucky,2,1,0,
… etc. …
The first line of our CSV files always contains the meaning of each value per line. The number of lines can vary from one CSV file to the next. Note that the values are always separated by commas. Using data from the file, the function builds and returns a dictionary that maps each state’s name to a list of regions in that state.
After opening a file with theopen()function, getting its contents line-by-line is as simple as writing a for-loop:
for line in …:
do something with “line”, such as line.split(’,’)
where the…is the variable that points to the file opened viaopen().
For the file03-25-2020.csv, a portion of the resulting dictionary is shown below. Note that the order of keys (state names, for us) in a dictionary is irrelevant.
{
’West Virginia’: [’Hampshire’, ’Ohio’, ’Jefferson’, ’Monongalia’, …],
’Georgia’: [’Walton’, ’Jefferson’, ’Fulton’, ’Grady’, …],
’Kentucky’: [’Anderson’, ’Todd’, ’Hickman’, ’Clay’, …],
’Colorado’: [’Ouray’, ’Archuleta’, ’Clear Creek’, ’Costilla’, …],
…
}
Part II: Build a Dictionary that Maps a State’s Name to Infection Data (20 points)
Complete the functionbuild_dict_stats_by_state(), which takes two arguments:
- filename: the name of a CSV file of COVID-19 data
- stat: the data value we want to associate with each state. Valid values are “cases”, “deaths”, “recovered”
and “active”, which may appear in any case. For example, “Cases”, “CASES”, “caSES”, etc. would all be
valid.
The function opens the CSV file and processes it to build a dictionary that maps each state’s name to its total of all statvalues in that state’s regions. For example,build_dict_stats_by_state(’03-22-2020.csv’,
’Cases’)would build a dictionary that maps each state’s name to the total of the’cases’column for that state.
For example, for the file’03-22-2020.csv’andstats = ’cases’, the returned dictionary is given below:
{’New York’: 15800, ’Washington’: 1997, ’New Jersey’: 1914, ’California’: 1646,
’Illinois’: 1049, ’Michigan’: 1035, ’Florida’: 1004, ’Louisiana’: 837,
’Massachusetts’: 646, ’Texas’: 643, ’Georgia’: 621, ’Colorado’: 591,
’Pennsylvania’: 509, ’Tennessee’: 505, ’Wisconsin’: 381, ’Ohio’: 356,
’Connecticut’: 327, ’North Carolina’: 305, ’Maryland’: 244, ’Virginia’: 220,
… etc … }
Part III: Find the Region in a State with the Most Deaths (20 points)
Complete the functionget_region_max_deaths(), which takes two arguments:
- filename: the name of a CSV file of COVID-19 data
- state: the name of a state from the CSV file
The file processes the CSV file and returns the name of the region in the state namedstatethat reported the most deaths. Note that creating a dictionary is not necessary to solve this problem, but feel free to do so if it makes it easier for use to write the code.
1 | Function Arguments Return Value |
Part IV: Find the State with the Highest Death Rate (20 points)
Complete the functionhighest_death_rate_by_state(), which takes one argument:
- filename: the name of a CSV file of COVID-19 data
First, the function callsbuild_dict_stats_by_state()twice with appropriate arguments to generate two dictionaries:
- a dictionary that maps each state’s name to its number ofcases
- a dictionary that maps each state’s name to its number ofdeaths
The death rate for a given state can then be computed as:
1 | 100 ×deaths per state |
The function returns atuplecontaining the name of the state, followed by its death rate. For example, suppose you stored the name of that state inmax_stateand its death rate inmax_death_rate. Then, your function’s
return statement would be:
1 | return max_state, max_death_rate |
1 | Function Arguments Return Value |
Part V: And Now For Something Different: John’s Workout (10 points)
John wants to start a rigorous workout regimen to strengthen his arms and core. To do that, he decides to start doing push-ups everyday. He comes up with the following plan to increase his push-up count over time:
- For day 1, the push-up count is was it was yesterday. This value is given as the first argument to the function.
- Starting from day 2 until day 5, he doubles the push-up count each day.
- From day 6 until day 10, he increases the push-up count by 10 each day.
- From day 11 until day 20, he increases the push-up count by 8 each day.
- After 20 days, he just increases his push-up count by 5
Using loops (and not recursion), complete the functioniterative_num_of_pushups(), which takes two
arguments:
- cur_push_ups: the current number of push-ups that John has been doing (before day 2)
- days: the number of days John will exercise
For example, for the function calliterative_num_of_pushups(3,13), the returned value would be 122 because ((((3×2)×2)×2)×2) + 10 + 10 + 10 + 10 + 10 + 8 + 8 + 8 = 122
1 | Function Arguments Return Value |
Part VI: John’s Workout Revisited (10 points)
Re-implement your function from Part V of the assignment, this time using only recursion and no loops. The recursive function is to be calledrecursive_num_of_pushups(). The function takes the same arguments
(cur_push_upsanddays) and increases the number of push-ups each day according to the same scheme.
The general structure of the function will be a multi-way if-statement with function calls to recursive_num_of_pushups(). Naturally, with each recursive call, the argumentdaymust be decremented (decreased by 1). The base case that terminates the recursion is reached whendaysequals 1.
1 | Function Arguments Return Value |
How to Submit Your Work for Grading
To submit yourhomework4.pyfile for grading:
- Login to Blackboard and locate the section named “Assignments”.
- Click on the link for this assignment.
- Click the “Browse My Computer” button and locate thehomework4.pyfile. Submit only that one.py file.
- Click the “Submit” button to submit your work for grading.
Oops, I messed up and I need to resubmit a file!
No worries! Just follow the above directions again. We will grade only your last submission.