Friday April 14th, 2023 at 11:59PM ET.
You should have VSCode set up from the first week of the course. In case you don't, refer to the VSCode Installation and Setup Guide
hw6_code.py
to include your code.test_hw6.py
and put the following lines at the top of that file:All of the tests that you write should be written in test_hw6.py
as assert
expressions in functions that start with test_
.
The goal of this assignment to give you practice with the basics of writing list-processing programs in Python. It mostly follows what we've done in class, while also showing a couple of useful Python built-in functions. Everything you need for this will have been covered by the end of class on Friday, April 7.
Make sure to only use Python concepts we've talked about in lecture when solving these problems. In particular, if you have other Python knowledge of other techniques, don't use them here if you want full credit.
In particular, don't use the built-in Python count
function. That defeats the point of the problems.
CS111 is conducting a vote for the best superhero. We're going to write some programs to tally votes. We'll represent a vote as a single string
containing the name of the person or thing that got voted for. We'll represent the collection of votes recorded by a single voting machine as a list
of votes (list of string
).
For every function we ask you to write, you should have a corresponding testing function in test_hw6.py
. Name each test function the same as the original function but with test_
prepended. In other words, the tests for a function check_votes
would be in a function called test_check_votes
.
Task 1: Write a function called name_matches
. It should take in a vote
(string) and a name
(string). It should return a boolean indicating whether the vote is for the given name, regardless of how either vote or the name have been capitalized.
Note: You can convert a string s
to lowercase in Python by writing s.lower()
. As in Pyret, you have Boolean operators and
, or
, and not
. Equality is checked using ==
(and inequality using !=
)
Task 2: In hw6_code.py
, write a function called any_votes_for
that takes in a name and a list of votes and returns a boolean
indicating whether any of the votes were for the given name (case insensitive). Your solution should use a for
loop.
Task 3: In hw6_code.py
, write a function count_votes_for
that takes in a name and a list of votes. It should return the number of votes that were for the given name (case insensitive).
Task 4: In hw6_code.py
, write a function called got_more_votes
that determines which of two names got more votes. Your function should take in two names and a list of votes. It should return the name that got more votes (case insensitive). If there is a tie, return the string “tie”
.
Task 5: In hw6_code.py
, write a function to record a new vote. Your function should be called record_vote
, and take in a list of votes, and a string representing a new vote. It should add the new vote to the end of the list of votes. The function should not return anything!
With people concerned about the validity of the election results, the decision has been made to run some checks against the votes to see if they appear plausible.
One issue is that write-in votes can be ambiguous. Imagine an election in which two people with the first name "Ruth" have been trying to get elected. Some votes have come through for "Ruth Achebe", some for "Ruth Flynn", and some for just "Ruth". The "Ruth" votes need to get handled for accurate results.
Someone has proposed counting "Ruth" votes that have a last name, using the presence of a space in the name string to indicate whether a vote has both a first and a last name.
Task 6: Write a function clean_votes
that takes a list of votes and returns a new list of votes containing only the votes from the original list that contain at least one space (represented by the string " "
). Your solution should use a for
loop.
Task 7: Write a test_clean_votes
function (in the test_hw6.py
file) that explores various scenarios in which this specific approach would and wouldn't behave as we expected. If you think there are situations that the "check for space" situation would handle poorly for a goal of proper vote counting, include a test case that illustrates the undesirable behavior (write your test to pass what clean_votes
actually does, but include a comment about why the result is undesirable.) In general, pay particular attention to the set of strings and vote lists that you include in your test lists. We'll be looking to see how well you explore the space of names and vote lists when grading this question.
Even with removing the single-name votes, people are still concerned that there might be weird issues in the votes. To help find them, they want to be able to sort the list of votes in different ways to make it easier to look for inconsistencies in spelling, etc.
In Python, sorting a list numerically or alphabetically is as easy as writing sorted(the_list)
(where the_list
) is the list you want to sort. If you instead want to provide your own criterion, such as the length of the name you use the following form:
where theList
is the list to sort and keyFun
is a function that takes a single item of the list and returns a value (like a number) that gets used for sorting.
sorted
and keyFun
Here's the code to sort a list of strings by the lengths of the strings. The order in the list is preserved for items of the same length.
(Note that since string length is an integer, sorted
defaults to the integer string sorting behavior of sorting the elements in increasing order)
Task 8: Write a function sort_phrase_len
that takes a list of strings (each of which may have spaces) and sorts the phrases in order of how many words they have (from fewest to most). This function should return the sorted list.
Note: Similar to Pyret, Python has a function split
that converts a string into a list of words around a separator character. For example,
Given prior trends in the district where the votes have been cast, there are expectations on what percentage of the vote specific candidates might get (within a margin of error). We'll write a program to check whether a candidate's votes are within such an expected range.
Task 9: In hw6_code.py
, write a function check_percent
that takes the name of a candidate, a list of votes, an expected percentage (decimal between 0.0-1.0, inclusive), and an error tolerance (decimal). The function returns one of "higher", "in-range", or "lower". It returns "in-range" if the percentage of votes that the candidate received is within the error tolerance (inclusive) of the expected percentage. Otherwise, "higher" or "lower" is returned based on where the actual percentage lands relative to the error tolerance. For example, if the expected percentage is 0.4 and the error tolerance is 0.03, the function would return "in-range"
if and only if the candidate got between 0.37 and 0.43 fraction of the vote.
Hint: This is an excellent problem on which to practice planning. We're not having you develop the plan explicitly, but we strongly suggest that you develop one on paper in case you need to get help from the TAs.
Voting is a topic for which people are reasonably concerned about representation, access, and accuracy (among other things). Not surprisingly, there are many arguments and proposals regarding how to properly use computing technology as part of elections: should we vote on computers so they can count ballots? allow voting online? use digital scanners to process votes marked on paper? And so on. Each option offers benefits and risks around participation in, and accuracy of, elections.
Task 10: For this question, go to the form in the HW6 - SRC
assignment in Gradescope. You'll be asked to answer the following questions:
hw6_code.py
file and run the code. If All required methods exist!
prints, you're good to go! If One of the required methods is missing or named incorrectly
prints, you are either missing a method, or have named one incorrectly.Remember to submit all of your work on Gradescope! The code files you should be submitting are:
hw6_code.py
Tasks to be submitted in hw6_code.py
:
Part 1
name_matches
functionany_votes_for
functioncount_votes_for
functiongot_more_votes
functionPart 2
record_vote
functionPart 3
clean_votes
functionsort_phrase_len
functioncheck_percent
functiontest_hw6.py
Tasks to be submitted in test_hw6.py
:
test_name_matches
functiontest_any_votes_for
functiontest_count_votes_for
functiontest_got_more_votes
functionPart 2
test_record_vote
functionPart 2
test_clean_votes
functiontest_sort_phrase_len
functiontest_check_percent
functionhw6_code.py
. Hand in your work on Gradescope.test_hw6.py
. Hand in your work on Gradescope.Heroes by David Bowie
Brown University CSCI 0111 (Spring 2023)