Friday, February 24, 2017

Counting the winner in candidates with specific list of votes (Python)

def majority_vote(votes):
    """Used to calculate the vote with maximum frequency"""
    import random
    vote_counts={}
 
    for vote in votes:
        if vote in vote_counts:
            vote_counts[vote]+=1
        else:
            vote_counts[vote]=1
 
    max_count=max(vote_counts.values())
 
    winner=[]
 
    for vote, count in vote_counts.items():
        if count == max_count:
            winner.append(vote)
    return(random.choice(winner))

No comments:

Post a Comment