Домашни > Предизборно ООП > Решения > Решението на Кристиян Тодоров

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

7 точки общо

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

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

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

OK

Дискусия
История
Това решение има само една версия.