1class Currency:
2 def __init__(self, name, course):
3 self.name=name
4 self.course=course
5 def __eq__(self, other):
6 if not isinstance(other, Currency):
7 return False
8 return self.name==other.name and self.course==other.course
9
10class PoliticalParty:
11 def __init__ (self, name, moto, members=None, preferred_currency=None):
12 self.name=name
13 self._moto=moto
14 self.members = members if members is not None else []
15 self.preferred_currency= preferred_currency
16 def convert_currency_to_voters (self,num, val):
17 if not isinstance(val, Currency):
18 return 0
19 if val == self.preferred_currency:
20 return int(num/val.course)*2
21 return int(num/val.course)
22 def __str__(self):
23 return self.name
24 def __repr__(self):
25 return self.name
26 def __eq__ (self, other):
27 return isinstance(other, PoliticalParty) and self.name==other.name
28 def __hash__ (self):
29 return hash(self.name)
30 def __add__(self, other):
31 if isinstance(other, PoliticalParty):
32 return Coalition(self,other)
33 @property
34 def motto(self):
35 return self._moto
36
37class Coalition:
38 def __init__(self, *partii):
39 self.partii=[]
40 self.__unique=set()
41 for p in partii:
42 self.add_partii(p)
43 def add_partii(self, p):
44 if isinstance(p, PoliticalParty):
45 if p not in self.__unique:
46 self.partii.append(p)
47 self.__unique.add(p)
48 elif isinstance(p, Coalition):
49 for partiq in p.partii:
50 self.add_partii(partiq)
51 def __str__ (self):
52 return "-".join(str(p) for p in self.partii)
53 def __add__(self, other):
54 if isinstance(other, (Coalition, PoliticalParty)):
55 return Coalition(self,other)
56 @property
57 def members(self):
58 return {p.name : p.members for p in self.partii }
59
60class Elections:
61 _results = {}
62 def __init__ (self, year):
63 self.year=year
64 Elections._results[year]={}
65 def register_party_or_coalition (self, k):
66 if isinstance(k,(PoliticalParty, Coalition)):
67 Elections._results[self.year][str(k)]=0
68 def vote (self, k):
69 if isinstance(k, (PoliticalParty, Coalition)):
70 Elections._results[self.year][str(k)]+=1
71 def rig_elections (self, k, num, val):
72 if isinstance(k, PoliticalParty) and isinstance(val, Currency):
73 Elections._results[self.year][str(k)]+=k.convert_currency_to_voters(num,val)
74 elif isinstance(k, Coalition ) and isinstance(val, Currency):
75 cur=Elections._results[self.year][str(k)]
76 for p in k.partii:
77 if p.preferred_currency==val:
78 Elections._results[self.year][str(k)]+=p.convert_currency_to_voters(num,val)
79 break
80 if Elections._results[self.year][str(k)] == cur:
81 Elections._results[self.year][str(k)]+=k.partii[0].convert_currency_to_voters(num,val)
82 def get_results(self):
83 return Elections._results[self.year]
84 @classmethod
85 def get_results_by_year(cls, year):
86 if year in cls._results:
87 return cls._results[year]
.....................
----------------------------------------------------------------------
Ran 21 tests in 0.001s
OK
Виктор Бечев
25.03.2026 11:33Отвъд стила - решението не е лошо. Само да се четеше малко по-добре.
|
25.03.2026 11:29
25.03.2026 11:29
25.03.2026 11:30
25.03.2026 11:31
25.03.2026 11:33
25.03.2026 11:34