| f | class Currency: | f | class Currency: |
| def __init__(self, name: str, rate: float): | | def __init__(self, name: str, rate: float): |
| self.name = name | | self.name = name |
| self.rate = rate | | self.rate = rate |
| | | |
| def __eq__(self, other): | | def __eq__(self, other): |
| n | if not isinstance(other, Currency): | n | |
| return False | | |
| return self.name == other.name and self.rate == other.rate | | return isinstance(other, Currency) and self.name == other.name and self.rate == other.rate |
| | | |
| | | |
| class PoliticalParty: | | class PoliticalParty: |
| def __init__(self, name: str, motto: str, members: list[str] = None, preferred_currency: Currency = None): | | def __init__(self, name: str, motto: str, members: list[str] = None, preferred_currency: Currency = None): |
| self.name = name | | self.name = name |
| self.__motto = motto | | self.__motto = motto |
| | | |
| # Putting default = [] means its shared across all instances | | # Putting default = [] means its shared across all instances |
| t | self.members = members if members is not None else [] | t | self.members = members or [] |
| self.preferred_currency = preferred_currency | | self.preferred_currency = preferred_currency |
| | | |
| @property | | @property |
| def motto(self): | | def motto(self): |
| return self.__motto | | return self.__motto |
| | | |
| def convert_currency_to_voters(self, amount: float, currency: Currency): | | def convert_currency_to_voters(self, amount: float, currency: Currency): |
| result = int(amount / currency.rate) | | result = int(amount / currency.rate) |
| return result * 2 if self.preferred_currency == currency else result | | return result * 2 if self.preferred_currency == currency else result |
| | | |
| def __str__(self): | | def __str__(self): |
| return self.name | | return self.name |
| | | |
| def __add__(self, other: PoliticalParty): | | def __add__(self, other: PoliticalParty): |
| return Coalition(self, other) | | return Coalition(self, other) |
| | | |
| | | |
| class Coalition: | | class Coalition: |
| def __init__(self, *political_parties: PoliticalParty): | | def __init__(self, *political_parties: PoliticalParty): |
| self.political_parties = political_parties | | self.political_parties = political_parties |
| | | |
| @property | | @property |
| def members(self): | | def members(self): |
| return {party.name: party.members or [] for party in self.political_parties} | | return {party.name: party.members or [] for party in self.political_parties} |
| | | |
| def __add__(self, other: Coalition | PoliticalParty): | | def __add__(self, other: Coalition | PoliticalParty): |
| if isinstance(other, PoliticalParty): | | if isinstance(other, PoliticalParty): |
| return Coalition(*self.political_parties, other) | | return Coalition(*self.political_parties, other) |
| | | |
| return Coalition(*self.political_parties, *other.political_parties) | | return Coalition(*self.political_parties, *other.political_parties) |
| | | |
| def __str__(self): | | def __str__(self): |
| return "-".join(party.name for party in self.political_parties) | | return "-".join(party.name for party in self.political_parties) |
| | | |
| | | |
| class Elections: | | class Elections: |
| __elections = {} | | __elections = {} |
| | | |
| def __init__(self, year): | | def __init__(self, year): |
| self.votes = {} | | self.votes = {} |
| self.year = year | | self.year = year |
| Elections.__elections[year] = self | | Elections.__elections[year] = self |
| | | |
| def register_party_or_coalition(self, candidate: PoliticalParty | Coalition): | | def register_party_or_coalition(self, candidate: PoliticalParty | Coalition): |
| self.votes[str(candidate)] = 0 | | self.votes[str(candidate)] = 0 |
| | | |
| def vote(self, candidate: PoliticalParty | Coalition): | | def vote(self, candidate: PoliticalParty | Coalition): |
| if str(candidate) in self.votes: | | if str(candidate) in self.votes: |
| self.votes[str(candidate)] += 1 | | self.votes[str(candidate)] += 1 |
| | | |
| def rig_elections(self, candidate: PoliticalParty | Coalition, amount: float, currency: Currency): | | def rig_elections(self, candidate: PoliticalParty | Coalition, amount: float, currency: Currency): |
| converter = candidate | | converter = candidate |
| | | |
| if isinstance(candidate, Coalition): | | if isinstance(candidate, Coalition): |
| converter = candidate.political_parties[0] | | converter = candidate.political_parties[0] |
| for party in candidate.political_parties: | | for party in candidate.political_parties: |
| if party.preferred_currency == currency: | | if party.preferred_currency == currency: |
| converter = party | | converter = party |
| break | | break |
| | | |
| self.votes[str(candidate)] += converter.convert_currency_to_voters(amount, currency) | | self.votes[str(candidate)] += converter.convert_currency_to_voters(amount, currency) |
| | | |
| def get_results(self): | | def get_results(self): |
| return dict(self.votes) | | return dict(self.votes) |
| | | |
| @classmethod | | @classmethod |
| def get_results_by_year(cls, year: int): | | def get_results_by_year(cls, year: int): |
| return cls.__elections[year].get_results() | | return cls.__elections[year].get_results() |