1class Currency:
2 def __init__(self, name, rate):
3 self.name = name
4 self.rate = rate
5
6 def __eq__(self, other):
7 if not isinstance(other, Currency):
8 return False
9 return self.name == other.name and self.rate == other.rate
10
11
12class PoliticalParty:
13 def __init__(self, name, motto, members=None, preferred_currency=None):
14 self.name = name
15 self._motto = motto
16 self.members = members if members is not None else []
17 self.preferred_currency = preferred_currency
18
19 @property
20 def motto(self):
21 return self._motto
22
23 def convert_currency_to_voters(self, amount, currency):
24 voters = int(amount / currency.rate)
25
26 if self.preferred_currency == currency:
27 return voters * 2
28 return voters
29
30 def __str__(self):
31 return self.name
32
33 def __add__(self, other):
34 if isinstance(other, PoliticalParty):
35 return Coalition(self, other)
36 return NotImplemented
37
38
39class Coalition:
40 def __init__(self, *parties):
41 self._parties = list(parties)
42
43 @property
44 def members(self):
45 return {party.name: party.members for party in self._parties}
46
47 def __add__(self, other):
48 if isinstance(other, PoliticalParty):
49 return Coalition(*(self._parties + [other]))
50 elif isinstance(other, Coalition):
51 return Coalition(*(self._parties + other._parties))
52 return NotImplemented
53
54 def __str__(self):
55 return '-'.join([str(party) for party in self._parties])
56
57
58class Elections:
59 _all_elections = {}
60
61 def __init__(self, year):
62 self.year = year
63 self.results = {}
64 Elections._all_elections[self.year] = self.results
65
66 def register_party_or_coalition(self, candidate):
67 if isinstance(candidate, (PoliticalParty, Coalition)):
68 name = str(candidate)
69 if name not in self.results:
70 self.results[name] = 0
71
72 def vote(self, candidate):
73 name = str(candidate)
74 if name in self.results:
75 self.results[name] += 1
76
77 def rig_elections(self, candidate, amount, currency):
78 name = str(candidate)
79 if name not in self.results:
80 return
81
82 if isinstance(candidate, PoliticalParty):
83 self.results[name] += candidate.convert_currency_to_voters(amount, currency)
84 elif isinstance(candidate, Coalition):
85 bought_votes = 0
86 for party in candidate._parties:
87 votes_from_party = party.convert_currency_to_voters(amount, currency)
88 if votes_from_party > bought_votes:
89 bought_votes = votes_from_party
90 self.results[name] += bought_votes
91
92 def get_results(self):
93 return self.results
94
95 @classmethod
96 def get_results_by_year(cls, year):
97 return cls._all_elections.get(year, {})
.....................
----------------------------------------------------------------------
Ran 21 tests in 0.001s
OK
25.03.2026 11:45
25.03.2026 11:47