1class Currency:
2
3 def __init__(self, currency, count):
4 self.currency = currency
5 self.count = count
6
7 def __eq__(self, other):
8 if not isinstance(other, Currency):
9 return False
10 return self.currency == other.currency and self.count == other.count
11
12
13class PoliticalParty:
14
15 def __init__(self, name, motto, members=None, preferred_currency = None):
16 self.name = name
17 self.__motto = motto
18 self.members = members if members is not None else []
19 self.preferred_currency = preferred_currency
20
21 @property
22 def motto(self):
23 return self.__motto
24
25 def convert_currency_to_voters(self, count, currency):
26 coefficient_for_preferred_currency = 2
27 if currency == self.preferred_currency:
28 return int((count / currency.count) * coefficient_for_preferred_currency)
29 return int(count / currency.count)
30
31 def __str__(self):
32 return self.name
33
34 def __add__(self, other):
35 return Coalition(self, other)
36
37
38class Coalition:
39
40 def __init__(self, *args):
41
42 self.members = {}
43 self.parties = set()
44 for party in args:
45 self.members[party.name] = party.members
46 self.parties.add(party)
47
48 def __add__(self, other):
49 all_parties = set(self.parties)
50
51 if isinstance(other, Coalition):
52 all_parties.update(other.parties)
53 if isinstance(other, PoliticalParty):
54 all_parties.add(other)
55 return Coalition(*all_parties)
56
57 def __str__(self):
58 parties = list(self.parties)
59 name = ""
60 for party in parties:
61 name += party.name + '-'
62 return name[: len(name) - 1]
63
64class Elections:
65
66 __election_history = {}
67
68 def __init__(self, year):
69 self.year = year
70 self.parties_and_votes = {}
71 self.__add_election(self, year)
72
73 def register_party_or_coalition(self, participant):
74
75 if str(participant) in self.parties_and_votes:
76 return
77 self.parties_and_votes[str(participant)] = 0
78
79 def vote(self, party):
80 if str(party) not in self.parties_and_votes:
81 return
82 self.parties_and_votes[str(party)] += 1
83
84 def rig_elections(self, party, amount, currency):
85
86 if str(party) not in self.parties_and_votes:
87 return
88 best_trade = None
89 if isinstance(party, Coalition):
90
91 for p in party.parties:
92 best_trade = p
93 if p.preferred_currency == currency.currency:
94 break
95
96 if isinstance(party, PoliticalParty):
97 best_trade = party
98
99 self.parties_and_votes[str(party)] += best_trade.convert_currency_to_voters(amount, currency)
100
101 def get_results(self):
102 return dict(self.parties_and_votes)
103
104 @classmethod
105 def __add_election(cls,election, year):
106 cls.__election_history[year] = election
107
108 @classmethod
109 def get_results_by_year(cls, year):
110 if year not in cls.__election_history:
111 return None
112 return cls.__election_history[year]
....F...F..F.........
======================================================================
FAIL: test_coalition_str (test.TestCoalition.test_coalition_str)
Coalition should convert to a dash-separated list of party names.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 128, in test_coalition_str
self.assertEqual("Party 1-Party 2", str(coalition))
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 'Party 1-Party 2' != 'Party 2-Party 1'
- Party 1-Party 2
? ^ ^
+ Party 2-Party 1
? ^ ^
======================================================================
FAIL: test_get_results_by_year (test.TestElections.test_get_results_by_year)
get_results_by_year should return the results for the given election year.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 171, in test_get_results_by_year
self.assertEqual({"Party 1": 1}, Elections.get_results_by_year(2026))
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: {'Party 1': 1} != <solution.Elections object at 0x7609f7afeba0>
======================================================================
FAIL: 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 165, in test_rig_elections_preferred_currency
self.assertEqual({"Party 2-Party 3": 21}, self.elections_2026.get_results())
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: {'Party 2-Party 3': 21} != {'Party 3-Party 2': 13}
- {'Party 2-Party 3': 21}
? ^ ^ -
+ {'Party 3-Party 2': 13}
? ^ ^ +
----------------------------------------------------------------------
Ran 21 tests in 0.002s
FAILED (failures=3)
| f | 1 | class Currency: | f | 1 | class Currency: |
| 2 | 2 | ||||
| 3 | def __init__(self, currency, count): | 3 | def __init__(self, currency, count): | ||
| 4 | self.currency = currency | 4 | self.currency = currency | ||
| 5 | self.count = count | 5 | self.count = count | ||
| 6 | 6 | ||||
| 7 | def __eq__(self, other): | 7 | def __eq__(self, other): | ||
| 8 | if not isinstance(other, Currency): | 8 | if not isinstance(other, Currency): | ||
| 9 | return False | 9 | return False | ||
| 10 | return self.currency == other.currency and self.count == other.count | 10 | return self.currency == other.currency and self.count == other.count | ||
| 11 | 11 | ||||
| 12 | 12 | ||||
| 13 | class PoliticalParty: | 13 | class PoliticalParty: | ||
| 14 | 14 | ||||
| 15 | def __init__(self, name, motto, members=None, preferred_currency = None): | 15 | def __init__(self, name, motto, members=None, preferred_currency = None): | ||
| n | 16 | if members is None: | n | ||
| 17 | members = [] | ||||
| 18 | self.name = name | 16 | self.name = name | ||
| 19 | self.__motto = motto | 17 | self.__motto = motto | ||
| n | 20 | self.members = members | n | 18 | self.members = members if members is not None else [] |
| 21 | self.preferred_currency = preferred_currency | 19 | self.preferred_currency = preferred_currency | ||
| 22 | 20 | ||||
| 23 | @property | 21 | @property | ||
| 24 | def motto(self): | 22 | def motto(self): | ||
| 25 | return self.__motto | 23 | return self.__motto | ||
| 26 | 24 | ||||
| 27 | def convert_currency_to_voters(self, count, currency): | 25 | def convert_currency_to_voters(self, count, currency): | ||
| 28 | coefficient_for_preferred_currency = 2 | 26 | coefficient_for_preferred_currency = 2 | ||
| 29 | if currency == self.preferred_currency: | 27 | if currency == self.preferred_currency: | ||
| 30 | return int((count / currency.count) * coefficient_for_preferred_currency) | 28 | return int((count / currency.count) * coefficient_for_preferred_currency) | ||
| 31 | return int(count / currency.count) | 29 | return int(count / currency.count) | ||
| 32 | 30 | ||||
| 33 | def __str__(self): | 31 | def __str__(self): | ||
| 34 | return self.name | 32 | return self.name | ||
| 35 | 33 | ||||
| 36 | def __add__(self, other): | 34 | def __add__(self, other): | ||
| 37 | return Coalition(self, other) | 35 | return Coalition(self, other) | ||
| 38 | 36 | ||||
| 39 | 37 | ||||
| 40 | class Coalition: | 38 | class Coalition: | ||
| 41 | 39 | ||||
| 42 | def __init__(self, *args): | 40 | def __init__(self, *args): | ||
| 43 | 41 | ||||
| 44 | self.members = {} | 42 | self.members = {} | ||
| 45 | self.parties = set() | 43 | self.parties = set() | ||
| 46 | for party in args: | 44 | for party in args: | ||
| 47 | self.members[party.name] = party.members | 45 | self.members[party.name] = party.members | ||
| 48 | self.parties.add(party) | 46 | self.parties.add(party) | ||
| 49 | 47 | ||||
| 50 | def __add__(self, other): | 48 | def __add__(self, other): | ||
| 51 | all_parties = set(self.parties) | 49 | all_parties = set(self.parties) | ||
| 52 | 50 | ||||
| 53 | if isinstance(other, Coalition): | 51 | if isinstance(other, Coalition): | ||
| 54 | all_parties.update(other.parties) | 52 | all_parties.update(other.parties) | ||
| 55 | if isinstance(other, PoliticalParty): | 53 | if isinstance(other, PoliticalParty): | ||
| 56 | all_parties.add(other) | 54 | all_parties.add(other) | ||
| 57 | return Coalition(*all_parties) | 55 | return Coalition(*all_parties) | ||
| 58 | 56 | ||||
| 59 | def __str__(self): | 57 | def __str__(self): | ||
| 60 | parties = list(self.parties) | 58 | parties = list(self.parties) | ||
| 61 | name = "" | 59 | name = "" | ||
| 62 | for party in parties: | 60 | for party in parties: | ||
| 63 | name += party.name + '-' | 61 | name += party.name + '-' | ||
| n | 64 | return name[0:len(name) - 1] | n | 62 | return name[: len(name) - 1] |
| 65 | 63 | ||||
| 66 | class Elections: | 64 | class Elections: | ||
| 67 | 65 | ||||
| 68 | __election_history = {} | 66 | __election_history = {} | ||
| 69 | 67 | ||||
| 70 | def __init__(self, year): | 68 | def __init__(self, year): | ||
| 71 | self.year = year | 69 | self.year = year | ||
| 72 | self.parties_and_votes = {} | 70 | self.parties_and_votes = {} | ||
| 73 | self.__add_election(self, year) | 71 | self.__add_election(self, year) | ||
| 74 | 72 | ||||
| 75 | def register_party_or_coalition(self, participant): | 73 | def register_party_or_coalition(self, participant): | ||
| 76 | 74 | ||||
| n | 77 | if self.parties_and_votes.__contains__(str(participant)): | n | 75 | if str(participant) in self.parties_and_votes: |
| 78 | return | 76 | return | ||
| 79 | self.parties_and_votes[str(participant)] = 0 | 77 | self.parties_and_votes[str(participant)] = 0 | ||
| 80 | 78 | ||||
| 81 | def vote(self, party): | 79 | def vote(self, party): | ||
| n | 82 | if not self.parties_and_votes.__contains__(str(party)): | n | 80 | if str(party) not in self.parties_and_votes: |
| 83 | return | 81 | return | ||
| 84 | self.parties_and_votes[str(party)] += 1 | 82 | self.parties_and_votes[str(party)] += 1 | ||
| 85 | 83 | ||||
| 86 | def rig_elections(self, party, amount, currency): | 84 | def rig_elections(self, party, amount, currency): | ||
| 87 | 85 | ||||
| n | 88 | if not self.parties_and_votes.__contains__(str(party)): | n | 86 | if str(party) not in self.parties_and_votes: |
| 89 | return | 87 | return | ||
| 90 | best_trade = None | 88 | best_trade = None | ||
| 91 | if isinstance(party, Coalition): | 89 | if isinstance(party, Coalition): | ||
| 92 | 90 | ||||
| 93 | for p in party.parties: | 91 | for p in party.parties: | ||
| 94 | best_trade = p | 92 | best_trade = p | ||
| 95 | if p.preferred_currency == currency.currency: | 93 | if p.preferred_currency == currency.currency: | ||
| 96 | break | 94 | break | ||
| 97 | 95 | ||||
| 98 | if isinstance(party, PoliticalParty): | 96 | if isinstance(party, PoliticalParty): | ||
| 99 | best_trade = party | 97 | best_trade = party | ||
| 100 | 98 | ||||
| 101 | self.parties_and_votes[str(party)] += best_trade.convert_currency_to_voters(amount, currency) | 99 | self.parties_and_votes[str(party)] += best_trade.convert_currency_to_voters(amount, currency) | ||
| 102 | 100 | ||||
| 103 | def get_results(self): | 101 | def get_results(self): | ||
| 104 | return dict(self.parties_and_votes) | 102 | return dict(self.parties_and_votes) | ||
| 105 | 103 | ||||
| 106 | @classmethod | 104 | @classmethod | ||
| 107 | def __add_election(cls,election, year): | 105 | def __add_election(cls,election, year): | ||
| 108 | cls.__election_history[year] = election | 106 | cls.__election_history[year] = election | ||
| 109 | 107 | ||||
| 110 | @classmethod | 108 | @classmethod | ||
| 111 | def get_results_by_year(cls, year): | 109 | def get_results_by_year(cls, year): | ||
| n | 112 | if not cls.__election_history.__contains__(year): | n | 110 | if year not in cls.__election_history: |
| 113 | return None | 111 | return None | ||
| 114 | return cls.__election_history[year] | 112 | return cls.__election_history[year] | ||
| 115 | 113 | ||||
| t | 116 | elections_2026 = Elections(2026) | t | ||
| 117 | |||||
| 118 | BSP = PoliticalParty("БСП", | ||||
| 119 | "Ще вдигнем пенсиите и тази година!", | ||||
| 120 | members=["Крум Еди-Кой-Си", "Атанас Еди-Кой-Си", "Онзи другия"]) | ||||
| 121 | |||||
| 122 | elections_2026.register_party_or_coalition(BSP) | ||||
| 123 | |||||
| 124 | |||||
| 125 | gold_bars = Currency("мини-златни кюлчета", 0.2) | ||||
| 126 | GERB = PoliticalParty("ГЕРБ", | ||||
| 127 | "Ту-тууу!", | ||||
| 128 | members=["Бат'", "Бойко", "и", "сам", "стига"], | ||||
| 129 | preferred_currency=gold_bars) | ||||
| 130 | SDS = PoliticalParty("СДС", | ||||
| 131 | "Ние също сме шокирани, че все още съществуваме...", | ||||
| 132 | members=["Румен, ама не Радев"]) | ||||
| 133 | |||||
| 134 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||