Домашни > Предизборно ООП > Решения > Решението на Никита Симинкович

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

7 точки общо

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

 1class Currency:
 2    def __init__(self, name, amount_to_bribe):
 3        self.name =name
 4        self.amount_to_bribe = amount_to_bribe
 5
 6    def __eq__(self, other):
 7        if isinstance(other, type(self)):
 8            return self.amount_to_bribe == other.amount_to_bribe and self.name == other.name
 9        return False
10
11
12class PoliticalParty:
13    def __init__(self, party_name, motto, members=[], preferred_currency=None):
14        # този път фразата е на английски не на италиански :D
15        self.name = party_name
16        self.__motto = motto
17        self.members = members
18        self.preferred_currency = preferred_currency
19
20    # @property
21    # def motto(self):
22    #     return self._motto
23    
24    # @motto.setter
25    # def motto(self, value):
26    #     return "ТЦ! Не може! Грешка."
27
28    def convert_currency_to_voters(self, amount, currency: Currency):
29        return int(amount / currency.amount_to_bribe) * (2 if self.preferred_currency and self.preferred_currency == currency else 1)
30
31    def __getattr__(self, name: str):
32        if name == "motto":
33            return self.__motto
34        super().__getattr__(name)
35    
36    def __setattr__(self, name: str, value):
37        if name == "motto":
38            return "ТЦ! Не може! Грешка."
39        super().__setattr__(name, value)
40
41    def __str__(self):
42        return self.name
43    
44    def __add__(self, other: PoliticalParty | Coalition):
45        return Coalition(self, other) if isinstance(other, PoliticalParty) else Coalition(self, *other.parties)
46
47
48# GERB = PoliticalParty("ГЕРБ", "Ту-тууу!")
49# print(GERB.motto)  # "Ту-тууу!"
50# print(GERB.__setattr__("motto", "Дайте пак да се пробваме?"))  # ТЦ! Не може! Грешка.
51# print(GERB.motto)  # "Ту-тууу!"
52
53class Coalition:
54    def __init__(self, *political_parties):
55        self.parties = political_parties
56
57    @property
58    def members(self):
59        return {party.name: party.members for party in self.parties}
60
61    def __add__(self, other: PoliticalParty | Coalition):
62        return Coalition(*self.parties, other) if isinstance(other, PoliticalParty) else Coalition(*self.parties, *other.parties)
63
64    def __str__(self):
65        return "-".join([n for n in self.members.keys()])
66
67
68class Elections:
69    history: dict[int, Elections] = {}
70
71    def __init__(self, year):
72        self.year = year
73        self.candidates = {}
74        Elections.history[year] = self
75
76    def register_party_or_coalition(self, party_or_coalition: PoliticalParty | Coalition):
77        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0)
78
79    def vote(self, party_or_coalition: PoliticalParty | Coalition):
80        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + 1
81
82    def rig_elections(self, party_or_coalition: PoliticalParty | Coalition, amount, currency: Currency):
83        all_members = party_or_coalition.parties if isinstance(party_or_coalition, Coalition) else [party_or_coalition]
84        doubling_currency_party = next((c for c in all_members if c.preferred_currency == currency), None)
85        votes = doubling_currency_party.convert_currency_to_voters(amount, currency) if doubling_currency_party else all_members[0].convert_currency_to_voters(amount, currency)
86        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + votes
87
88    def get_results(self):
89        return {str(party_or_coalition): votes for party_or_coalition, votes in self.candidates.items()}
90
91    @classmethod
92    def get_results_by_year(cls, year):
93        return cls.history[year].get_results()
94    

.................F...
======================================================================
FAIL: test_motto_write (test.TestPoliticalParty.test_motto_write)
Motto should not be writable after the party is created.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 37, in test_motto_write
with self.assertRaises(Exception):
~~~~~~~~~~~~~~~~~^^^^^^^^^^^
AssertionError: Exception not raised

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

FAILED (failures=1)

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

Отвъд горното подобрение - изглежда доста добре.
История

n1from typing import Anyn
2 
3 
4class Currency:1class Currency:
5    def __init__(self, name, amount_to_bribe):2    def __init__(self, name, amount_to_bribe):
6        self.name =name3        self.name =name
7        self.amount_to_bribe = amount_to_bribe4        self.amount_to_bribe = amount_to_bribe
85
9    def __eq__(self, other):6    def __eq__(self, other):
10        if isinstance(other, type(self)):7        if isinstance(other, type(self)):
11            return self.amount_to_bribe == other.amount_to_bribe and self.name == other.name8            return self.amount_to_bribe == other.amount_to_bribe and self.name == other.name
12        return False9        return False
1310
1411
15class PoliticalParty:12class PoliticalParty:
16    def __init__(self, party_name, motto, members=[], preferred_currency=None):13    def __init__(self, party_name, motto, members=[], preferred_currency=None):
17        # този път фразата е на английски не на италиански :D14        # този път фразата е на английски не на италиански :D
18        self.name = party_name15        self.name = party_name
19        self.__motto = motto16        self.__motto = motto
20        self.members = members17        self.members = members
21        self.preferred_currency = preferred_currency18        self.preferred_currency = preferred_currency
2219
23    # @property20    # @property
24    # def motto(self):21    # def motto(self):
25    #     return self._motto22    #     return self._motto
26    23    
27    # @motto.setter24    # @motto.setter
28    # def motto(self, value):25    # def motto(self, value):
29    #     return "ТЦ! Не може! Грешка."26    #     return "ТЦ! Не може! Грешка."
3027
31    def convert_currency_to_voters(self, amount, currency: Currency):28    def convert_currency_to_voters(self, amount, currency: Currency):
32        return int(amount / currency.amount_to_bribe) * (2 if self.preferred_currency and self.preferred_currency == currency else 1)29        return int(amount / currency.amount_to_bribe) * (2 if self.preferred_currency and self.preferred_currency == currency else 1)
3330
34    def __getattr__(self, name: str):31    def __getattr__(self, name: str):
35        if name == "motto":32        if name == "motto":
36            return self.__motto33            return self.__motto
37        super().__getattr__(name)34        super().__getattr__(name)
38    35    
n39    def __setattr__(self, name: str, value: Any):n36    def __setattr__(self, name: str, value):
40        if name == "motto":37        if name == "motto":
41            return "ТЦ! Не може! Грешка."38            return "ТЦ! Не може! Грешка."
42        super().__setattr__(name, value)39        super().__setattr__(name, value)
4340
44    def __str__(self):41    def __str__(self):
45        return self.name42        return self.name
46    43    
47    def __add__(self, other: PoliticalParty | Coalition):44    def __add__(self, other: PoliticalParty | Coalition):
48        return Coalition(self, other) if isinstance(other, PoliticalParty) else Coalition(self, *other.parties)45        return Coalition(self, other) if isinstance(other, PoliticalParty) else Coalition(self, *other.parties)
4946
tt47 
48# GERB = PoliticalParty("ГЕРБ", "Ту-тууу!")
49# print(GERB.motto)  # "Ту-тууу!"
50# print(GERB.__setattr__("motto", "Дайте пак да се пробваме?"))  # ТЦ! Не може! Грешка.
51# print(GERB.motto)  # "Ту-тууу!"
5052
51class Coalition:53class Coalition:
52    def __init__(self, *political_parties):54    def __init__(self, *political_parties):
53        self.parties = political_parties55        self.parties = political_parties
5456
55    @property57    @property
56    def members(self):58    def members(self):
57        return {party.name: party.members for party in self.parties}59        return {party.name: party.members for party in self.parties}
5860
59    def __add__(self, other: PoliticalParty | Coalition):61    def __add__(self, other: PoliticalParty | Coalition):
60        return Coalition(*self.parties, other) if isinstance(other, PoliticalParty) else Coalition(*self.parties, *other.parties)62        return Coalition(*self.parties, other) if isinstance(other, PoliticalParty) else Coalition(*self.parties, *other.parties)
6163
62    def __str__(self):64    def __str__(self):
63        return "-".join([n for n in self.members.keys()])65        return "-".join([n for n in self.members.keys()])
6466
6567
66class Elections:68class Elections:
67    history: dict[int, Elections] = {}69    history: dict[int, Elections] = {}
6870
69    def __init__(self, year):71    def __init__(self, year):
70        self.year = year72        self.year = year
71        self.candidates = {}73        self.candidates = {}
72        Elections.history[year] = self74        Elections.history[year] = self
7375
74    def register_party_or_coalition(self, party_or_coalition: PoliticalParty | Coalition):76    def register_party_or_coalition(self, party_or_coalition: PoliticalParty | Coalition):
75        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0)77        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0)
7678
77    def vote(self, party_or_coalition: PoliticalParty | Coalition):79    def vote(self, party_or_coalition: PoliticalParty | Coalition):
78        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + 180        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + 1
7981
80    def rig_elections(self, party_or_coalition: PoliticalParty | Coalition, amount, currency: Currency):82    def rig_elections(self, party_or_coalition: PoliticalParty | Coalition, amount, currency: Currency):
81        all_members = party_or_coalition.parties if isinstance(party_or_coalition, Coalition) else [party_or_coalition]83        all_members = party_or_coalition.parties if isinstance(party_or_coalition, Coalition) else [party_or_coalition]
82        doubling_currency_party = next((c for c in all_members if c.preferred_currency == currency), None)84        doubling_currency_party = next((c for c in all_members if c.preferred_currency == currency), None)
83        votes = doubling_currency_party.convert_currency_to_voters(amount, currency) if doubling_currency_party else all_members[0].convert_currency_to_voters(amount, currency)85        votes = doubling_currency_party.convert_currency_to_voters(amount, currency) if doubling_currency_party else all_members[0].convert_currency_to_voters(amount, currency)
84        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + votes86        self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + votes
8587
86    def get_results(self):88    def get_results(self):
87        return {str(party_or_coalition): votes for party_or_coalition, votes in self.candidates.items()}89        return {str(party_or_coalition): votes for party_or_coalition, votes in self.candidates.items()}
8890
89    @classmethod91    @classmethod
90    def get_results_by_year(cls, year):92    def get_results_by_year(cls, year):
91        return cls.history[year].get_results()93        return cls.history[year].get_results()
92    94    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op