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

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

7 точки общо

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

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

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

OK

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