Skip to content

Instantly share code, notes, and snippets.

@guru-beach
Created December 27, 2016 20:06
Show Gist options
  • Save guru-beach/b3008759aa246ade479022d6943482ef to your computer and use it in GitHub Desktop.
Save guru-beach/b3008759aa246ade479022d6943482ef to your computer and use it in GitHub Desktop.
How I went about picking a winner for a contest
import random
contestants = ['mitch', 'dave', 'michael', 'james b', 'james g', 'robyn', 'cole', 'richard', 'tom', 'lonnie']
def choose_winner(contestants, cycles):
max_count = 0
max_person = 'jake'
random.seed()
counts = dict(zip(contestants,[0]*len(contestants)))
for i in range(cycles):
person = random.choice(contestants)
counts[person] += 1
if counts[person] > max_count:
max_count = counts[person]
max_person = person
stacked_list = sorted(counts, key=lambda i: counts[i], reverse=True)
print "Winner: {}".format(max_person)
print "Count: {}".format(max_count)
for entry in stacked_list:
print "{:4d}: {}".format(counts[entry], entry)
if __name__ == "__main__":
choose_winner(contestants, 2016)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment