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

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

7 точки общо

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

 1class Currency:
 2
 3    def __init__(self, name, course):
 4        self.name = name
 5        self.course = course # kolko e nujno za da se kupi 1 glas
 6    
 7    def __eq__(self, other):
 8        if not isinstance(other, Currency):
 9            return False
10        return self.name == other.name and self.course == other.course
11
12class PoliticalParty:
13
14    def __init__(self, name, motto, members=None, preferred_currency=None):
15        self.name = name
16        self.motto_atr = motto
17        self.members = members if members else []
18        self.preferred_currency = preferred_currency
19    
20    @property
21    def motto(self): #?????
22        return self.motto_atr
23    
24    def convert_currency_to_voters(self, amount, currency):
25        voters = int(amount / currency.course)
26        if self.preferred_currency and currency == self.preferred_currency:
27            voters *= 2
28        return voters
29
30    def __str__(self):
31        return self.name
32    
33    def __add__(self, other):
34        return Coalition(self, other)
35    
36
37class Coalition:
38    
39    def __init__(self, *args):
40        self.args = list(args)
41
42    @property
43    def members(self):
44        party_members = {}
45        for party in self.args:
46            if party.members:
47                party_members[party.name] = party.members
48            else:
49                party_members[party.name]=[]
50        return party_members
51    
52    def __str__(self):
53        names_of_parties=[]
54        for party in self.args:
55            names_of_parties.append(party.name)
56        return "-".join(names_of_parties)
57    
58    def __add__(self, other):
59        if isinstance(other,PoliticalParty):
60            return Coalition(*(self.args + [other]))
61        if isinstance(other,Coalition):
62            return Coalition(*(self.args + other.args))
63    
64class Elections:
65
66    current_year_elections = {}
67    def __init__(self, year):
68        self.year = year
69        self.result = {}
70        Elections.current_year_elections[year] = self
71    
72    def register_party_or_coalition (self, party):
73        name = str(party)
74        self.result[name] = 0
75
76    def vote(self, party):
77        name = str(party)
78        self.result[name] += 1
79    
80    def rig_elections(self, party, amount, currency):
81        bought_votes = 0
82        if isinstance(party, PoliticalParty):
83            bought_votes = party.convert_currency_to_voters(amount, currency)
84        elif isinstance(party, Coalition):
85            more_votes = 0
86            for party_ in party.args:
87                votes = party_.convert_currency_to_voters(amount, currency)
88                if votes > more_votes:
89                    more_votes = votes
90            bought_votes = more_votes
91        self.result[str(party)] += bought_votes
92    
93    def get_results(self):
94        return self.result
95    
96    def get_results_by_year(year):
97        if year in Elections.current_year_elections:
98            return Elections.current_year_elections[year].result
99        return {}

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

OK

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