Домашни > Да върнем левчето обратно! > Решения > Решението на Стефан Иванов

Резултати
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.name == other.name and self.rate == other.rate
 10    
 11class PoliticalParty:
 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 = 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, total_amount, currency):
 23        voters_count = int(total_amount / currency.rate)
 24        if currency == self.preferred_currency:
 25            voters_count *= 2
 26        return int(voters_count)
 27    
 28    def __str__(self):
 29        return self.party_name
 30    
 31    def __add__(self, other):
 32        return Coalition(self, other)
 33    
 34class Coalition:
 35    def __init__(self, *parties):
 36        self.parties = list(parties)
 37
 38    @property
 39    def members(self):
 40        coalition_dict = {}
 41        for party in self.parties:
 42            coalition_dict[party.party_name] = party.members
 43        return coalition_dict
 44
 45    def __add__(self, other):
 46        new_coalition = list(self.parties)
 47        if type(other) == PoliticalParty:
 48            new_coalition.append(other)
 49        else:
 50            for party in other.parties:
 51                new_coalition.append(party)
 52        return Coalition(*new_coalition)
 53    
 54    def __str__(self):
 55        parties_names = [party.party_name for party in self.parties]
 56        return '-'.join(parties_names)
 57    
 58class Elections:
 59    all_elections = {}
 60
 61    def __init__(self, year):
 62        self.year = year
 63        self.participants = {}
 64        type(self).all_elections[year] = self
 65
 66    def register_party_or_coalition(self, participant):
 67        if participant not in self.participants:
 68            self.participants[participant] = 0
 69        #return self.participants
 70
 71    def vote(self, participant):
 72        if participant in self.participants:
 73            self.participants[participant] += 1
 74        #return self.participants
 75    
 76    def rig_elections(self, participant, amount, currency):
 77        bought_votes = 0
 78        if type(participant) == PoliticalParty:
 79            bought_votes = participant.convert_currency_to_voters(amount, currency)
 80        else:
 81            for party in participant.parties:
 82                current_votes = party.convert_currency_to_voters(amount, currency)
 83                bought_votes = max(bought_votes, current_votes)
 84
 85        if participant in self.participants:
 86            self.participants[participant] += bought_votes
 87        #return self.participants
 88    
 89    def get_results(self):
 90        results = {}
 91        for participant, votes in self.participants.items():
 92            results[str(participant)] = votes
 93        return results
 94    
 95    @classmethod
 96    def get_results_by_year(cls, year):
 97        if year not in cls.all_elections:
 98            return {}
 99        return cls.all_elections[year].get_results()
100    

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

OK

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