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

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

7 точки общо

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

 1class Currency:
 2    def __init__(self, name, amount_per_vote):
 3        self.name = name
 4        self.amount_per_vote = amount_per_vote
 5
 6    def __eq__(self, other):
 7        if not isinstance(other, Currency):
 8            return False
 9        return self.name == other.name and self.amount_per_vote == other.amount_per_vote
10
11class PoliticalParty:
12    def __init__(self, name, motto, members=[], preferred_currency=None):
13        self.name = name
14        self.__motto = motto
15        self.members = members
16        self.preferred_currency = preferred_currency
17
18    @property
19    def motto(self):
20        return self.__motto
21
22    def convert_currency_to_voters(self, amount, currency):
23        votes = int(amount / currency.amount_per_vote)
24        if currency == self.preferred_currency:
25            return votes * 2
26        return votes
27
28    def __str__(self):
29        return self.name
30
31    def __add__(self, other):
32        return Coalition(self, other)
33
34class Coalition:
35    def __init__(self, *political_parties):
36        self.political_parties = list(political_parties)
37
38    @property
39    def members(self):
40        return {party.name: party.members for party in self.political_parties}    
41
42    def __add__(self, other):
43        if isinstance(other, PoliticalParty):
44            return Coalition(*self.political_parties, other)
45        if isinstance(other, Coalition):
46            return Coalition(*self.political_parties, *other.political_parties)
47    
48    def __str__(self):
49        return "-".join(str(political_party) for political_party in self.political_parties)
50
51class Elections:
52    _memory = {}
53
54    def __init__(self, year):
55        self.year = year
56        self.votes = {}
57        type(self)._memory[year] = self
58
59    def register_party_or_coalition(self, participant):
60        self.votes[participant] = 0
61
62    def vote(self, participant):
63        if participant in self.votes:
64            self.votes[participant] += 1
65
66    def rig_elections(self, participant, amount, currency):
67        if participant in self.votes:
68            if isinstance(participant, PoliticalParty):
69                votes = participant.convert_currency_to_voters(amount, currency)
70            if isinstance(participant, Coalition):
71                votes = max(political_party.convert_currency_to_voters(amount, currency) for political_party in participant.political_parties)
72            self.votes[participant] += votes
73    
74    def get_results(self):
75        return {str(participant): votes for participant, votes in self.votes.items()}
76
77    @classmethod
78    def get_results_by_year(cls, year):
79        if year in cls._memory:
80            return cls._memory[year].get_results()
81        return {}

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

OK

Дискусия
Виктор Бечев
22.03.2026 17:10

Чисто решение, браво.
История

f1class Currency:f1class Currency:
2    def __init__(self, name, amount_per_vote):2    def __init__(self, name, amount_per_vote):
3        self.name = name3        self.name = name
4        self.amount_per_vote = amount_per_vote4        self.amount_per_vote = amount_per_vote
55
6    def __eq__(self, other):6    def __eq__(self, other):
7        if not isinstance(other, Currency):7        if not isinstance(other, Currency):
8            return False8            return False
9        return self.name == other.name and self.amount_per_vote == other.amount_per_vote9        return self.name == other.name and self.amount_per_vote == other.amount_per_vote
1010
11class PoliticalParty:11class PoliticalParty:
12    def __init__(self, name, motto, members=[], preferred_currency=None):12    def __init__(self, name, motto, members=[], preferred_currency=None):
13        self.name = name13        self.name = name
14        self.__motto = motto14        self.__motto = motto
15        self.members = members15        self.members = members
16        self.preferred_currency = preferred_currency16        self.preferred_currency = preferred_currency
1717
18    @property18    @property
19    def motto(self):19    def motto(self):
20        return self.__motto20        return self.__motto
2121
22    def convert_currency_to_voters(self, amount, currency):22    def convert_currency_to_voters(self, amount, currency):
23        votes = int(amount / currency.amount_per_vote)23        votes = int(amount / currency.amount_per_vote)
24        if currency == self.preferred_currency:24        if currency == self.preferred_currency:
25            return votes * 225            return votes * 2
26        return votes26        return votes
2727
28    def __str__(self):28    def __str__(self):
29        return self.name29        return self.name
3030
31    def __add__(self, other):31    def __add__(self, other):
32        return Coalition(self, other)32        return Coalition(self, other)
3333
34class Coalition:34class Coalition:
35    def __init__(self, *political_parties):35    def __init__(self, *political_parties):
36        self.political_parties = list(political_parties)36        self.political_parties = list(political_parties)
3737
38    @property38    @property
39    def members(self):39    def members(self):
40        return {party.name: party.members for party in self.political_parties}    40        return {party.name: party.members for party in self.political_parties}    
4141
42    def __add__(self, other):42    def __add__(self, other):
43        if isinstance(other, PoliticalParty):43        if isinstance(other, PoliticalParty):
44            return Coalition(*self.political_parties, other)44            return Coalition(*self.political_parties, other)
45        if isinstance(other, Coalition):45        if isinstance(other, Coalition):
46            return Coalition(*self.political_parties, *other.political_parties)46            return Coalition(*self.political_parties, *other.political_parties)
47    47    
48    def __str__(self):48    def __str__(self):
49        return "-".join(str(political_party) for political_party in self.political_parties)49        return "-".join(str(political_party) for political_party in self.political_parties)
5050
51class Elections:51class Elections:
52    _memory = {}52    _memory = {}
5353
54    def __init__(self, year):54    def __init__(self, year):
55        self.year = year55        self.year = year
56        self.votes = {}56        self.votes = {}
57        type(self)._memory[year] = self57        type(self)._memory[year] = self
5858
59    def register_party_or_coalition(self, participant):59    def register_party_or_coalition(self, participant):
60        self.votes[participant] = 060        self.votes[participant] = 0
6161
62    def vote(self, participant):62    def vote(self, participant):
63        if participant in self.votes:63        if participant in self.votes:
64            self.votes[participant] += 164            self.votes[participant] += 1
6565
66    def rig_elections(self, participant, amount, currency):66    def rig_elections(self, participant, amount, currency):
67        if participant in self.votes:67        if participant in self.votes:
68            if isinstance(participant, PoliticalParty):68            if isinstance(participant, PoliticalParty):
69                votes = participant.convert_currency_to_voters(amount, currency)69                votes = participant.convert_currency_to_voters(amount, currency)
70            if isinstance(participant, Coalition):70            if isinstance(participant, Coalition):
71                votes = max(political_party.convert_currency_to_voters(amount, currency) for political_party in participant.political_parties)71                votes = max(political_party.convert_currency_to_voters(amount, currency) for political_party in participant.political_parties)
72            self.votes[participant] += votes72            self.votes[participant] += votes
73    73    
74    def get_results(self):74    def get_results(self):
75        return {str(participant): votes for participant, votes in self.votes.items()}75        return {str(participant): votes for participant, votes in self.votes.items()}
7676
77    @classmethod77    @classmethod
78    def get_results_by_year(cls, year):78    def get_results_by_year(cls, year):
79        if year in cls._memory:79        if year in cls._memory:
80            return cls._memory[year].get_results()80            return cls._memory[year].get_results()
81        return {}81        return {}
t82 t
83yes_Bulgaria = PoliticalParty("Да, България",
84                              "Ще щурмуваме всичко на север от Одрин!",
85                              members=["Божо", "Ивайло Мирчев"])
86DSB = PoliticalParty("ДСБ",
87                     "Помните ли Иван Костов?",
88                     members=["Не-Иван-Костов", "Пак не е Иван Костов"])
89 
90DB = Coalition(yes_Bulgaria, DSB)
91 
92PP = PoliticalParty("ПП", "Не сме сигурни все още...", members=["Асенката"])
93 
94yes_Bulgaria = PoliticalParty("Да, България",
95                              "Ще щурмуваме всичко на север от Одрин!",
96                              members=["Божо", "Ивайло Мирчев"])
97DSB = PoliticalParty("ДСБ",
98                     "Помните ли Иван Костов?",
99                     members=["Не-Иван-Костов", "Пак не е Иван Костов"])
100 
101DB = Coalition(yes_Bulgaria, DSB)
102 
103GERB = PoliticalParty("ГЕРБ",
104                      "Ту-тууу!",
105                      members=["Бат'", "Бойко", "и", "сам", "стига"])
106SDS = PoliticalParty("СДС",
107                     "Ние също сме шокирани, че все още съществуваме...",
108                     members=["Румен, ама не Радев"])
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op