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

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

7 точки общо

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

 1class Currency:
 2    def __init__(self, name, rate):
 3        self.name = name
 4        self.rate = rate
 5    def __eq__(self, other):
 6        if isinstance(other, Currency):
 7            return self.name == other.name and self.rate == other.rate
 8
 9class PoliticalParty:
10    def __init__(self, name, motto, members = None, preferred_currency = None):
11        self.name = name
12        self._motto = motto
13        self.members = members if members is not None else []
14        self.preferred_currency = preferred_currency
15    @property
16    def motto(self):
17        return self._motto
18    def convert_currency_to_voters(self, amount, currency):
19        voters = int(amount / currency.rate)
20        if (currency == self.preferred_currency):
21            voters *= 2
22        return voters
23    def __str__(self):
24        return self.name
25    def __add__(self, other):
26        return Coalition(self, other)
27    
28class Coalition:
29    def __init__(self, *parties):
30        self.parties = list(parties)
31    @property
32    def members(self):
33        return {party.name : party.members for party in self.parties}
34    def __add__(self, other):
35        if isinstance(other, PoliticalParty):
36            return Coalition(*(self.parties + [other]))
37        elif isinstance(other, Coalition):
38            return Coalition(*(self.parties + other.parties))
39    def __str__(self):
40        return '-'.join(party.name for party in self.parties)
41
42class Elections:
43    _all_results = {}
44    def __init__(self, year):
45        self.year = year
46        self.registered = []
47        self.results = {}
48        Elections._all_results[self.year] = self.results
49    def register_party_or_coalition(self, other):
50        if other not in self.registered:
51            self.registered.append(other)
52            self.results[str(other)] = 0
53    def vote(self, other):
54        if other not in self.registered:
55            print(f"{str(other)} not registered!")
56        else:
57            self.results[str(other)] += 1    
58    def rig_elections(self, other, amount, currency):
59        if other not in self.registered:
60            return
61        max_voters = 0
62        if isinstance(other, Coalition):
63            for party in other.parties:
64                voters = party.convert_currency_to_voters(amount, currency)
65                if (voters > max_voters):
66                    max_voters = voters                   
67        elif other is not None:
68            max_voters = other.convert_currency_to_voters(amount, currency)
69        self.results[str(other)] += max_voters
70    def get_results(self):
71        return self.results
72    @staticmethod
73    def get_results_by_year(year):
74        if year in Elections._all_results:
75            return Elections._all_results[year]
76        else:
77            return {}
78           

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

OK

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