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

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

7 точки общо

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

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

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

OK

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