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

Резултати
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 isinstance(other, Currency) and self.name == other.name and self.rate == other.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 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        voters = int(amount / currency.rate)
23        if self.preferred_currency == currency:
24            voters *= 2
25        return voters
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        return NotImplemented
34
35
36class Coalition:
37    def __init__(self, *parties):
38        self.parties = list(parties)
39
40    @property
41    def members(self):
42        return {party.name: party.members for party in self.parties}
43
44    def __add__(self, other):
45        if isinstance(other, PoliticalParty):
46            return Coalition(*self.parties, other)
47        if isinstance(other, Coalition):
48            return Coalition(*self.parties, *other.parties)
49        return NotImplemented
50
51    def __str__(self):
52        return "-".join([party.name for party in self.parties])
53
54
55class Elections:
56    _history = {}
57
58    def __init__(self, year):
59        self.year = year
60        self.result = {}
61        Elections._history[year] = self
62
63    def register_party_or_coalition(self, obj):
64        name = str(obj)
65        if name not in self.result:
66            self.result[name] = 0
67
68    def vote(self, obj):
69        name = str(obj)
70        self.result[name] += 1
71
72    def rig_elections(self, obj, amount, currency):
73        name = str(obj)
74        voters = 0
75
76        if isinstance(obj, PoliticalParty):
77            voters = obj.convert_currency_to_voters(amount, currency)
78        elif isinstance(obj, Coalition):
79            voters = max(
80                party.convert_currency_to_voters(amount, currency)
81                for party in obj.parties
82            )
83
84        self.result[name] += voters
85
86    def get_results(self):
87        return self.result
88
89    @classmethod
90    def get_results_by_year(cls, year):
91        if year in cls._history:
92            return cls._history[year].get_results()
93        return None

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

OK

Дискусия
История

f1class Currency:f1class Currency:
2    def __init__(self, name, rate):2    def __init__(self, name, rate):
3        self.name = name3        self.name = name
4        self.rate = rate4        self.rate = rate
55
6    def __eq__(self, other):6    def __eq__(self, other):
n7        if not isinstance(other, Currency):n
8            return False
9        return self.name == other.name and self.rate == other.rate7        return isinstance(other, Currency) and self.name == other.name and self.rate == other.rate
8 
109
11class PoliticalParty:10class PoliticalParty:
12    def __init__(self, name, motto, members=None, preferred_currency=None):11    def __init__(self, name, motto, members=None, preferred_currency=None):
13        self.name = name12        self.name = name
14        self._motto = motto13        self._motto = motto
15        self.members = members if members else []14        self.members = members if members else []
16        self.preferred_currency = preferred_currency15        self.preferred_currency = preferred_currency
1716
18    @property17    @property
19    def motto(self):18    def motto(self):
20        return self._motto19        return self._motto
2120
22    def convert_currency_to_voters(self, amount, currency):21    def convert_currency_to_voters(self, amount, currency):
23        voters = int(amount / currency.rate)22        voters = int(amount / currency.rate)
24        if self.preferred_currency == currency:23        if self.preferred_currency == currency:
25            voters *= 224            voters *= 2
26        return voters25        return voters
2726
28    def __str__(self):27    def __str__(self):
29        return self.name28        return self.name
3029
31    def __add__(self, other):30    def __add__(self, other):
32        if isinstance(other, PoliticalParty):31        if isinstance(other, PoliticalParty):
n33            return Coalition(self,other)n32            return Coalition(self, other)
34        return NotImplemented33        return NotImplemented
nn34 
3535
36class Coalition:36class Coalition:
37    def __init__(self, *parties):37    def __init__(self, *parties):
38        self.parties = list(parties)38        self.parties = list(parties)
3939
40    @property40    @property
41    def members(self):41    def members(self):
42        return {party.name: party.members for party in self.parties}42        return {party.name: party.members for party in self.parties}
4343
44    def __add__(self, other):44    def __add__(self, other):
45        if isinstance(other, PoliticalParty):45        if isinstance(other, PoliticalParty):
n46            return Coalition(*self.parties,other)n46            return Coalition(*self.parties, other)
47 
48        if isinstance(other, Coalition):47        if isinstance(other, Coalition):
49            return Coalition(*self.parties, *other.parties)48            return Coalition(*self.parties, *other.parties)
n50 n
51        return NotImplemented49        return NotImplemented
5250
53    def __str__(self):51    def __str__(self):
54        return "-".join([party.name for party in self.parties])52        return "-".join([party.name for party in self.parties])
nn53 
5554
56class Elections:55class Elections:
57    _history = {}56    _history = {}
5857
59    def __init__(self, year):58    def __init__(self, year):
60        self.year = year59        self.year = year
61        self.result = {}60        self.result = {}
62        Elections._history[year] = self61        Elections._history[year] = self
6362
64    def register_party_or_coalition(self, obj):63    def register_party_or_coalition(self, obj):
65        name = str(obj)64        name = str(obj)
66        if name not in self.result:65        if name not in self.result:
67            self.result[name] = 066            self.result[name] = 0
6867
69    def vote(self, obj):68    def vote(self, obj):
70        name = str(obj)69        name = str(obj)
71        self.result[name] += 170        self.result[name] += 1
7271
73    def rig_elections(self, obj, amount, currency):72    def rig_elections(self, obj, amount, currency):
74        name = str(obj)73        name = str(obj)
n75 n
76        voters = 074        voters = 0
7775
78        if isinstance(obj, PoliticalParty):76        if isinstance(obj, PoliticalParty):
79            voters = obj.convert_currency_to_voters(amount, currency)77            voters = obj.convert_currency_to_voters(amount, currency)
n80 n
81        elif isinstance(obj, Coalition):78        elif isinstance(obj, Coalition):
82            voters = max(79            voters = max(
83                party.convert_currency_to_voters(amount, currency)80                party.convert_currency_to_voters(amount, currency)
84                for party in obj.parties81                for party in obj.parties
85            )82            )
8683
87        self.result[name] += voters84        self.result[name] += voters
8885
89    def get_results(self):86    def get_results(self):
90        return self.result87        return self.result
9188
92    @classmethod89    @classmethod
93    def get_results_by_year(cls, year):90    def get_results_by_year(cls, year):
94        if year in cls._history:91        if year in cls._history:
95            return cls._history[year].get_results()92            return cls._history[year].get_results()
96        return None93        return None
t97 t
98 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op