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

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

7 точки общо

21 успешни теста
0 неуспешни теста
Код (приемам критиките)

 1class Currency:
 2    def __init__(self, name: str, rate: float):
 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: str, motto: str, members: list[str] = None, preferred_currency: Currency = None):
12        self.name = name
13        self.__motto = motto
14
15        # Putting default = [] means its shared across all instances
16        self.members = members or []
17        self.preferred_currency = preferred_currency
18
19    @property
20    def motto(self):
21        return self.__motto
22
23    def convert_currency_to_voters(self, amount: float, currency: Currency):
24        result = int(amount / currency.rate)
25        return result * 2 if self.preferred_currency == currency else result
26
27    def __str__(self):
28        return self.name
29
30    def __add__(self, other: PoliticalParty):
31        return Coalition(self, other)
32
33
34class Coalition:
35    def __init__(self, *political_parties: PoliticalParty):
36        self.political_parties = political_parties
37
38    @property
39    def members(self):
40        return {party.name: party.members or [] for party in self.political_parties}
41
42    def __add__(self, other: Coalition | PoliticalParty):
43        if isinstance(other, PoliticalParty):
44            return Coalition(*self.political_parties, other)
45
46        return Coalition(*self.political_parties, *other.political_parties)
47
48    def __str__(self):
49        return "-".join(party.name for party in self.political_parties)
50
51
52class Elections:
53    __elections = {}
54
55    def __init__(self, year):
56        self.votes = {}
57        self.year = year
58        Elections.__elections[year] = self
59
60    def register_party_or_coalition(self, candidate: PoliticalParty | Coalition):
61        self.votes[str(candidate)] = 0
62
63    def vote(self, candidate: PoliticalParty | Coalition):
64        if str(candidate) in self.votes:
65            self.votes[str(candidate)] += 1
66
67    def rig_elections(self, candidate: PoliticalParty | Coalition, amount: float, currency: Currency):
68        converter = candidate
69
70        if isinstance(candidate, Coalition):
71            converter = candidate.political_parties[0]
72            for party in candidate.political_parties:
73                if party.preferred_currency == currency:
74                    converter = party
75                    break
76
77        self.votes[str(candidate)] += converter.convert_currency_to_voters(amount, currency)
78
79    def get_results(self):
80        return dict(self.votes)
81
82    @classmethod
83    def get_results_by_year(cls, year: int):
84        return cls.__elections[year].get_results()

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

OK

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

f1class Currency:f1class Currency:
2    def __init__(self, name: str, rate: float):2    def __init__(self, name: str, rate: float):
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
108
119
12class PoliticalParty:10class PoliticalParty:
13    def __init__(self, name: str, motto: str, members: list[str] = None, preferred_currency: Currency = None):11    def __init__(self, name: str, motto: str, members: list[str] = None, preferred_currency: Currency = None):
14        self.name = name12        self.name = name
15        self.__motto = motto13        self.__motto = motto
1614
17        # Putting default = [] means its shared across all instances15        # Putting default = [] means its shared across all instances
t18        self.members = members if members is not None else []t16        self.members = members or []
19        self.preferred_currency = preferred_currency17        self.preferred_currency = preferred_currency
2018
21    @property19    @property
22    def motto(self):20    def motto(self):
23        return self.__motto21        return self.__motto
2422
25    def convert_currency_to_voters(self, amount: float, currency: Currency):23    def convert_currency_to_voters(self, amount: float, currency: Currency):
26        result = int(amount / currency.rate)24        result = int(amount / currency.rate)
27        return result * 2 if self.preferred_currency == currency else result25        return result * 2 if self.preferred_currency == currency else result
2826
29    def __str__(self):27    def __str__(self):
30        return self.name28        return self.name
3129
32    def __add__(self, other: PoliticalParty):30    def __add__(self, other: PoliticalParty):
33        return Coalition(self, other)31        return Coalition(self, other)
3432
3533
36class Coalition:34class Coalition:
37    def __init__(self, *political_parties: PoliticalParty):35    def __init__(self, *political_parties: PoliticalParty):
38        self.political_parties = political_parties36        self.political_parties = political_parties
3937
40    @property38    @property
41    def members(self):39    def members(self):
42        return {party.name: party.members or [] for party in self.political_parties}40        return {party.name: party.members or [] for party in self.political_parties}
4341
44    def __add__(self, other: Coalition | PoliticalParty):42    def __add__(self, other: Coalition | PoliticalParty):
45        if isinstance(other, PoliticalParty):43        if isinstance(other, PoliticalParty):
46            return Coalition(*self.political_parties, other)44            return Coalition(*self.political_parties, other)
4745
48        return Coalition(*self.political_parties, *other.political_parties)46        return Coalition(*self.political_parties, *other.political_parties)
4947
50    def __str__(self):48    def __str__(self):
51        return "-".join(party.name for party in self.political_parties)49        return "-".join(party.name for party in self.political_parties)
5250
5351
54class Elections:52class Elections:
55    __elections = {}53    __elections = {}
5654
57    def __init__(self, year):55    def __init__(self, year):
58        self.votes = {}56        self.votes = {}
59        self.year = year57        self.year = year
60        Elections.__elections[year] = self58        Elections.__elections[year] = self
6159
62    def register_party_or_coalition(self, candidate: PoliticalParty | Coalition):60    def register_party_or_coalition(self, candidate: PoliticalParty | Coalition):
63        self.votes[str(candidate)] = 061        self.votes[str(candidate)] = 0
6462
65    def vote(self, candidate: PoliticalParty | Coalition):63    def vote(self, candidate: PoliticalParty | Coalition):
66        if str(candidate) in self.votes:64        if str(candidate) in self.votes:
67            self.votes[str(candidate)] += 165            self.votes[str(candidate)] += 1
6866
69    def rig_elections(self, candidate: PoliticalParty | Coalition, amount: float, currency: Currency):67    def rig_elections(self, candidate: PoliticalParty | Coalition, amount: float, currency: Currency):
70        converter = candidate68        converter = candidate
7169
72        if isinstance(candidate, Coalition):70        if isinstance(candidate, Coalition):
73            converter = candidate.political_parties[0]71            converter = candidate.political_parties[0]
74            for party in candidate.political_parties:72            for party in candidate.political_parties:
75                if party.preferred_currency == currency:73                if party.preferred_currency == currency:
76                    converter = party74                    converter = party
77                    break75                    break
7876
79        self.votes[str(candidate)] += converter.convert_currency_to_voters(amount, currency)77        self.votes[str(candidate)] += converter.convert_currency_to_voters(amount, currency)
8078
81    def get_results(self):79    def get_results(self):
82        return dict(self.votes)80        return dict(self.votes)
8381
84    @classmethod82    @classmethod
85    def get_results_by_year(cls, year: int):83    def get_results_by_year(cls, year: int):
86        return cls.__elections[year].get_results()84        return cls.__elections[year].get_results()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op