Домашни > Предизборно ООП > Решения > Решението на Александър Ангелов

Резултати
7 точки от тестове
0 точки от учител

7 точки общо

21 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

  1class Currency:
  2
  3    def __init__(self, name_currency, exchange_rate):
  4        self.name = name_currency
  5        self.exchange_rate = exchange_rate
  6
  7    def __eq__(self, other):
  8        return self.name == other.name and self.exchange_rate == other.exchange_rate
  9
 10class PoliticalParty:
 11
 12    def __init__(self, party_name, motto, members=None, preferred_currency=None):
 13        self.party_name = party_name
 14        self._motto = motto
 15        self.members = list(members) if members is not None else []
 16        self.preferred_currency = preferred_currency
 17
 18    @property
 19    def motto(self):
 20        return self._motto
 21
 22    def convert_currency_to_voters(self, amount, currency):
 23        voters = int(amount / currency.exchange_rate)
 24
 25        if self.preferred_currency and self.preferred_currency == currency:
 26            voters *= 2
 27
 28        return voters
 29
 30    def __str__(self):
 31        return self.party_name
 32
 33    def __add__(self, other):
 34        if isinstance(other, PoliticalParty):
 35            return Coalition(self, other)
 36        return NotImplemented
 37
 38class Coalition:
 39
 40    def __init__(self, *parties):
 41        self.parties = list(parties)
 42
 43    @property
 44    def members(self):
 45        return {party.party_name: party.members for party in self.parties}
 46
 47    def __add__(self, other):
 48        if isinstance(other, PoliticalParty):
 49            return Coalition(*self.parties, other)
 50
 51        if isinstance(other, Coalition):
 52            return Coalition(*self.parties, *other.parties)
 53
 54        return NotImplemented
 55
 56    def __str__(self):
 57        return "-".join(str(party) for party in self.parties)
 58
 59class Elections:
 60
 61    elections_by_year = {}
 62
 63    def __init__(self, year):
 64        self.year = year
 65        self._participants = []
 66        self._results = {}
 67        Elections.elections_by_year[year] = self
 68
 69    def register_party_or_coalition(self, participant):
 70        self._participants.append(participant)
 71        name = str(participant)
 72        self._results[name] = self._results.get(name, 0)
 73
 74    def vote(self, participant):
 75        name = str(participant)
 76        self._results[name] = self._results.get(name, 0) + 1
 77
 78    def rig_elections(self, participant, amount, currency):
 79        name = str(participant)
 80
 81        if hasattr(participant, "parties"):
 82            bought_votes = 0
 83            for party in participant.parties:
 84                votes = party.convert_currency_to_voters(amount, currency)
 85                if votes > bought_votes:
 86                    bought_votes = votes
 87        else:
 88            bought_votes = participant.convert_currency_to_voters(amount, currency)
 89
 90        self._results[name] = self._results.get(name, 0) + bought_votes
 91
 92    def get_results(self):
 93        return self._results
 94
 95    @classmethod
 96    def get_results_by_year(cls, year):
 97        elections = cls.elections_by_year.get(year)
 98        if elections is None:
 99            return {}
100        return elections.get_results()

.....................
----------------------------------------------------------------------
Ran 21 tests in 0.001s

OK

Дискусия
Виктор Бечев
25.03.2026 11:42

Супер, много чисто и четимо решение.
История
Това решение има само една версия.