Skip to content

Instantly share code, notes, and snippets.

@timkofu
Last active September 23, 2018 03:08
Show Gist options
  • Save timkofu/05f5ad8ba6a80424f2007b4e7a8e25c8 to your computer and use it in GitHub Desktop.
Save timkofu/05f5ad8ba6a80424f2007b4e7a8e25c8 to your computer and use it in GitHub Desktop.
import sys
import math
import random
import collections
def democracy(number_of_candidates):
votes = []
cntr = collections.Counter()
for _ in range(200, 3000): # roll it
votes.append(random.randint(1,number_of_candidates))
for x in votes:
cntr[x] += 1
# flip the keys and values, then we can get the candicate with the highest vote
cntr = {v:k for k,v in cntr.items()}
most_votes = sorted(cntr.keys())[-1] # Sometimes the max vote has multiple candidates.
# First one wins.
return cntr[most_votes],most_votes
if __name__ == "__main__":
if len(sys.argv) == 2:
try:
print("Candidate {0} won with {1} votes 🎉".format(*democracy(int(math.floor(float(sys.argv[1]))))))
except ValueError:
print("That wasn't a number :/")
else:
print(f"Usage: {__file__} number_of_voters[integer]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment