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