| f | from collections import defaultdict | f | from collections import defaultdict |
| | | |
| class Currency: | | class Currency: |
| def __init__(self, name, exchange_rate): | | def __init__(self, name, exchange_rate): |
| self.name = name | | self.name = name |
| self.exchange_rate = exchange_rate | | self.exchange_rate = exchange_rate |
| | | |
| def __eq__(self, other): | | def __eq__(self, other): |
| return self.name == other.name and self.exchange_rate == other.exchange_rate | | return self.name == other.name and self.exchange_rate == other.exchange_rate |
| | | |
| | | |
| class PoliticalParty: | | class PoliticalParty: |
| def __init__(self, name, motto, *members, **preferred_currency): | | def __init__(self, name, motto, *members, **preferred_currency): |
| self.name = name | | self.name = name |
| self._motto = motto | | self._motto = motto |
| if "members" in preferred_currency: | | if "members" in preferred_currency: |
| self.members = preferred_currency.pop("members") | | self.members = preferred_currency.pop("members") |
| else: | | else: |
| self.members = list(members) | | self.members = list(members) |
| 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, currency): | | def convert_currency_to_voters(self, amount, currency): |
| effective_amount = amount | | effective_amount = amount |
| for preferred_currency in self.preferred_currency.values(): | | for preferred_currency in self.preferred_currency.values(): |
| if currency == preferred_currency: | | if currency == preferred_currency: |
| effective_amount = amount * 2 | | effective_amount = amount * 2 |
| t | voters = int(effective_amount // currency.exchange_rate) | t | voters = int(effective_amount / currency.exchange_rate) |
| return voters | | return voters |
| | | |
| def __str__(self): | | def __str__(self): |
| return self.name | | return self.name |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| if isinstance(other, PoliticalParty): | | if isinstance(other, PoliticalParty): |
| return Coalition(self, other) | | return Coalition(self, other) |
| elif isinstance(other, Coalition): | | elif isinstance(other, Coalition): |
| return Coalition(self, *other.parties) | | return Coalition(self, *other.parties) |
| | | |
| | | |
| class Coalition: | | class Coalition: |
| def __init__(self, *parties): | | def __init__(self, *parties): |
| self.parties = parties | | self.parties = parties |
| | | |
| @property | | @property |
| def members(self): | | def members(self): |
| members = defaultdict(list) | | members = defaultdict(list) |
| for party in self.parties: | | for party in self.parties: |
| members[party.name] = [] | | members[party.name] = [] |
| for member in party.members: | | for member in party.members: |
| members[party.name].append(member) | | members[party.name].append(member) |
| return members | | return members |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| if isinstance(other, PoliticalParty): | | if isinstance(other, PoliticalParty): |
| return Coalition(*self.parties, other) | | return Coalition(*self.parties, other) |
| elif isinstance(other, Coalition): | | elif isinstance(other, Coalition): |
| return Coalition(*self.parties, *other.parties) | | return Coalition(*self.parties, *other.parties) |
| | | |
| def __str__(self): | | def __str__(self): |
| names = [party.name for party in self.parties] | | names = [party.name for party in self.parties] |
| result = "" | | result = "" |
| result = "-".join(names) | | result = "-".join(names) |
| return result | | return result |
| | | |
| | | |
| class Elections: | | class Elections: |
| _election_archive = {} | | _election_archive = {} |
| | | |
| def __init__(self, year): | | def __init__(self, year): |
| self.year = year | | self.year = year |
| self.results = defaultdict(int) | | self.results = defaultdict(int) |
| Elections._election_archive[year] = self | | Elections._election_archive[year] = self |
| | | |
| def register_party_or_coalition(self, party_or_coalition): | | def register_party_or_coalition(self, party_or_coalition): |
| if isinstance(party_or_coalition, PoliticalParty): | | if isinstance(party_or_coalition, PoliticalParty): |
| self.results[str(party_or_coalition)] = 0 | | self.results[str(party_or_coalition)] = 0 |
| elif isinstance(party_or_coalition, Coalition): | | elif isinstance(party_or_coalition, Coalition): |
| self.results[str(party_or_coalition)] = 0 | | self.results[str(party_or_coalition)] = 0 |
| | | |
| def vote(self, party_or_coalition): | | def vote(self, party_or_coalition): |
| self.results[str(party_or_coalition)] += 1 | | self.results[str(party_or_coalition)] += 1 |
| | | |
| def rig_elections(self, party_or_coalition, amount, currency): | | def rig_elections(self, party_or_coalition, amount, currency): |
| coalition_voters = [] | | coalition_voters = [] |
| if isinstance(party_or_coalition, PoliticalParty): | | if isinstance(party_or_coalition, PoliticalParty): |
| self.results[str(party_or_coalition)] += party_or_coalition.convert_currency_to_voters(amount, currency) | | self.results[str(party_or_coalition)] += party_or_coalition.convert_currency_to_voters(amount, currency) |
| elif isinstance(party_or_coalition, Coalition): | | elif isinstance(party_or_coalition, Coalition): |
| for party in party_or_coalition.parties: | | for party in party_or_coalition.parties: |
| coalition_voters.append(party.convert_currency_to_voters(amount, currency)) | | coalition_voters.append(party.convert_currency_to_voters(amount, currency)) |
| self.results[str(party_or_coalition)] += max(coalition_voters) | | self.results[str(party_or_coalition)] += max(coalition_voters) |
| | | |
| def get_results(self): | | def get_results(self): |
| return dict(self.results) | | return dict(self.results) |
| | | |
| @classmethod | | @classmethod |
| def get_results_by_year(cls, year): | | def get_results_by_year(cls, year): |
| if year in cls._election_archive: | | if year in cls._election_archive: |
| return cls._election_archive[year].get_results() | | return cls._election_archive[year].get_results() |
| return {} | | return {} |
25.03.2026 11:56