Homework 7: Python Practice

After completing the homework, you will submit:

Hand in your work at this Google Form: https://goo.gl/forms/Fkr461qrsm27iZnP2

The Assignment

Planetary Election

An election is happening on Valeria’s planet of Saturn, and she needs help counting the votes (the hanging chads are causing an uproar in the election between Al Blood and George Shrub). Help her ensure a peaceful transition of power!

  1. Write a function name_matches that takes a vote (string) and a name 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 and 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 and 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 and a list of votes, and returns the name that got more votes (case insensitve). If there is a tie, return “Tie”.
  5. Write a function record_vote that takes a list of votes and a new vote and adds the vote to the list. This should modify the input list, and not return anything.
  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. Assume that there is always at least one vote in the list, and you should return the percent in decimal form (i.e. if the name matches 90% of the votes, return 0.9).
  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.

Remember that you need to put from testlight import * in your file to get the testing function from class. There are instructions on getting the testlight file into the proper directory in the Youtube video below; as usual, any problems and just let the TAs know.