Homework 8: Python Practice with Detective Pikachu

Due: Tuesday November 12, 2019 at 9:00PM EST.

Handin

After completing the homework, you will submit:

Because all we need are the files, you do not need to submit the whole project folder. As long as the file you create has the same name as what needs to be submitted, you’re good to go!

If you are using late days, make sure to make a note of that in your readme. Remember, you may only use a maximum of 3 late days per assignment. If the assignment is late (and you do NOT have anymore late days) no credit will be given.

Helpful Things

Quick Syntax

Rounding

You can round a number in Python using the round function. The round function has two arguments, the first being the number to round and the second representing how many decimal places to have. Here are a few examples:

# If you'd like to round to the nearest whole number
# Note: this will still produce a decimal not an Int

round(1.3446, 0)     # produces 1.0
round(1.5111, 0)     # produces 2.0
round(1.7934, 0)     # produces 2.0

# If you'd like to round to the nearest tenth

round(1.3446, 1)     # produces 1.3
round(1.5111, 1)     # produces 1.5
round(1.7934, 1)     # produces 1.8

# If you'd like to round to the nearest hundreth

round(1.3446, 2)     # produces 1.34
round(1.5111, 2)     # produces 1.51
round(1.7934, 2)     # produces 1.79
round(1.799, 2)      # produces 1.8

Documentation

The Assignment

Detective Pikachu’s Election

Write your solution for this file in election.py. Check out this how to get started document on how to create files and write simple scripts.

An election is happening in the Kanto region, and Detective Pikachu needs help counting the votes (Bulbasaur and Charmander are making a mess in the election between Al Blood and George Shrub). Help Detective Pikachu ensure a peaceful transition of power!

  1. Write a function name_matches that takes a vote (string) and a name (string) and returns a boolean indicating whether the vote is for the given name, regardless of how the vote and name have been capitalized (that is, ignore case). You can lowercase a string s in Python by writing s.lower() (where s gets replaced by the string you want to lowercase). As in Pyret, you have Boolean operators and, or, and not.
  2. Write a function any_votes_for that takes a name (string) and a votes, a list of votes, and returns a boolean indicating whether any votes were for the given name (case insensitive).
  3. Write a function count_votes_for that takes a name (string) and a votes, a list of votes, and returns the number of votes that were for the given name (case insensitve).
  4. Write a function got_more_votes that takes two names, name_one and name_two, and votes, a list of votes, and returns the name that got more votes (case insensitve). If there is a tie, return the string “Tie”.
  5. Write a function record_vote that takes in votes, a list of votes, and new_vote, a new vote, and adds the vote to the end of the list.
  6. Write a function percentage_votes that takes a name and a list of votes and returns the percentage of votes that match (case insensitve) the given name. You should return the percent in decimal form (i.e. if the name matches 90% of the votes, return 0.9). Please round to the nearest tenth. Don’t forget to consider the case where someone has no votes (or where there are no votes)!
  7. Write a function test_votes to test your vote functions. In particular, think about how to test record_vote. Run test_votes() to make sure all your tests pass and your functions work as expected.

Any time you are returning a name, it may be returned lowercased or uppercased. Our test suite is case insensitive, so returning "Jennifer" will be treated the same as "jennifer" and "JENNIFER". Same applies for functions that return lists. ["Jen","Jennifer"] will be treated the same as ["jen","JENNIFER"].