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

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

5 точки общо

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

 1class Currency:
 2    def __init__(self, name_currency, rate_currency):
 3        self.name_currency = name_currency
 4        self.rate_currency = rate_currency
 5
 6    def __eq__(self, other):
 7        return (self.name_currency == other.name_currency
 8                and self.rate_currency == other.rate_currency)
 9
10class PoliticalParty:
11
12    def __init__(self, name, motto, members=None, preferred_currency=None):
13        self.name = name
14        self.__motto = motto
15        self.members = members if members is not None else []
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, currency):
23        voters = int(amount_currency / currency.rate_currency)
24
25        if currency == self.preferred_currency:
26            return 2 * voters
27
28        return voters
29
30    def __str__(self):
31        return self.name
32
33    def __add__(self, other):
34        return Coalition(self, other)
35
36class Coalition:
37
38    def __init__(self, *political_parties):
39        self.political_parties = political_parties
40
41    @property
42    def members(self):
43        return {party.name : party.members for party in self.political_parties}
44
45    def __add__(self, other):
46        if isinstance(other, PoliticalParty):
47            return Coalition(*self.political_parties, other)
48        elif isinstance(other, Coalition):
49            return Coalition(*self.political_parties, *other.political_parties)
50
51    def __str__(self):
52        return "-".join(party.name for party in self.political_parties)
53
54class Elections:
55    all_elections = []
56
57    def __init__(self, year):
58        self.year = year
59        self.participants = []
60        self.votes = {}
61        Elections.all_elections.append(self)
62
63    def register_party_or_coalition(self, party_or_coalition):
64        self.participants.append(party_or_coalition)
65        self.votes[str(party_or_coalition)] = 0
66
67    def vote(self, party_or_coalition):
68        self.votes[str(party_or_coalition)] += 1
69
70    def rig_elections(self, party_or_coalition, amount_currency, currency):
71        if isinstance(party_or_coalition, PoliticalParty):
72            self.votes[str(party_or_coalition)] += party_or_coalition.convert_currency_to_voters(amount_currency, currency)
73        elif isinstance(party_or_coalition, Coalition):
74            max_votes = 0
75            for party in party_or_coalition.political_parties:
76                current_votes = party.convert_currency_to_voters(amount_currency, currency)
77                if current_votes > max_votes:
78                    max_votes = current_votes
79
80            self.votes[str(party_or_coalition)] += max_votes
81
82    def get_results(self):
83        return self.votes
84
85    @classmethod
86    def get_results_by_year(cls, year):
87        for election in cls.all_elections:
88            if election.year == year:
89                return election.get_results()
90
91        return {}

..........EE.EEE.....
======================================================================
ERROR: test_rig_elections (test.TestElections.test_rig_elections)
rig_elections should add as many bought votes as possible to the result.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 157, in test_rig_elections
self.elections_2026.rig_elections(self.party, 50, self.currency)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 72, in rig_elections
self.votes[str(party_or_coalition)] += party_or_coalition.convert_currency_to_voters(amount_currency, currency)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 25, in convert_currency_to_voters
if currency == self.preferred_currency:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in __eq__
return (self.name_currency == other.name_currency
^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'name_currency'

======================================================================
ERROR: test_rig_elections_preferred_currency (test.TestElections.test_rig_elections_preferred_currency)
rig_elections should use the most effective preferred currency conversion in a coalition.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 163, in test_rig_elections_preferred_currency
self.elections_2026.rig_elections(self.coalition, 80, self.currency)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 76, in rig_elections
current_votes = party.convert_currency_to_voters(amount_currency, currency)
File "/tmp/solution.py", line 25, in convert_currency_to_voters
if currency == self.preferred_currency:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in __eq__
return (self.name_currency == other.name_currency
^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'name_currency'

======================================================================
ERROR: test_convert_currency_to_voters (test.TestPoliticalParty.test_convert_currency_to_voters)
convert_currency_to_voters should return normal or double voters depending on the preferred currency.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 42, in test_convert_currency_to_voters
self.assertEqual(4, self.party.convert_currency_to_voters(34, self.currency))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 25, in convert_currency_to_voters
if currency == self.preferred_currency:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in __eq__
return (self.name_currency == other.name_currency
^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'name_currency'

======================================================================
ERROR: test_convert_currency_to_voters_floating_point_edge_case (test.TestPoliticalParty.test_convert_currency_to_voters_floating_point_edge_case)
convert_currency_to_voters should properly account for floating point edge cases (e.g. 0.200001 errors).
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 55, in test_convert_currency_to_voters_floating_point_edge_case
self.assertEqual(5, party.convert_currency_to_voters(2, currency))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
File "/tmp/solution.py", line 25, in convert_currency_to_voters
if currency == self.preferred_currency:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in __eq__
return (self.name_currency == other.name_currency
^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'name_currency'

======================================================================
ERROR: test_convert_currency_to_voters_rounding (test.TestPoliticalParty.test_convert_currency_to_voters_rounding)
convert_currency_to_voters should round the number of voters down.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 49, in test_convert_currency_to_voters_rounding
self.assertEqual(4, self.party.convert_currency_to_voters(34.5, self.currency))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 25, in convert_currency_to_voters
if currency == self.preferred_currency:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in __eq__
return (self.name_currency == other.name_currency
^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'name_currency'

----------------------------------------------------------------------
Ran 21 tests in 0.003s

FAILED (errors=5)

Дискусия
История
Това решение има само една версия.