Handle ballots of uneven length

This commit is contained in:
Peter J. Holzer 2022-11-29 20:55:23 +01:00
parent d1ffb55f08
commit c45e514bfc
1 changed files with 7 additions and 1 deletions

6
app.py
View File

@ -354,11 +354,17 @@ def runoff(ballots):
candidates = {} candidates = {}
for ballot in ballots: for ballot in ballots:
for r in ballot: for r in ballot:
if r.id not in count or len(count[r.id]) < len(ballot):
log.debug("count[%d] <- %d elements", r.id, len(ballot))
count[r.id] = [0] * len(ballot) count[r.id] = [0] * len(ballot)
candidates[r.id] = r candidates[r.id] = r
for ballot in ballots: for ballot in ballots:
for pos, r in enumerate(ballot): for pos, r in enumerate(ballot):
log.debug("count = %s", count)
log.debug("r.id = %s", r.id)
log.debug("pos = %s", pos)
count[r.id][pos] += 1 count[r.id][pos] += 1
log.debug("count[%d][%d]) = %d", r.id, pos, count[r.id][pos])
result = sorted(count.keys(), key=lambda i: count[i]) result = sorted(count.keys(), key=lambda i: count[i])
log.debug("result of this round:") log.debug("result of this round:")
for r in result: for r in result: