Subversion Repositories Remote Hare Voting

Rev

Rev 15 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 15 Rev 21
Line 1... Line 1...
1
import string
1
import string
2
 
2
 
3
# Parse input file
3
# Parse input file
4
#
4
#
5
# input: lines of comma-separated values
5
# input: lines of comma-separated values
6
#	nWinners, ...ignored fields...
6
#       nWinners, ...ignored fields...
7
#	candidate1, ballot1rank, ballot2rank, ...
7
#       candidate1, ballot1rank, ballot2rank, ...
8
#	candidate2, ballot1rank, ballot2rank, ...
8
#       candidate2, ballot1rank, ballot2rank, ...
9
#
9
#
10
# output: list of lists of ballots
10
# output: list of lists of ballots
11
#	[[1stcandbal1, 2ndcandbal1, ...], [1stcandbal2, 2ndcandbal2, ...], ...]
11
#       [[1stcandbal1, 2ndcandbal1, ...], [1stcandbal2, 2ndcandbal2, ...], ...]
12
 
12
 
13
def parsestring(contents):
13
def parsestring(contents):
14
    lines = string.split(contents, "\r\n")
14
    lines = contents.splitlines()
15
    fields = string.split(lines[0], ",")
15
    fields = string.split(lines[0], ",")
16
    nWinners = int(fields[0])
16
    nWinners = int(fields[0])
17
 
17
 
18
    votes = []
18
    votes = []
19
    for line in lines[1:]:
19
    for line in lines[1:]:
20
        fields = string.split(line, ",")
20
        fields = string.split(line, ",")
21
        candidate = fields[0]
21
        candidate = fields[0]
22
	positions = fields[1:]
22
        positions = fields[1:]
23
        ballotnumber = 0
23
        ballotnumber = 0
24
	for position in positions:
24
        for position in positions:
25
            if position:
25
            if position:
26
                votes.append([ballotnumber, int(position), candidate])
26
                votes.append([ballotnumber, int(position), candidate])
27
            ballotnumber = ballotnumber + 1
27
            ballotnumber = ballotnumber + 1
28
 
28
 
29
    votes.sort()
29
    votes.sort()
Line 31... Line 31...
31
    ballots = []
31
    ballots = []
32
    ballotnumber = ""
32
    ballotnumber = ""
33
    ballot = ""
33
    ballot = ""
34
    for triple in votes:
34
    for triple in votes:
35
        if ballotnumber == triple[0]:
35
        if ballotnumber == triple[0]:
36
	    ballot.append(triple[2])
36
            ballot.append(triple[2])
37
        else:
37
        else:
38
            if ballot:
38
            if ballot:
39
                ballots.append(ballot)
39
                ballots.append(ballot)
40
            ballot = [triple[2]]
40
            ballot = [triple[2]]
41
            ballotnumber = triple[0]
41
            ballotnumber = triple[0]