-
Notifications
You must be signed in to change notification settings - Fork 0
/
Urn.py
79 lines (54 loc) · 2.37 KB
/
Urn.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: latin1 -*-
#102618-OOMS AURELIEN-BA1 INFORMATIQUE-G2/Urn.py
from Ballot import *
from myAssert import *
from copy import deepcopy
class Urn:
def __init__(self):
self.__ballots = []
def addBallot(self,b):
self.__ballots.append(b)
def getSize(self):
return len(self.__ballots)
def tally(self):
return self.getCompressed(len(self.__ballots)).__ballots[0]
def assertInvariant(self):
for i in range(len(self.__ballots)-1):
assert(self.getBallot(i).numberOfPossibleChoices == self.getBallot(i+1).numberOfPossibleChoices)
def getCompressed(self,f):
try:
f = int(f)
except ValueError:
raise Exception("Le facteur de compression doit être un entier positif.")
if f < 0:#si f = 1 ou 0 getCompressed renvoie une "deepcopy" (les identifier ont été modifiés) de l'urne donnée en paramètre
raise Exception("Le facteur de compression doit être un entier positif.")
newUrn = Urn()
i = 0
while i < self.getSize():
compressedBallot = deepcopy(self.__ballots[i]) #pour modifier l'identifiant au cas où
i+=1
for j in range(f-1):
try:
compressedBallot += self.__ballots[i]
i+=1
except:
break
if compressedBallot == self.__ballots[i-1]: #on modifie l'identifiant si le ballot résultat est égal au précédent
compressedBallot.identifier = Ballot.getNewIdentifier()
newUrn.addBallot(compressedBallot)
return newUrn
def getBallot(self,index):
return self.__ballots[index]
if __name__ == '__main__':
from Constants_Test import *
from Functions_Test import *
u = Urn()
# print(u)
b = randomFilledBallotCreator()
u.addBallot(b)
#for i in range(5):
#u.addBallot(randomFilledBallotSimilator(b)) #randomFilledBallotSimilator() n'existe pas
print(u)
# print(u.getCompressed(1))
# print(u.getCompressed(2))
print(u.getCompressed(3))