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

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

7 точки общо

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

 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
 9class PoliticalParty:
10    def __init__(self, name, motto, members = None, preferred_currency = None):
11        self.name = name
12        self._motto = motto
13        self.members = members or []
14        self.preferred_currency = preferred_currency
15
16    @property
17    def motto(self):
18        return self._motto
19
20    def convert_currency_to_voters(self, amount, currency):
21        votes = int(amount / currency.rate)
22
23        if self.preferred_currency and currency == self.preferred_currency:
24            votes *=2
25
26        return votes
27
28    def __str__(self):
29        return self.name
30
31    def __add__(self, other):
32        if isinstance(other, PoliticalParty):
33            return Coalition(self, other)
34
35class Coalition:
36    def __init__(self, *parties):
37        self.parties = list(parties)
38
39    @property
40    def members(self):
41        return {party.name: party.members for party in self.parties}
42
43    def __add__(self, other):
44        if isinstance(other, PoliticalParty):
45            return Coalition(*self.parties, other)
46        elif isinstance(other, Coalition):
47            return Coalition(*self.parties, *other.parties)
48
49    def __str__(self):
50        return "-".join(p.name for p in self.parties)
51
52
53class Elections:
54    _all_results_by_year = {}
55
56    def __init__(self, year):
57        self.year = year
58        self.registered = []
59        self.votes = {}
60        Elections._all_results_by_year.setdefault(year, {})
61
62    def register_party_or_coalition(self, obj):
63        self.registered.append(obj)
64        self.votes[str(obj)] = 0
65
66    def vote(self, obj):
67        if str(obj) in self.votes:
68            self.votes[str(obj)] += 1
69
70    def rig_elections(self, obj, amount, currency):
71        votes = 0
72        if isinstance(obj, PoliticalParty):
73            votes = obj.convert_currency_to_voters(amount, currency)
74        elif isinstance(obj, Coalition):
75            votes = self.__votes_for_coalition(obj, amount, currency)
76        #else :
77            #throwing an exception, but we haven't taught them
78
79        if str(obj) in self.votes:
80            self.votes[str(obj)] += votes
81
82    def __votes_for_coalition(self, coalition, amount, currency):
83        votes = 0
84        for party in coalition.parties:
85            current = party.convert_currency_to_voters(amount, currency)
86            if party.preferred_currency and currency == party.preferred_currency:
87                votes = current
88                break
89            else:
90                votes = max(votes, current)
91        return votes
92
93    def get_results(self):
94        Elections._all_results_by_year[self.year] = self.votes.copy()
95        return self.votes
96
97    @classmethod
98    def get_results_by_year(cls, year):
99        return cls._all_results_by_year.get(year, {})

........F............
======================================================================
FAIL: test_get_results_by_year (test.TestElections.test_get_results_by_year)
get_results_by_year should return the results for the given election year.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 171, in test_get_results_by_year
self.assertEqual({"Party 1": 1}, Elections.get_results_by_year(2026))
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: {'Party 1': 1} != {}
- {'Party 1': 1}
+ {}

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

FAILED (failures=1)

Дискусия
Костадин Русалов
23.03.2026 12:40

Решението е чисто написано, а изключения ще учим скоро.
История
Това решение има само една версия.