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

Резултати
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        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 if preferred_currency is not None else Currency("",0)
15    
16    @property
17    def motto(self):
18        return self._motto
19    
20    def convert_currency_to_voters(self, count, currency):
21        return 2 * int((count / currency.rate)) if currency == self.preferred_currency else int(count / currency.rate)
22    
23    def __str__(self):
24        return self.name
25    
26    def __add__(self, other):
27        return Coalition(self, other)
28    
29class Coalition:
30    def __init__(self, *parties):
31        self.members = {}
32        self.parties = set()
33        for party in parties:
34            self.members[party.name] = party.members
35            self.parties.add(party)
36
37    def __add__(self, other):
38        result = Coalition()
39        result.members = self.members.copy()
40        result.parties = self.parties.copy()
41        if isinstance(other, PoliticalParty):
42            result.members[other.name] = other.members
43            result.parties.add(other)
44        elif isinstance(other, Coalition):
45            for name in other.members:
46                result.members[name] = other.members[name]
47            for party in other.parties:
48                result.parties.add(party)
49        return result
50    
51    def __str__(self):
52        return '-'.join(self.members.keys())
53
54class Elections:
55    all_elections = {}
56
57    def __init__(self, year):
58        self.year = year
59        self.votes = {}
60        Elections.all_elections[year] = self
61
62    def register_party_or_coalition(self, pc):
63        self.votes[pc] = 0
64
65    def vote(self, pc):
66        self.votes[pc] += 1
67
68    def rig_elections(self, pc, amount, currency):
69        if isinstance(pc, PoliticalParty):
70            self.votes[pc] += pc.convert_currency_to_voters(amount, currency)
71        elif isinstance(pc, Coalition):
72            max_votes = 0
73            for party in pc.parties:
74                if party.convert_currency_to_voters(amount, currency) > max_votes:
75                    max_votes = party.convert_currency_to_voters(amount, currency)
76            self.votes[pc] += max_votes
77
78    def get_results(self):
79        results = {}
80        for pc in self.votes:
81            results[str(pc)] = self.votes[pc]
82        return results
83
84    @staticmethod
85    def get_results_by_year(year):
86        if year in Elections.all_elections:
87            return Elections.all_elections[year].get_results()
88        return {}

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

OK

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