Subversion Repositories Remote Hare Voting

Rev

Rev 44 | Rev 46 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 44 Rev 45
Line 24... Line 24...
24
 
24
 
25
import sys
25
import sys
26
import math 
26
import math 
27
import random
27
import random
28
 
28
 
-
 
29
fdOut = None
29
fTrace = 1
30
fTrace = 1
30
randcalls = 0
31
randcalls = 0
31
 
32
 
32
def trace(s):
33
def trace(s):
33
    global fTrace
34
    global fTrace, fdOut
34
    if fTrace: print s
35
    if fTrace: print >>fdOut, s
35
    return
36
    return
36
 
37
 
37
def findWinner(winners, losers, tally, quota, nWinners):
38
def findWinner(winners, losers, tally, quota, nWinners):
38
    global randcalls
39
    global randcalls
39
    cWin = quota       # number of votes for highest winner
40
    cWin = quota       # number of votes for highest winner
Line 160... Line 161...
160
def dotally(nWinners, ballots):
161
def dotally(nWinners, ballots):
161
    global randcalls
162
    global randcalls
162
    nBallots = len(ballots)
163
    nBallots = len(ballots)
163
    quota = int(math.ceil((nBallots + 1.0)/(nWinners + 1)))
164
    quota = int(math.ceil((nBallots + 1.0)/(nWinners + 1)))
164
 
165
 
165
    trace("INPUT SUMMARY")
166
    trace("INPUT SUMMARY AND SINGLE RUN TRACE")
166
    trace("\t%d ballots" % nBallots)
167
    trace("\t%d ballots" % nBallots)
167
    trace("\tChoosing %s winners" % nWinners)
168
    trace("\tChoosing %s winners" % nWinners)
168
    trace("\tNeed ceil((%d + 1)/(%d + 1)) = %d ballots to win" %
169
    trace("\tNeed ceil((%d + 1)/(%d + 1)) = %d ballots to win" %
169
          (nBallots, nWinners, quota))
170
          (nBallots, nWinners, quota))
170
 
171
 
171
    # Create initial tally
172
    # Create initial tally and statistics
172
    #
173
    #
173
    tally = {}
174
    tally = {}
174
    for ballot in ballots:
175
    for ballot in ballots:
175
        candidate = ballot[0]
176
        candidate = ballot[0]
176
        if not tally.has_key(candidate):
177
        if not tally.has_key(candidate):
177
            tally[candidate] = []
178
            tally[candidate] = []
178
        tally[candidate].append(ballot)
179
        tally[candidate].append(ballot)
-
 
180
 
-
 
181
    total1st = {}
-
 
182
    total2nd = {}
-
 
183
    totalMrk = {}
-
 
184
 
-
 
185
    for ballot in ballots:
-
 
186
        for c in ballot:
-
 
187
            total1st[c] = 0
-
 
188
            total2nd[c] = 0
-
 
189
            totalMrk[c] = 0
-
 
190
 
-
 
191
    for ballot in ballots:
-
 
192
        total1st[ballot[0]] += 1
-
 
193
        if len(ballot) > 1:
-
 
194
            total2nd[ballot[1]] += 1
-
 
195
        for c in ballot:
-
 
196
            totalMrk[c] += 1
-
 
197
 
-
 
198
    trace("\n\tBALLOT MARKS\t1ST\t2ND\tNONE")
-
 
199
    candidates = totalMrk.keys()
-
 
200
    candidates.sort()
-
 
201
    for c in candidates:
-
 
202
        trace("\t%-16s%3d\t%3d\t%4d" % (c, total1st[c], total2nd[c], len(ballots)-totalMrk[c]))
179
    traceTally(quota, tally)
203
    traceTally(quota, tally)
180
    
204
    
181
    winners = []
205
    winners = []
182
    losers = []
206
    losers = []
183
 
207