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

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

7 точки общо

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

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

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

OK

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