Logo Contest IRV programs

[SVN r28260]
This commit is contained in:
Joel de Guzman 2005-04-15 06:17:20 +00:00
parent d50180530c
commit 7ad7d804e3
3 changed files with 1198 additions and 0 deletions

125
logo_contest_irv/irv.cpp Normal file
View File

@ -0,0 +1,125 @@
#include <string>
#include <vector>
#include <list>
#include <map>
#include <iostream>
using namespace std;
int main ( )
{
string s;
list < string > vote;
vector < list < string > > votes;
map < string, size_t > candidates;
while ( true )
{
getline ( cin, s );
if ( cin.eof ( ) )
{
if ( !vote.empty ( ) ) votes.push_back ( vote );
break;
}
if ( s == "vote:" )
{
if ( !vote.empty ( ) )
{
votes.push_back ( vote );
vote.clear ( );
}
}
else
{
candidates.insert ( map < string, size_t >::value_type ( s, 0 ) );
vote.push_back ( s );
}
}
bool winner = false;
size_t min_votes;
map < string, size_t >::iterator c_iter, c_next;
vector < list < string > >::iterator vl_iter;
while ( !winner )
{
c_iter = candidates.begin ( );
while ( c_iter != candidates.end ( ) )
{
cout << "clearing votes for " << c_iter->first << endl;
c_iter->second = 0;
++c_iter;
}
vl_iter = votes.begin ( );
while ( vl_iter != votes.end ( ) )
{
list < string >::iterator v_iter ( vl_iter->begin ( ) );
while ( v_iter != vl_iter->end ( ) )
{
c_iter = candidates.find ( *v_iter );
if ( c_iter != candidates.end ( ) )
{
++( c_iter->second );
break;
}
++v_iter;
}
++vl_iter;
}
c_iter = candidates.begin ( );
min_votes = c_iter->second;
winner = true;
while ( c_iter != candidates.end ( ) )
{
cout << c_iter->first << " has a total of " << c_iter->second << " votes" << endl;
if ( c_iter->second != min_votes )
{
winner = false;
}
if ( c_iter->second < min_votes )
{
min_votes = c_iter->second;
}
++c_iter;
}
if ( winner )
{
if ( candidates.size ( ) > 1 )
{
cout << endl << "TIE, WINNERS WERE:" << endl;
}
else
{
cout << endl << "WINNER:" << endl;
}
}
c_iter = candidates.begin ( );
while ( c_iter != candidates.end ( ) )
{
c_next = c_iter;
++c_next;
if ( winner )
{
cout << c_iter->first << endl;
}
else if ( c_iter->second == min_votes )
{
cout << c_iter->first << " has been removed from the running" << endl;
candidates.erase ( c_iter );
}
c_iter = c_next;
}
}
return 0;
}

77
logo_contest_irv/irv.py Normal file
View File

@ -0,0 +1,77 @@
import string
f = open("votes.txt")
def load_votes():
result = []
f = open("votes.txt")
for l in f:
if string.strip(l) == "vote:":
result.append([])
else:
result[-1].append(int(l))
return result
def count_first_votes(votes):
result = {}
for l in votes:
if l:
first_vote = l[0]
if not result.has_key(first_vote):
result[first_vote] = 1
else:
result[first_vote] = result[first_vote]+1
return result
def winner(first_votes):
print "Trying to find a winner"
total_votes = 0
for l in first_votes:
total_votes += first_votes[l]
print total_votes, "total votes"
for l in first_votes:
if first_votes[l] > total_votes/2:
return l
return None
def looser(first_votes):
min_votes = None
looser = None
for l in first_votes:
if min_votes == None or first_votes[l] < min_votes:
min_votes = first_votes[l]
looser = l
return looser
def drop_looser(raw_votes, looser):
for l in raw_votes:
if l and l[0] == looser:
del l[0]
raw = load_votes()
print len(raw), "initial votes"
for i in raw:
print i
while 1:
counted = count_first_votes(raw)
print "First votes:"
for i in counted:
print i, " --> ", counted[i]
w = winner(counted)
if w is not None:
print "Winner is", w
break
else:
print "No winner yet"
l = looser(counted)
assert l is not None
print "Dropping", l, "which had", counted[l], "votes"
drop_looser(raw, l)

996
logo_contest_irv/votes.txt Normal file
View File

@ -0,0 +1,996 @@
vote:
87
39
50
67
74
vote:
87
39
50
67
74
vote:
75
92
98
52
67
vote:
61
98
92
21
29
vote:
92
38
18
39
67
vote:
50
61
86
95
21
vote:
37
37
37
37
37
vote:
38
39
75
1
44
vote:
67
83
75
39
88
vote:
18
60
67
52
75
vote:
83
52
67
68
18
vote:
67
17
21
83
92
vote:
50
vote:
50
50
7
88
24
vote:
83
38
75
5
17
vote:
83
38
75
5
17
vote:
67
92
69
74
20
vote:
50
70
27
38
49
vote:
25
68
43
17
69
vote:
38
67
52
75
83
vote:
38
39
50
29
4
vote:
50
95
85
76
51
vote:
1
31
21
vote:
68
39
17
46
85
vote:
68
67
68
68
75
vote:
92
39
27
86
vote:
28
67
39
75
54
vote:
67
39
38
17
6
vote:
50
36
75
60
99
vote:
67
vote:
52
75
50
83
vote:
50
50
83
83
75
vote:
39
38
46
43
54
vote:
60
20
67
29
18
vote:
69
68
38
60
19
vote:
20
85
50
92
97
vote:
75
75
39
38
1
vote:
67
67
68
75
75
vote:
68
83
20
38
67
vote:
38
67
68
69
20
vote:
39
75
68
92
83
vote:
18
75
29
1
vote:
0
75
75
38
1
vote:
83
68
38
75
23
vote:
50
58
63
40
53
vote:
91
56
57
85
88
vote:
39
6
3
32
98
vote:
50
85
98
91
93
vote:
50
75
81
38
97
vote:
3
50
88
75
99
vote:
75
20
39
60
83
vote:
39
60
4
67
83
vote:
18
67
68
83
43
vote:
50
50
51
58
76
vote:
68
83
95
85
21
vote:
50
50
50
50
50
vote:
20
92
71
33
5
vote:
2
68
68
68
2
vote:
75
67
39
83
29
vote:
42
6
46
44
20
vote:
20
86
25
36
18
vote:
38
5
2
92
29
vote:
46
17
66
85
27
vote:
61
50
25
76
60
vote:
60
42
92
27
18
vote:
67
67
43
46
43
vote:
38
38
77
76
10
vote:
3
67
52
38
1
vote:
20
88
97
18
48
vote:
97
6
92
50
57
vote:
3
5
67
88
50
vote:
75
68
85
98
60
vote:
50
50
67
20
75
vote:
39
20
92
67
99
vote:
85
75
67
17
60
vote:
2
19
67
75
18
vote:
75
68
12
38
83
vote:
96
85
92
83
88
vote:
75
52
60
47
50
vote:
83
67
75
20
17
vote:
38
5
50
67
39
vote:
92
69
48
68
20
vote:
86
23
20
18
17
vote:
39
38
5
75
19
vote:
92
19
29
75
77
vote:
68
67
85
46
38
vote:
3
17
18
68
10
vote:
50
63
68
84
96
vote:
25
67
6
99
68
vote:
38
39
38
39
67
vote:
5
18
20
3
4
vote:
67
67
20
20
75
vote:
83
67
39
5
29
vote:
83
50
6
38
75
vote:
21
15
29
10
67
vote:
61
20
67
50
92
vote:
17
36
5
33
42
vote:
29
29
30
68
75
vote:
87
87
87
62
52
vote:
38
39
75
20
92
vote:
67
83
92
50
vote:
21
29
20
67
92
vote:
13
5
46
20
42
vote:
19
28
33
39
71
vote:
23
39
39
5
6
vote:
19
7
38
29
99
vote:
60
39
50
48
25
vote:
67
50
52
75
83
vote:
46
20
39
75
92
vote:
39
75
50
60
68
vote:
51
17
38
49
75
vote:
61
39
20
46
97
vote:
85
68
83
75
67
vote:
1
vote:
50
75
20
92
87
vote:
75
52
68
67
83
vote:
52
28
75
83
67
vote:
3
6
18
47
5
vote:
67
67
87
49
63
vote:
97
50
68
6
29
vote:
99
75
20
1
vote:
21
21
68
85
99
vote:
38
75
5
17
20
vote:
91
52
83
29
94
vote:
92
75
38
60
52
vote:
67
83
49
21
38
vote:
67
83
74
38
3
vote:
18
61
92
5
39
vote:
92
83
68
6
67
vote:
5
20
19
60
92
vote:
75
75
75
38
97
vote:
39
68
83
75
17
vote:
67
50
75
92
38
vote:
68
83
5
67
23
vote:
39
3
17
43
67
vote:
61
92
19
95
50
vote:
74
56
57
58
59
vote:
38
5
83
6
67
vote:
50
92
39
69
39
vote:
67
7
75
20
68
vote:
67
38
92
75
46
vote:
50
vote:
85
69
68
101
92
vote:
85
67
43
75
5
vote:
67
83
29
1
39
vote:
18
23
68
68
67
vote:
75
60
21
38
67
vote:
50
50
38
92
81
vote:
42
98
20
23
85
vote:
30
30
69
85
2
vote:
38
92
5
24
67
vote:
52
75
83
68
29
vote:
75
52
75
52
52
vote:
68
52
85
80
22
vote:
68
67
17
51
85
vote:
68
75
83
38
1
vote:
50
50
10
88
23
vote:
52
75
50
83
vote:
50
75
97
61
83
vote:
62
43
46
19
5
vote:
85
31
38
38
38
vote:
83
38
75
28
20
vote:
43
85
7
37
97
vote:
75
60
67
20
49
vote:
75
68
39
21
98
vote:
85
68
23
89
31
vote:
20
3
5
17
91
vote:
48
83
21
67
75
vote:
61
60
20
75
68
vote:
50
97
88
46
75