-
Notifications
You must be signed in to change notification settings - Fork 0
/
décompte.py
executable file
·65 lines (50 loc) · 1.7 KB
/
décompte.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/env python2.7
# -*- coding: utf-8 -*-
# Décompte de Condorcet
# (C) 2009 Arkanosis
# jroquet@arkanosis.net
# Suivant la méthode expliquée sur
# http://fr.wikipedia.org/wiki/Méthode_Condorcet
# Ce script est placé dans le domaine public
import itertools
import sys
if len(sys.argv) != 2:
print 'Usage: décompte.py <votes.txt>'
sys.exit(1)
votes = []
with open(sys.argv[1]) as votes_bruts:
for vote_brut in votes_bruts:
votes.append({})
rangs = vote_brut.replace(' ', '').split('|')[1].split('>')
for valeur, rang in enumerate(rangs):
for option in rang.replace(',', '=').split('='):
votes[-1][option.strip()] = valeur
options = votes[0].keys()
options.sort()
print 'Décompte selon la méthode Condorcet'
print 'Options possibles : ', options
scores = {}
score_max = len(options) - 1
for option in options:
scores[option] = 0
duels = itertools.combinations(options, 2)
for duel in duels:
print 'Duel entre %s et %s' % duel,
score = [0, 0, 0]
for vote in votes:
if vote[duel[0]] < vote[duel[1]]:
score[0] += 1
elif vote[duel[0]] > vote[duel[1]]:
score[1] += 1
else:
score[2] += 1
if score[0] == score[1]:
print '=> match nul'
else:
print '=> %s' % duel[score[0] < score[1]], '(à %d contre %d, %d indifférents)' % (max(score[:-1]), min(score[:-1]), score[2])
scores[duel[0]] += score[0] >= score[1]
scores[duel[1]] += score[0] <= score[1]
vainqueurs = filter(lambda option: scores[option] == score_max, options)
print 'Vainqueurs potentiels :', vainqueurs
if len(vainqueurs) != 1:
print 'La prise de décision n\'a pas abouti'