1class Currency:
2 '''
3 Represents currencies used in election contexts.
4 '''
5 def __init__(self, name, exchange_rate):
6 '''
7 Initializes the currency.
8
9 :param name: Name of the currency
10 :param exchange_rate: Units required for 1 vote
11 '''
12 self.name = name
13 self.exchange_rate = exchange_rate
14
15 def __str__(self):
16 return f"Name: {self.name}, Exchange rate: {self.exchange_rate}"
17
18 def __eq__(self, other):
19 # Check if the other object is also a Currency type
20 if not isinstance(other, Currency):
21 return False
22
23 return self.name == other.name and self.exchange_rate == other.exchange_rate
24
25class PoliticalParty:
26 '''
27 Defines the properties and behavior of a political party.
28 '''
29 def __init__(self, name, motto, members=None, preferred_currency=None):
30 self.name = name
31 self._motto = motto
32 self.members = members
33 self.preferred_currency = preferred_currency
34
35 def __str__(self):
36 #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}"
37 return self.name
38
39 @property
40 def motto(self):
41 '''
42 Allows reading the motto via party.motto without modification
43 '''
44 return self._motto
45
46 def convert_currency_to_voters(self, amount, currency):
47 '''
48 Converts a given amount of currency into a number of voters.
49
50 If this is the party's preferred currency, its efficiency doubles,
51 allowing it to acquire twice as many votes.
52 '''
53 if self.preferred_currency == currency:
54 return int(amount / currency.exchange_rate) * 2
55 else:
56 return int(amount / currency.exchange_rate)
57
58 def __add__(self, other):
59 '''
60 Merges two parties to create and return a new Coalition instance.
61 '''
62 return Coalition(self, other)
63
64
65class Coalition:
66 '''
67 Represents an electoral coalition of political parties.
68 '''
69 def __init__(self, *parties):
70 self.parties = parties
71
72 def __str__(self):
73
74 names = [str(party) for party in self.parties]
75
76 return "-".join(names)
77
78 @property
79 def members(self):
80 return {party.name: party.members for party in self.parties}
81
82 def __add__(self, other):
83 '''
84 Merges two coalitions or a coalition with a party.
85 '''
86 if isinstance(other, PoliticalParty):
87 return Coalition(*self.parties, other)
88 elif isinstance(other, Coalition):
89 return Coalition(*self.parties, *other.parties)
90
91
92class Elections:
93 '''
94 Handles the organization and execution of the election process.
95 '''
96
97 all_elections = []
98
99 def __init__(self, year):
100 self.year = year
101 self.contestants = dict()
102 Elections.all_elections.append(self)
103
104 def register_party_or_coalition(self, contestant):
105 self.contestants[contestant] = 0
106
107 def vote(self, contestant):
108 self.contestants[contestant] += 1
109
110 def rig_elections(self, contestant, amount, currency):
111 amount_bought = 0
112
113 if isinstance(contestant, PoliticalParty):
114 amount_bought = contestant.convert_currency_to_voters(amount, currency)
115 else:
116 found = False
117 for party in contestant.parties:
118 if currency == party.preferred_currency:
119 found = True
120 amount_bought = party.convert_currency_to_voters(amount, currency)
121 break
122
123 if not found:
124 amount_bought = int(amount / currency.exchange_rate)
125
126 self.contestants[contestant] += amount_bought
127
128 def get_results(self):
129 return {str(contestant): votes for contestant, votes in self.contestants.items()}
130
131 @classmethod
132 def get_results_by_year(cls, year):
133 for election in cls.all_elections:
134 if election.year == year:
135 return election.get_results()
...F.................
======================================================================
FAIL: test_coalition_members_include_empty_lists (test.TestCoalition.test_coalition_members_include_empty_lists)
Coalition.members should return an empty list for parties without members.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 96, in test_coalition_members_include_empty_lists
self.assertEqual({"Party 1": [], "Party 2": ["a"]}, coalition.members)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: {'Party 1': [], 'Party 2': ['a']} != {'Party 1': None, 'Party 2': ['a']}
- {'Party 1': [], 'Party 2': ['a']}
? ^^
+ {'Party 1': None, 'Party 2': ['a']}
? ^^^^
----------------------------------------------------------------------
Ran 21 tests in 0.001s
FAILED (failures=1)
| t | 1 | class Currency: | t | 1 | class Currency: |
| 2 | ''' | 2 | ''' | ||
| 3 | Represents currencies used in election contexts. | 3 | Represents currencies used in election contexts. | ||
| 4 | ''' | 4 | ''' | ||
| 5 | def __init__(self, name, exchange_rate): | 5 | def __init__(self, name, exchange_rate): | ||
| 6 | ''' | 6 | ''' | ||
| 7 | Initializes the currency. | 7 | Initializes the currency. | ||
| 8 | 8 | ||||
| 9 | :param name: Name of the currency | 9 | :param name: Name of the currency | ||
| 10 | :param exchange_rate: Units required for 1 vote | 10 | :param exchange_rate: Units required for 1 vote | ||
| 11 | ''' | 11 | ''' | ||
| 12 | self.name = name | 12 | self.name = name | ||
| 13 | self.exchange_rate = exchange_rate | 13 | self.exchange_rate = exchange_rate | ||
| 14 | 14 | ||||
| 15 | def __str__(self): | 15 | def __str__(self): | ||
| 16 | return f"Name: {self.name}, Exchange rate: {self.exchange_rate}" | 16 | return f"Name: {self.name}, Exchange rate: {self.exchange_rate}" | ||
| 17 | 17 | ||||
| 18 | def __eq__(self, other): | 18 | def __eq__(self, other): | ||
| 19 | # Check if the other object is also a Currency type | 19 | # Check if the other object is also a Currency type | ||
| 20 | if not isinstance(other, Currency): | 20 | if not isinstance(other, Currency): | ||
| 21 | return False | 21 | return False | ||
| 22 | 22 | ||||
| 23 | return self.name == other.name and self.exchange_rate == other.exchange_rate | 23 | return self.name == other.name and self.exchange_rate == other.exchange_rate | ||
| 24 | 24 | ||||
| 25 | class PoliticalParty: | 25 | class PoliticalParty: | ||
| 26 | ''' | 26 | ''' | ||
| 27 | Defines the properties and behavior of a political party. | 27 | Defines the properties and behavior of a political party. | ||
| 28 | ''' | 28 | ''' | ||
| 29 | def __init__(self, name, motto, members=None, preferred_currency=None): | 29 | def __init__(self, name, motto, members=None, preferred_currency=None): | ||
| 30 | self.name = name | 30 | self.name = name | ||
| 31 | self._motto = motto | 31 | 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 __str__(self): | 35 | def __str__(self): | ||
| 36 | #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}" | 36 | #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}" | ||
| 37 | return self.name | 37 | return self.name | ||
| 38 | 38 | ||||
| 39 | @property | 39 | @property | ||
| 40 | def motto(self): | 40 | def motto(self): | ||
| 41 | ''' | 41 | ''' | ||
| 42 | Allows reading the motto via party.motto without modification | 42 | Allows reading the motto via party.motto without modification | ||
| 43 | ''' | 43 | ''' | ||
| 44 | return self._motto | 44 | return self._motto | ||
| 45 | 45 | ||||
| 46 | def convert_currency_to_voters(self, amount, currency): | 46 | def convert_currency_to_voters(self, amount, currency): | ||
| 47 | ''' | 47 | ''' | ||
| 48 | Converts a given amount of currency into a number of voters. | 48 | Converts a given amount of currency into a number of voters. | ||
| 49 | 49 | ||||
| 50 | If this is the party's preferred currency, its efficiency doubles, | 50 | If this is the party's preferred currency, its efficiency doubles, | ||
| 51 | allowing it to acquire twice as many votes. | 51 | allowing it to acquire twice as many votes. | ||
| 52 | ''' | 52 | ''' | ||
| 53 | if self.preferred_currency == currency: | 53 | if self.preferred_currency == currency: | ||
| 54 | return int(amount / currency.exchange_rate) * 2 | 54 | return int(amount / currency.exchange_rate) * 2 | ||
| 55 | else: | 55 | else: | ||
| 56 | return int(amount / currency.exchange_rate) | 56 | return int(amount / currency.exchange_rate) | ||
| 57 | 57 | ||||
| 58 | def __add__(self, other): | 58 | def __add__(self, other): | ||
| 59 | ''' | 59 | ''' | ||
| 60 | Merges two parties to create and return a new Coalition instance. | 60 | Merges two parties to create and return a new Coalition instance. | ||
| 61 | ''' | 61 | ''' | ||
| 62 | return Coalition(self, other) | 62 | return Coalition(self, other) | ||
| 63 | 63 | ||||
| 64 | 64 | ||||
| 65 | class Coalition: | 65 | class Coalition: | ||
| 66 | ''' | 66 | ''' | ||
| 67 | Represents an electoral coalition of political parties. | 67 | Represents an electoral coalition of political parties. | ||
| 68 | ''' | 68 | ''' | ||
| 69 | def __init__(self, *parties): | 69 | def __init__(self, *parties): | ||
| 70 | self.parties = parties | 70 | self.parties = parties | ||
| 71 | 71 | ||||
| 72 | def __str__(self): | 72 | def __str__(self): | ||
| 73 | 73 | ||||
| 74 | names = [str(party) for party in self.parties] | 74 | names = [str(party) for party in self.parties] | ||
| 75 | 75 | ||||
| 76 | return "-".join(names) | 76 | return "-".join(names) | ||
| 77 | 77 | ||||
| 78 | @property | 78 | @property | ||
| 79 | def members(self): | 79 | def members(self): | ||
| 80 | return {party.name: party.members for party in self.parties} | 80 | return {party.name: party.members for party in self.parties} | ||
| 81 | 81 | ||||
| 82 | def __add__(self, other): | 82 | def __add__(self, other): | ||
| 83 | ''' | 83 | ''' | ||
| 84 | Merges two coalitions or a coalition with a party. | 84 | Merges two coalitions or a coalition with a party. | ||
| 85 | ''' | 85 | ''' | ||
| 86 | if isinstance(other, PoliticalParty): | 86 | if isinstance(other, PoliticalParty): | ||
| 87 | return Coalition(*self.parties, other) | 87 | return Coalition(*self.parties, other) | ||
| 88 | elif isinstance(other, Coalition): | 88 | elif isinstance(other, Coalition): | ||
| 89 | return Coalition(*self.parties, *other.parties) | 89 | return Coalition(*self.parties, *other.parties) | ||
| 90 | 90 | ||||
| 91 | 91 | ||||
| 92 | class Elections: | 92 | class Elections: | ||
| 93 | ''' | 93 | ''' | ||
| 94 | Handles the organization and execution of the election process. | 94 | Handles the organization and execution of the election process. | ||
| 95 | ''' | 95 | ''' | ||
| 96 | 96 | ||||
| 97 | all_elections = [] | 97 | all_elections = [] | ||
| 98 | 98 | ||||
| 99 | def __init__(self, year): | 99 | def __init__(self, year): | ||
| 100 | self.year = year | 100 | self.year = year | ||
| 101 | self.contestants = dict() | 101 | self.contestants = dict() | ||
| 102 | Elections.all_elections.append(self) | 102 | Elections.all_elections.append(self) | ||
| 103 | 103 | ||||
| 104 | def register_party_or_coalition(self, contestant): | 104 | def register_party_or_coalition(self, contestant): | ||
| 105 | self.contestants[contestant] = 0 | 105 | self.contestants[contestant] = 0 | ||
| 106 | 106 | ||||
| 107 | def vote(self, contestant): | 107 | def vote(self, contestant): | ||
| 108 | self.contestants[contestant] += 1 | 108 | self.contestants[contestant] += 1 | ||
| 109 | 109 | ||||
| 110 | def rig_elections(self, contestant, amount, currency): | 110 | def rig_elections(self, contestant, amount, currency): | ||
| 111 | amount_bought = 0 | 111 | amount_bought = 0 | ||
| 112 | 112 | ||||
| 113 | if isinstance(contestant, PoliticalParty): | 113 | if isinstance(contestant, PoliticalParty): | ||
| 114 | amount_bought = contestant.convert_currency_to_voters(amount, currency) | 114 | amount_bought = contestant.convert_currency_to_voters(amount, currency) | ||
| 115 | else: | 115 | else: | ||
| 116 | found = False | 116 | found = False | ||
| 117 | for party in contestant.parties: | 117 | for party in contestant.parties: | ||
| 118 | if currency == party.preferred_currency: | 118 | if currency == party.preferred_currency: | ||
| 119 | found = True | 119 | found = True | ||
| 120 | amount_bought = party.convert_currency_to_voters(amount, currency) | 120 | amount_bought = party.convert_currency_to_voters(amount, currency) | ||
| 121 | break | 121 | break | ||
| 122 | 122 | ||||
| 123 | if not found: | 123 | if not found: | ||
| 124 | amount_bought = int(amount / currency.exchange_rate) | 124 | amount_bought = int(amount / currency.exchange_rate) | ||
| 125 | 125 | ||||
| 126 | self.contestants[contestant] += amount_bought | 126 | self.contestants[contestant] += amount_bought | ||
| 127 | 127 | ||||
| 128 | def get_results(self): | 128 | def get_results(self): | ||
| 129 | return {str(contestant): votes for contestant, votes in self.contestants.items()} | 129 | return {str(contestant): votes for contestant, votes in self.contestants.items()} | ||
| 130 | 130 | ||||
| 131 | @classmethod | 131 | @classmethod | ||
| 132 | def get_results_by_year(cls, year): | 132 | def get_results_by_year(cls, year): | ||
| 133 | for election in cls.all_elections: | 133 | for election in cls.all_elections: | ||
| 134 | if election.year == year: | 134 | if election.year == year: | ||
| 135 | return election.get_results() | 135 | return election.get_results() |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | class Currency: | f | 1 | class Currency: |
| 2 | ''' | 2 | ''' | ||
| 3 | Represents currencies used in election contexts. | 3 | Represents currencies used in election contexts. | ||
| 4 | ''' | 4 | ''' | ||
| 5 | def __init__(self, name, exchange_rate): | 5 | def __init__(self, name, exchange_rate): | ||
| 6 | ''' | 6 | ''' | ||
| 7 | Initializes the currency. | 7 | Initializes the currency. | ||
| 8 | 8 | ||||
| 9 | :param name: Name of the currency | 9 | :param name: Name of the currency | ||
| 10 | :param exchange_rate: Units required for 1 vote | 10 | :param exchange_rate: Units required for 1 vote | ||
| 11 | ''' | 11 | ''' | ||
| 12 | self.name = name | 12 | self.name = name | ||
| 13 | self.exchange_rate = exchange_rate | 13 | self.exchange_rate = exchange_rate | ||
| 14 | 14 | ||||
| 15 | def __str__(self): | 15 | def __str__(self): | ||
| 16 | return f"Name: {self.name}, Exchange rate: {self.exchange_rate}" | 16 | return f"Name: {self.name}, Exchange rate: {self.exchange_rate}" | ||
| 17 | 17 | ||||
| 18 | def __eq__(self, other): | 18 | def __eq__(self, other): | ||
| 19 | # Check if the other object is also a Currency type | 19 | # Check if the other object is also a Currency type | ||
| 20 | if not isinstance(other, Currency): | 20 | if not isinstance(other, Currency): | ||
| 21 | return False | 21 | return False | ||
| 22 | 22 | ||||
| 23 | return self.name == other.name and self.exchange_rate == other.exchange_rate | 23 | return self.name == other.name and self.exchange_rate == other.exchange_rate | ||
| 24 | 24 | ||||
| 25 | class PoliticalParty: | 25 | class PoliticalParty: | ||
| 26 | ''' | 26 | ''' | ||
| 27 | Defines the properties and behavior of a political party. | 27 | Defines the properties and behavior of a political party. | ||
| 28 | ''' | 28 | ''' | ||
| 29 | def __init__(self, name, motto, members=None, preferred_currency=None): | 29 | def __init__(self, name, motto, members=None, preferred_currency=None): | ||
| 30 | self.name = name | 30 | self.name = name | ||
| 31 | self._motto = motto | 31 | 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 __str__(self): | 35 | def __str__(self): | ||
| 36 | #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}" | 36 | #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}" | ||
| 37 | return self.name | 37 | return self.name | ||
| 38 | 38 | ||||
| 39 | @property | 39 | @property | ||
| 40 | def motto(self): | 40 | def motto(self): | ||
| 41 | ''' | 41 | ''' | ||
| 42 | Allows reading the motto via party.motto without modification | 42 | Allows reading the motto via party.motto without modification | ||
| 43 | ''' | 43 | ''' | ||
| 44 | return self._motto | 44 | return self._motto | ||
| 45 | 45 | ||||
| 46 | def convert_currency_to_voters(self, amount, currency): | 46 | def convert_currency_to_voters(self, amount, currency): | ||
| 47 | ''' | 47 | ''' | ||
| 48 | Converts a given amount of currency into a number of voters. | 48 | Converts a given amount of currency into a number of voters. | ||
| 49 | 49 | ||||
| 50 | If this is the party's preferred currency, its efficiency doubles, | 50 | If this is the party's preferred currency, its efficiency doubles, | ||
| 51 | allowing it to acquire twice as many votes. | 51 | allowing it to acquire twice as many votes. | ||
| 52 | ''' | 52 | ''' | ||
| 53 | if self.preferred_currency == currency: | 53 | if self.preferred_currency == currency: | ||
| 54 | return int(amount / currency.exchange_rate) * 2 | 54 | return int(amount / currency.exchange_rate) * 2 | ||
| 55 | else: | 55 | else: | ||
| 56 | return int(amount / currency.exchange_rate) | 56 | return int(amount / currency.exchange_rate) | ||
| 57 | 57 | ||||
| 58 | def __add__(self, other): | 58 | def __add__(self, other): | ||
| 59 | ''' | 59 | ''' | ||
| 60 | Merges two parties to create and return a new Coalition instance. | 60 | Merges two parties to create and return a new Coalition instance. | ||
| 61 | ''' | 61 | ''' | ||
| 62 | return Coalition(self, other) | 62 | return Coalition(self, other) | ||
| 63 | 63 | ||||
| 64 | 64 | ||||
| 65 | class Coalition: | 65 | class Coalition: | ||
| 66 | ''' | 66 | ''' | ||
| 67 | Represents an electoral coalition of political parties. | 67 | Represents an electoral coalition of political parties. | ||
| 68 | ''' | 68 | ''' | ||
| 69 | def __init__(self, *parties): | 69 | def __init__(self, *parties): | ||
| 70 | self.parties = parties | 70 | self.parties = parties | ||
| 71 | 71 | ||||
| 72 | def __str__(self): | 72 | def __str__(self): | ||
| 73 | 73 | ||||
| 74 | names = [str(party) for party in self.parties] | 74 | names = [str(party) for party in self.parties] | ||
| 75 | 75 | ||||
| 76 | return "-".join(names) | 76 | return "-".join(names) | ||
| 77 | 77 | ||||
| 78 | @property | 78 | @property | ||
| 79 | def members(self): | 79 | def members(self): | ||
| 80 | return {party.name: party.members for party in self.parties} | 80 | return {party.name: party.members for party in self.parties} | ||
| 81 | 81 | ||||
| 82 | def __add__(self, other): | 82 | def __add__(self, other): | ||
| 83 | ''' | 83 | ''' | ||
| 84 | Merges two coalitions or a coalition with a party. | 84 | Merges two coalitions or a coalition with a party. | ||
| 85 | ''' | 85 | ''' | ||
| 86 | if isinstance(other, PoliticalParty): | 86 | if isinstance(other, PoliticalParty): | ||
| 87 | return Coalition(*self.parties, other) | 87 | return Coalition(*self.parties, other) | ||
| 88 | elif isinstance(other, Coalition): | 88 | elif isinstance(other, Coalition): | ||
| 89 | return Coalition(*self.parties, *other.parties) | 89 | return Coalition(*self.parties, *other.parties) | ||
| 90 | 90 | ||||
| 91 | 91 | ||||
| 92 | class Elections: | 92 | class Elections: | ||
| 93 | ''' | 93 | ''' | ||
| 94 | Handles the organization and execution of the election process. | 94 | Handles the organization and execution of the election process. | ||
| 95 | ''' | 95 | ''' | ||
| 96 | 96 | ||||
| 97 | all_elections = [] | 97 | all_elections = [] | ||
| 98 | 98 | ||||
| 99 | def __init__(self, year): | 99 | def __init__(self, year): | ||
| 100 | self.year = year | 100 | self.year = year | ||
| 101 | self.contestants = dict() | 101 | self.contestants = dict() | ||
| 102 | Elections.all_elections.append(self) | 102 | Elections.all_elections.append(self) | ||
| 103 | 103 | ||||
| 104 | def register_party_or_coalition(self, contestant): | 104 | def register_party_or_coalition(self, contestant): | ||
| 105 | self.contestants[contestant] = 0 | 105 | self.contestants[contestant] = 0 | ||
| 106 | 106 | ||||
| 107 | def vote(self, contestant): | 107 | def vote(self, contestant): | ||
| 108 | self.contestants[contestant] += 1 | 108 | self.contestants[contestant] += 1 | ||
| 109 | 109 | ||||
| 110 | def rig_elections(self, contestant, amount, currency): | 110 | def rig_elections(self, contestant, amount, currency): | ||
| 111 | amount_bought = 0 | 111 | amount_bought = 0 | ||
| 112 | 112 | ||||
| 113 | if isinstance(contestant, PoliticalParty): | 113 | if isinstance(contestant, PoliticalParty): | ||
| 114 | amount_bought = contestant.convert_currency_to_voters(amount, currency) | 114 | amount_bought = contestant.convert_currency_to_voters(amount, currency) | ||
| 115 | else: | 115 | else: | ||
| 116 | found = False | 116 | found = False | ||
| 117 | for party in contestant.parties: | 117 | for party in contestant.parties: | ||
| 118 | if currency == party.preferred_currency: | 118 | if currency == party.preferred_currency: | ||
| 119 | found = True | 119 | found = True | ||
| 120 | amount_bought = party.convert_currency_to_voters(amount, currency) | 120 | amount_bought = party.convert_currency_to_voters(amount, currency) | ||
| 121 | break | 121 | break | ||
| 122 | 122 | ||||
| 123 | if not found: | 123 | if not found: | ||
| t | 124 | amount_bought = amount // currency.exchange_rate | t | 124 | amount_bought = int(amount / currency.exchange_rate) |
| 125 | 125 | ||||
| 126 | self.contestants[contestant] += amount_bought | 126 | self.contestants[contestant] += amount_bought | ||
| 127 | 127 | ||||
| 128 | def get_results(self): | 128 | def get_results(self): | ||
| 129 | return {str(contestant): votes for contestant, votes in self.contestants.items()} | 129 | return {str(contestant): votes for contestant, votes in self.contestants.items()} | ||
| 130 | 130 | ||||
| 131 | @classmethod | 131 | @classmethod | ||
| 132 | def get_results_by_year(cls, year): | 132 | def get_results_by_year(cls, year): | ||
| 133 | for election in cls.all_elections: | 133 | for election in cls.all_elections: | ||
| 134 | if election.year == year: | 134 | if election.year == year: | ||
| 135 | return election.get_results() | 135 | return election.get_results() |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
22.03.2026 11:46
22.03.2026 11:49
22.03.2026 11:51