You need to sign in or sign up before continuing.
voting.py 2.11 KB
import math


def process_votes() -> tuple[dict[str, list[str]], str | None]:
    """
    Takes in votes from the voters, and processes the winner.

    Returns:
        dict[str, list[str]]: A dictionary where the keys are the possible
        options someone could vote for. The value associated with each option
        is a list of people who voted for that option.
    """

    vote_tracker: dict[str, list[str]] = {}

    done: bool = False
    while ...:
        name: str = input("What is your name? ")
        choice: str = input("Hi there " + name + ". Which ice cream flavor is your favorite? ")
        ...
        more_votes: str = input("Your vote has been registered. Are there more votes to process? Y/N ")
        if more_votes == "N":
            done = True 

    #Determine the winning option
    max_votes = ...
    winning_option = ...
    
    for option in vote_tracker:
        num_votes = ...
        if ...:
            max_votes = ...
            winning_option = ...

    return (vote_tracker, winning_option)

# results = process_votes()
# print(votes[0])
# print(votes[1])

def highest_vote_count(vote_tracker: dict[str, list[str]]) -> str | None:
    """
    Identifies the person who voted the most times across all options.

    Args:
        vote_tracker (dict[str, list[str]]): A dictionary where the keys are
        the possible options someone could vote for. The value associated
        with each option is a list of people who voted for that option.

    Returns:
        str: The name of the person who appears most frequently across all
        voting lists. If there's a tie, one of the top voters is returned
        arbitrarily.
    """

    vote_counter: dict[str, int] = {}

    for voters in vote_tracker.values():
        for person in voters:
            if person in vote_counter:
                vote_counter[person] += 1
            else:
                vote_counter[person] = 1

    max_votes = -math.inf
    top_voter = None

    for person in vote_counter:
        if vote_counter[person] > max_votes:
            max_votes = vote_counter[person]
            top_voter = person

    return top_voter