| f | import math | f | import math |
| | | |
| class Currency: | | class Currency: |
| def __init__(self, name, curs): | | def __init__(self, name, curs): |
| self.name = name | | self.name = name |
| self.curs = curs | | self.curs = curs |
| | | |
| def __eq__(self, other): | | def __eq__(self, other): |
| if not isinstance(other, Currency): | | if not isinstance(other, Currency): |
| return NotImplemented | | return NotImplemented |
| return self.name == other.name and self.curs == other.curs | | return self.name == other.name and self.curs == other.curs |
| | | |
| class PoliticalParty: | | class PoliticalParty: |
| def __init__(self, name, motto, members = None, preferred_currency : Currency = None): | | def __init__(self, name, motto, members = None, preferred_currency : Currency = None): |
| self.name = name | | self.name = name |
| self._motto = motto | | self._motto = motto |
| self.members = members if members is not None else [] | | self.members = members if members is not None else [] |
| 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, voters, currency: Currency): | | def convert_currency_to_voters(self, voters, currency: Currency): |
| quantity = math.floor(voters / currency.curs) | | quantity = math.floor(voters / currency.curs) |
| if self.preferred_currency is not None and currency == self.preferred_currency: | | if self.preferred_currency is not None and currency == self.preferred_currency: |
| quantity *= 2 | | quantity *= 2 |
| return quantity | | return quantity |
| | | |
| 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) |
| return NotImplemented | | return NotImplemented |
| | | |
| class Coalition: | | class Coalition: |
| def __init__(self, *parties : PoliticalParty): | | def __init__(self, *parties : PoliticalParty): |
| self.parties = list(parties) | | self.parties = list(parties) |
| | | |
| @property | | @property |
| def members(self): | | def members(self): |
| n | res = {} | n | res = {party.name: party.members for party in self.parties} |
| for party in self.parties: | | |
| res[party.name] = party.members | | |
| return res | | return res |
| | | |
| def __add__(self, other : PoliticalParty): | | def __add__(self, other : PoliticalParty): |
| if isinstance(other, PoliticalParty): | | if isinstance(other, PoliticalParty): |
| return Coalition(*self.parties, other) | | return Coalition(*self.parties, other) |
| if isinstance(other, Coalition): | | if isinstance(other, Coalition): |
| return Coalition(*self.parties, *other.parties) | | return Coalition(*self.parties, *other.parties) |
| return NotImplemented | | return NotImplemented |
| | | |
| def __str__(self): | | def __str__(self): |
| return "-".join(party.name for party in self.parties) | | return "-".join(party.name for party in self.parties) |
| | | |
| class Elections: | | class Elections: |
| all_results = {} | | all_results = {} |
| | | |
| def __init__(self, year): | | def __init__(self, year): |
| self.year = year | | self.year = year |
| if year not in Elections.all_results: | | if year not in Elections.all_results: |
| Elections.all_results[year] = {} | | Elections.all_results[year] = {} |
| self.results = Elections.all_results[year] | | self.results = Elections.all_results[year] |
| | | |
| def register_party_or_coalition(self, party): | | def register_party_or_coalition(self, party): |
| if party not in self.results: | | if party not in self.results: |
| self.results[party] = 0 | | self.results[party] = 0 |
| | | |
| def vote(self, party): | | def vote(self, party): |
| if party in self.results: | | if party in self.results: |
| self.results[party] += 1 | | self.results[party] += 1 |
| | | |
| def rig_elections(self, party, quantity, currency): | | def rig_elections(self, party, quantity, currency): |
| votes = 0 | | votes = 0 |
| if isinstance(party, PoliticalParty): | | if isinstance(party, PoliticalParty): |
| votes = party.convert_currency_to_voters(quantity, currency) | | votes = party.convert_currency_to_voters(quantity, currency) |
| if isinstance(party, Coalition): | | if isinstance(party, Coalition): |
| for p in party.parties: | | for p in party.parties: |
| votes = max(votes, p.convert_currency_to_voters(quantity, currency)) | | votes = max(votes, p.convert_currency_to_voters(quantity, currency)) |
| self.results[party] += votes | | self.results[party] += votes |
| | | |
| def get_results(self): | | def get_results(self): |
| t | res = {} | t | res = {str(p): votes for p,votes in self.results.items()} |
| for p, votes in self.results.items(): | | |
| res[str(p)] = votes | | |
| return res | | return res |
| | | |
| @classmethod | | @classmethod |
| def get_results_by_year(cls, year): | | def get_results_by_year(cls, year): |
| res = {} | | res = {} |
| if year in cls.all_results: | | if year in cls.all_results: |
| for p, votes in cls.all_results[year].items(): | | for p, votes in cls.all_results[year].items(): |
| res[str(p)] = votes | | res[str(p)] = votes |
| return res | | return res |
| | | |
24.03.2026 07:26
24.03.2026 07:33