From ff065e5db78d90f1c746d9777391f5ec364bfdce Mon Sep 17 00:00:00 2001 From: Jedidiah Tsang <jedidiahtsang@berkeley.edu> Date: Wed, 7 May 2025 11:50:20 -0700 Subject: [PATCH] Add lecture code --- 16/vote_counter.py | 41 +++++++++++++++++++++++++++++++++++++++++ manifest.json | 3 ++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 16/vote_counter.py diff --git a/16/vote_counter.py b/16/vote_counter.py new file mode 100644 index 0000000..093c06e --- /dev/null +++ b/16/vote_counter.py @@ -0,0 +1,41 @@ +import math +from collections import Counter + +def process_votes() -> tuple[Counter, str | None]: + """ + Takes in votes from the voters, and processes the winner. + + Returns: + tuple[Counter, str | None]: A Counter where the keys are the possible + options someone could vote for. The value associated with each option + is the number of people who voted for that option. + """ + + vote_tracker = {} + + #Solicit votes + done: bool = False + while not done: + choice: str = input("Hi there. Which ice cream flavor is your favorite? ").lower() + if choice not in vote_tracker: + vote_tracker[choice] = 0 + vote_tracker[choice] += 1 + more_votes: str = input("Your vote has been registered. Are there more votes to process? Y/N ").upper() + if more_votes == "N": + done = True + + #Determine the winning option + max_votes = -math.inf + winning_option = None + + for option in vote_tracker: + num_votes = vote_tracker[option] + if num_votes > max_votes: + max_votes = num_votes + winning_option = option + + return (vote_tracker, winning_option) + +results = process_votes() +if results[1] != None: + print("The winning option is...", results[1] + "!!!!!") diff --git a/manifest.json b/manifest.json index a5ee44a..e6eb8a1 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,8 @@ "00/experimenting.py", "00/main.py", "12/physics.py", - "13/monty.py" + "13/monty.py", + "16/vote_counter.py" ], "ignore": [] } -- GitLab