<div dir="ltr"><div><div><div><div><div><div><div><div><div><span style="font-family:monospace,monospace">"""<br></span></div><span style="font-family:monospace,monospace">The code below derives from a thread on <a href="mailto:elections-wg@python.org">elections-wg@python.org</a><br></span></div><span style="font-family:monospace,monospace">where we were talking about tie-breaking techniques.  There's a<br></span></div><span style="font-family:monospace,monospace">well known way that's actually used, but I was inspired to think</span><span style="font-family:monospace,monospace"> <br>about a game wherein the ballots are like playing cards in some<br></span></div></div><span style="font-family:monospace,monospace">ways, each encoded with a unique prime.  <br><br>I've got some notes in the moderator queue at math-teach (lots<br>of traffic), for which I'll share the URL.  More in this month's<br></span></div><div><span style="font-family:monospace,monospace">elections-wg archive, public-facing.<br></span></div><div><span style="font-family:monospace,monospace"><br></span></div><span style="font-family:monospace,monospace">It's just another way to introduce the Fundamental Theorem of<br></span></div><span style="font-family:monospace,monospace">Arithmetic while reminding students of the composite vs. prime<br></span></div><span style="font-family:monospace,monospace">distinction.  Standard fare.<br><br></span></div><span style="font-family:monospace,monospace">(CL) copyleft, K. Urner / 4Dsolutions.net, 2015<br><br></span><div><div><div><div><div><span style="font-family:monospace,monospace">"""<br><br></span><div><div><div><div><div><div><span style="font-family:monospace,monospace"><br>import random<br><br>class Ballot:<br>    """secret:  unique prime belongs to ballot itself<br>    """<br><br>    def __init__(self):<br>        self.dna = random.choice(pool_of_primes)  # this is done at the store<br>        pool_of_primes.remove(self.dna)<br><br>    def vote(self, name, vote=1):<br>        self.candidate = name<br>        self.approve = vote<br><br>    def __repr__(self):<br>        return "Ballot with prime {}".format(self.dna)<br><br>pool_of_primes = [int(it) for it in """\<br> 2    3   5   7  11  13  17  19  23  29<br>31   37  41  43  47  53  59  61  67  71<br>73   79  83  89  97 101 103 107 109 113<br>127 131 137 139 149 151 157 163 167 173<br>179 181 191 193 197 199 211 223 227 229<br>233 239""".split()]  # 52 in a deck in this example<br><br>deck = [Ballot() for _ in range(52)]  # two extra ballots were not used<br>random.shuffle(deck) # dealt to voters (how many they get depends on how many candidates)<br><br># ballots coming in from voters all over the world!....<br><br># each candidate on the slate gates same number of ballots cast<br><br>stackA = []<br>for _ in range(26):<br>    secret_ballot = deck.pop()<br>    secret_ballot.vote("Shrek")<br>    stackA.append(secret_ballot)<br><br>stackB = []<br>for _ in range(26):<br>    secret_ballot = deck.pop()<br>    secret_ballot.vote("Alice")<br>    stackB.append(secret_ballot)<br><br># the Vote! a TIE, 25 for each candidate!<br><br>def break_tie(sA, sB):<br>    productA, productB = 1, 1<br>    for idx in range(len(sA)):<br>        productA = productA * sA[idx].dna<br>        productB = productB * sB[idx].dna<br>    if productA == productB:<br>        print("The impossible happened")<br>        raise Exception<br>    if productA > productB:<br>        print("Shrek wins!")<br>    else:<br>        print("Alice wins!")<br><br>break_tie(stackA, stackB)<br>print("The crowd roars")<br></span><br></div></div></div></div></div></div></div></div></div></div></div></div>