1class Currency :
2 def __init__(self, currency_name, exchange_rate) :
3 self.currency_name = currency_name
4 self.exchange_rate = exchange_rate
5
6 def __eq__(self, other):
7 return True if self.currency_name == other.currency_name and self.exchange_rate == other.exchange_rate else False
8
9
10class Coalition:
11 def __init__(self, *parties):
12 self.partners = parties
13 self.members = {}
14 for party in parties:
15 if isinstance(party, PoliticalParty):
16 self.members[str(party)] = party.members
17 else:
18 for name, members in party.members.items():
19 self.members[name] = members
20
21 def __str__(self):
22 return str("-".join(str(party) for party in self.members.keys()))
23
24 def __add__(self, other):
25 return Coalition(self, other) if isinstance(other, PoliticalParty) or isinstance(other, Coalition) else None
26
27
28class PoliticalParty:
29 def __init__(self, party_name, motto, members = [], preferred_currency = None):
30 self.party_name = party_name
31 object.__setattr__(self, "motto", motto)
32 self.members = members
33 self.preferred_currency = preferred_currency
34
35 def __setattr__(self, name, value):
36 None if name == "motto" else object.__setattr__(self, name, value)
37
38 def __str__(self):
39 return self.party_name
40
41 def __add__(self, other):
42 return Coalition(self, other) if isinstance(other, PoliticalParty) else None
43
44 def convert_currency_to_voters(self, amount, type_of_currency):
45 return ((amount / type_of_currency.exchange_rate) // 1) * (2 if isinstance(self.preferred_currency, type_of_currency.__class__) else 1)
46
47
48class Elections:
49 year_election = []
50
51 def __init__(self, year):
52 self.year = year
53 self.registered_parties_or_coalitions = {}
54 Elections.year_election.append(self)
55
56 def register_party_or_coalition(self, party_or_coalition):
57 self.registered_parties_or_coalitions[str(party_or_coalition)] = 0
58
59 def vote(self, party_or_coalition):
60 self.registered_parties_or_coalitions[str(party_or_coalition)] += 1
61
62 def rig_elections(self, party_or_coalition, amount, currency):
63 if isinstance(party_or_coalition, Coalition):
64 for party in party_or_coalition.partners:
65 if party.preferred_currency == currency:
66 self.registered_parties_or_coalitions[str(party_or_coalition)] += party.convert_currency_to_voters(amount, currency)
67 return
68 self.registered_parties_or_coalitions[str(party_or_coalition)] += party_or_coalition.convert_currency_to_voters(amount, currency)
69
70 def get_results(self):
71 return self.registered_parties_or_coalitions
72
73 @staticmethod
74 def get_results_by_year(year):
75 for election in Elections.year_election:
76 if election.year == year:
77 return election.get_results()
...........E.....F...
======================================================================
ERROR: test_rig_elections_preferred_currency (test.TestElections.test_rig_elections_preferred_currency)
rig_elections should use the most effective preferred currency conversion in a coalition.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 163, in test_rig_elections_preferred_currency
self.elections_2026.rig_elections(self.coalition, 80, self.currency)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 65, in rig_elections
if party.preferred_currency == currency:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in __eq__
return True if self.currency_name == other.currency_name and self.exchange_rate == other.exchange_rate else False
^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'currency_name'
======================================================================
FAIL: test_motto_write (test.TestPoliticalParty.test_motto_write)
Motto should not be writable after the party is created.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 37, in test_motto_write
with self.assertRaises(Exception):
~~~~~~~~~~~~~~~~~^^^^^^^^^^^
AssertionError: Exception not raised
----------------------------------------------------------------------
Ran 21 tests in 0.002s
FAILED (failures=1, errors=1)
| f | 1 | class Currency : | f | 1 | class Currency : |
| 2 | def __init__(self, currency_name, exchange_rate) : | 2 | def __init__(self, currency_name, exchange_rate) : | ||
| 3 | self.currency_name = currency_name | 3 | self.currency_name = currency_name | ||
| 4 | self.exchange_rate = exchange_rate | 4 | self.exchange_rate = exchange_rate | ||
| 5 | 5 | ||||
| 6 | def __eq__(self, other): | 6 | def __eq__(self, other): | ||
| 7 | return True if self.currency_name == other.currency_name and self.exchange_rate == other.exchange_rate else False | 7 | return True if self.currency_name == other.currency_name and self.exchange_rate == other.exchange_rate else False | ||
| 8 | 8 | ||||
| 9 | 9 | ||||
| 10 | class Coalition: | 10 | class Coalition: | ||
| 11 | def __init__(self, *parties): | 11 | def __init__(self, *parties): | ||
| 12 | self.partners = parties | 12 | self.partners = parties | ||
| 13 | self.members = {} | 13 | self.members = {} | ||
| 14 | for party in parties: | 14 | for party in parties: | ||
| 15 | if isinstance(party, PoliticalParty): | 15 | if isinstance(party, PoliticalParty): | ||
| 16 | self.members[str(party)] = party.members | 16 | self.members[str(party)] = party.members | ||
| 17 | else: | 17 | else: | ||
| 18 | for name, members in party.members.items(): | 18 | for name, members in party.members.items(): | ||
| 19 | self.members[name] = members | 19 | self.members[name] = members | ||
| 20 | 20 | ||||
| 21 | def __str__(self): | 21 | def __str__(self): | ||
| 22 | return str("-".join(str(party) for party in self.members.keys())) | 22 | return str("-".join(str(party) for party in self.members.keys())) | ||
| 23 | 23 | ||||
| 24 | def __add__(self, other): | 24 | def __add__(self, other): | ||
| 25 | return Coalition(self, other) if isinstance(other, PoliticalParty) or isinstance(other, Coalition) else None | 25 | return Coalition(self, other) if isinstance(other, PoliticalParty) or isinstance(other, Coalition) else None | ||
| 26 | 26 | ||||
| 27 | 27 | ||||
| 28 | class PoliticalParty: | 28 | class PoliticalParty: | ||
| 29 | def __init__(self, party_name, motto, members = [], preferred_currency = None): | 29 | def __init__(self, party_name, motto, members = [], preferred_currency = None): | ||
| 30 | self.party_name = party_name | 30 | self.party_name = party_name | ||
| 31 | object.__setattr__(self, "motto", motto) | 31 | object.__setattr__(self, "motto", motto) | ||
| 32 | self.members = members | 32 | self.members = members | ||
| 33 | self.preferred_currency = preferred_currency | 33 | self.preferred_currency = preferred_currency | ||
| 34 | 34 | ||||
| 35 | def __setattr__(self, name, value): | 35 | def __setattr__(self, name, value): | ||
| 36 | None if name == "motto" else object.__setattr__(self, name, value) | 36 | None if name == "motto" else object.__setattr__(self, name, value) | ||
| 37 | 37 | ||||
| 38 | def __str__(self): | 38 | def __str__(self): | ||
| 39 | return self.party_name | 39 | return self.party_name | ||
| 40 | 40 | ||||
| 41 | def __add__(self, other): | 41 | def __add__(self, other): | ||
| 42 | return Coalition(self, other) if isinstance(other, PoliticalParty) else None | 42 | return Coalition(self, other) if isinstance(other, PoliticalParty) else None | ||
| 43 | 43 | ||||
| 44 | def convert_currency_to_voters(self, amount, type_of_currency): | 44 | def convert_currency_to_voters(self, amount, type_of_currency): | ||
| 45 | return ((amount / type_of_currency.exchange_rate) // 1) * (2 if isinstance(self.preferred_currency, type_of_currency.__class__) else 1) | 45 | return ((amount / type_of_currency.exchange_rate) // 1) * (2 if isinstance(self.preferred_currency, type_of_currency.__class__) else 1) | ||
| 46 | 46 | ||||
| 47 | 47 | ||||
| 48 | class Elections: | 48 | class Elections: | ||
| 49 | year_election = [] | 49 | year_election = [] | ||
| 50 | 50 | ||||
| 51 | def __init__(self, year): | 51 | def __init__(self, year): | ||
| 52 | self.year = year | 52 | self.year = year | ||
| 53 | self.registered_parties_or_coalitions = {} | 53 | self.registered_parties_or_coalitions = {} | ||
| 54 | Elections.year_election.append(self) | 54 | Elections.year_election.append(self) | ||
| 55 | 55 | ||||
| 56 | def register_party_or_coalition(self, party_or_coalition): | 56 | def register_party_or_coalition(self, party_or_coalition): | ||
| 57 | self.registered_parties_or_coalitions[str(party_or_coalition)] = 0 | 57 | self.registered_parties_or_coalitions[str(party_or_coalition)] = 0 | ||
| 58 | 58 | ||||
| 59 | def vote(self, party_or_coalition): | 59 | def vote(self, party_or_coalition): | ||
| 60 | self.registered_parties_or_coalitions[str(party_or_coalition)] += 1 | 60 | self.registered_parties_or_coalitions[str(party_or_coalition)] += 1 | ||
| 61 | 61 | ||||
| 62 | def rig_elections(self, party_or_coalition, amount, currency): | 62 | def rig_elections(self, party_or_coalition, amount, currency): | ||
| 63 | if isinstance(party_or_coalition, Coalition): | 63 | if isinstance(party_or_coalition, Coalition): | ||
| 64 | for party in party_or_coalition.partners: | 64 | for party in party_or_coalition.partners: | ||
| 65 | if party.preferred_currency == currency: | 65 | if party.preferred_currency == currency: | ||
| 66 | self.registered_parties_or_coalitions[str(party_or_coalition)] += party.convert_currency_to_voters(amount, currency) | 66 | self.registered_parties_or_coalitions[str(party_or_coalition)] += party.convert_currency_to_voters(amount, currency) | ||
| 67 | return | 67 | return | ||
| 68 | self.registered_parties_or_coalitions[str(party_or_coalition)] += party_or_coalition.convert_currency_to_voters(amount, currency) | 68 | self.registered_parties_or_coalitions[str(party_or_coalition)] += party_or_coalition.convert_currency_to_voters(amount, currency) | ||
| 69 | 69 | ||||
| 70 | def get_results(self): | 70 | def get_results(self): | ||
| 71 | return self.registered_parties_or_coalitions | 71 | return self.registered_parties_or_coalitions | ||
| 72 | 72 | ||||
| 73 | @staticmethod | 73 | @staticmethod | ||
| 74 | def get_results_by_year(year): | 74 | def get_results_by_year(year): | ||
| 75 | for election in Elections.year_election: | 75 | for election in Elections.year_election: | ||
| 76 | if election.year == year: | 76 | if election.year == year: | ||
| 77 | return election.get_results() | 77 | return election.get_results() | ||
| t | 78 | t | |||
| 79 | x = PoliticalParty("Democrats", "For the people", preferred_currency=Currency("USD", 1)) | ||||
| 80 | x.motto = "Changed motto" | ||||
| 81 | print(x.motto) # For the people |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
22.03.2026 11:35
22.03.2026 11:37
22.03.2026 11:39