1class Currency:
2 def __init__(self, name, exchange_rate):
3 self.name = name
4 self.exchange_rate = exchange_rate
5
6 def __eq__(self, other):
7 return self.name == other.name and self.exchange_rate == other.exchange_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 = [] if members is None else members
15 self.preferred_currency = preferred_currency
16
17 def __str__(self):
18 return self.name
19
20 def __add__(self, other):
21 if isinstance(self, PoliticalParty) and isinstance(other, PoliticalParty):
22 return Coalition(self, other)
23 else:
24 return NotImplemented
25
26 @property
27 def motto(self):
28 return self.__motto
29
30 def convert_currency_to_voters(self, given_amount_currency, currency):
31 if self.preferred_currency is not None and self.preferred_currency == currency:
32 return int((given_amount_currency / currency.exchange_rate) * 2)
33 else:
34 return int(given_amount_currency / currency.exchange_rate)
35
36
37class Coalition:
38 def __init__(self, *parties):
39 self._parties = parties
40
41 def __str__(self):
42 return "-".join(self.members.keys())
43
44 def __add__(self, other):
45 if isinstance(self, Coalition) and isinstance(other, PoliticalParty):
46 return Coalition(*(self._parties + (other,)))
47 elif isinstance(self, Coalition) and isinstance(other, Coalition):
48 return Coalition(*(self._parties + other._parties))
49 else:
50 return NotImplemented
51
52 @property
53 def members(self):
54 return {party.name: party.members for party in self._parties}
55
56
57class Elections:
58 _elections_held_in_given_year = {}
59
60 def __init__(self, year):
61 self._year = year
62 self._results = {}
63 Elections._elections_held_in_given_year[year] = self
64
65 def register_party_or_coalition(self, party_or_coalition):
66 self._results.setdefault(party_or_coalition, 0)
67
68 def vote(self, party_or_coalition):
69 if party_or_coalition in self._results:
70 self._results[party_or_coalition] += 1
71
72 def rig_elections(self, party_or_coalition, given_amount_currency, currency):
73 if party_or_coalition in self._results:
74 if isinstance(party_or_coalition, PoliticalParty):
75 votes = party_or_coalition.convert_currency_to_voters(given_amount_currency, currency)
76 elif isinstance(party_or_coalition, Coalition):
77 votes = max(party.convert_currency_to_voters(given_amount_currency, currency) for party in
78 party_or_coalition._parties)
79 else:
80 return
81 self._results[party_or_coalition] += votes
82
83 def get_results(self):
84 return {str(party_or_coalition): votes for party_or_coalition, votes in self._results.items()}
85
86 @classmethod
87 def get_results_by_year(cls, year):
88 if year in cls._elections_held_in_given_year:
89 return {year: cls._elections_held_in_given_year[year].get_results()}
90 else:
91 return {}
........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} != {2026: {'Party 1': 1}}
- {'Party 1': 1}
+ {2026: {'Party 1': 1}}
? +++++++ +
----------------------------------------------------------------------
Ran 21 tests in 0.001s
FAILED (failures=1)
Виктор Бечев
25.03.2026 12:53Отвъд горния супер дребен коментар - кодът е чист. Има някои излишни проверки, за методи, за които сме казали, че няма да ви "мамим" с тестването - т.е. да им подаваме грешен вход, но това не пречи по никакъв начин на самото решение.
|
25.03.2026 12:49
25.03.2026 13:02
25.03.2026 13:05