Домашни > Предизборно ООП > Решения > Решението на Велислава Спасова

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

7 точки общо

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

 1import math
 2
 3class Currency:
 4    def __init__(self, name, curs):
 5        self.name = name
 6        self.curs = curs
 7
 8    def __eq__(self, other):
 9        if not isinstance(other, Currency):
10            return NotImplemented
11        return self.name == other.name and self.curs == other.curs
12    
13class PoliticalParty:
14    def __init__(self, name, motto, members = None, preferred_currency : Currency = None):
15        self.name = name
16        self._motto = motto
17        self.members = members if members is not None else []
18        self.preferred_currency = preferred_currency
19    
20    @property
21    def motto(self):
22        return self._motto
23    
24    def convert_currency_to_voters(self, voters, currency: Currency):
25        quantity = math.floor(voters / currency.curs)
26        if self.preferred_currency is not None and currency == self.preferred_currency:
27            quantity *= 2
28        return quantity
29    
30    def __str__(self):
31        return self.name
32    
33    def __add__(self, other):
34        if isinstance(other, PoliticalParty):
35            return Coalition(self, other)
36        return NotImplemented
37    
38class Coalition:
39    def __init__(self, *parties : PoliticalParty):
40        self.parties = list(parties)
41
42    @property
43    def members(self):
44        res = {party.name: party.members for party in self.parties}
45        return res
46    
47    def __add__(self, other : PoliticalParty):
48        if isinstance(other, PoliticalParty):
49            return Coalition(*self.parties, other)
50        if isinstance(other, Coalition):
51            return Coalition(*self.parties, *other.parties)
52        return NotImplemented
53    
54    def __str__(self):
55        return "-".join(party.name for party in self.parties)
56    
57class Elections:
58    all_results = {}
59
60    def __init__(self, year):
61        self.year = year
62        if year not in Elections.all_results:
63            Elections.all_results[year] = {}
64        self.results = Elections.all_results[year]
65    
66    def register_party_or_coalition(self, party):
67        if party not in self.results:
68            self.results[party] = 0
69    
70    def vote(self, party):
71        if party in self.results:
72            self.results[party] += 1
73    
74    def rig_elections(self, party, quantity, currency):
75        votes = 0
76        if isinstance(party, PoliticalParty):
77            votes = party.convert_currency_to_voters(quantity, currency)
78        if isinstance(party, Coalition):
79            for p in party.parties:
80                votes = max(votes, p.convert_currency_to_voters(quantity, currency))
81        self.results[party] += votes
82    
83    def get_results(self):
84        res = {str(p): votes for p,votes in self.results.items()}
85        return res
86    
87    @classmethod
88    def get_results_by_year(cls, year):
89        res = {}
90        if year in cls.all_results:
91            for p, votes in cls.all_results[year].items():
92                res[str(p)] = votes
93        return res

...........F.........
======================================================================
FAIL: 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 165, in test_rig_elections_preferred_currency
self.assertEqual({"Party 2-Party 3": 21}, self.elections_2026.get_results())
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: {'Party 2-Party 3': 21} != {'Party 1': 5, 'Party 2-Party 3': 21}
- {'Party 2-Party 3': 21}
+ {'Party 1': 5, 'Party 2-Party 3': 21}
? ++++++++++++++

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

FAILED (failures=1)

Дискусия
История

f1import mathf1import math
22
3class Currency:3class Currency:
4    def __init__(self, name, curs):4    def __init__(self, name, curs):
5        self.name = name5        self.name = name
6        self.curs = curs6        self.curs = curs
77
8    def __eq__(self, other):8    def __eq__(self, other):
9        if not isinstance(other, Currency):9        if not isinstance(other, Currency):
10            return NotImplemented10            return NotImplemented
11        return self.name == other.name and self.curs == other.curs11        return self.name == other.name and self.curs == other.curs
12    12    
13class PoliticalParty:13class PoliticalParty:
14    def __init__(self, name, motto, members = None, preferred_currency : Currency = None):14    def __init__(self, name, motto, members = None, preferred_currency : Currency = None):
15        self.name = name15        self.name = name
16        self._motto = motto16        self._motto = motto
17        self.members = members if members is not None else []17        self.members = members if members is not None else []
18        self.preferred_currency = preferred_currency18        self.preferred_currency = preferred_currency
19    19    
20    @property20    @property
21    def motto(self):21    def motto(self):
22        return self._motto22        return self._motto
23    23    
24    def convert_currency_to_voters(self, voters, currency: Currency):24    def convert_currency_to_voters(self, voters, currency: Currency):
25        quantity = math.floor(voters / currency.curs)25        quantity = math.floor(voters / currency.curs)
26        if self.preferred_currency is not None and currency == self.preferred_currency:26        if self.preferred_currency is not None and currency == self.preferred_currency:
27            quantity *= 227            quantity *= 2
28        return quantity28        return quantity
29    29    
30    def __str__(self):30    def __str__(self):
31        return self.name31        return self.name
32    32    
33    def __add__(self, other):33    def __add__(self, other):
34        if isinstance(other, PoliticalParty):34        if isinstance(other, PoliticalParty):
35            return Coalition(self, other)35            return Coalition(self, other)
36        return NotImplemented36        return NotImplemented
37    37    
38class Coalition:38class Coalition:
39    def __init__(self, *parties : PoliticalParty):39    def __init__(self, *parties : PoliticalParty):
40        self.parties = list(parties)40        self.parties = list(parties)
4141
42    @property42    @property
43    def members(self):43    def members(self):
n44        res = {}n44        res = {party.name: party.members for party in self.parties}
45        for party in self.parties:
46            res[party.name] = party.members
47        return res45        return res
48    46    
49    def __add__(self, other : PoliticalParty):47    def __add__(self, other : PoliticalParty):
50        if isinstance(other, PoliticalParty):48        if isinstance(other, PoliticalParty):
51            return Coalition(*self.parties, other)49            return Coalition(*self.parties, other)
52        if isinstance(other, Coalition):50        if isinstance(other, Coalition):
53            return Coalition(*self.parties, *other.parties)51            return Coalition(*self.parties, *other.parties)
54        return NotImplemented52        return NotImplemented
55    53    
56    def __str__(self):54    def __str__(self):
57        return "-".join(party.name for party in self.parties)55        return "-".join(party.name for party in self.parties)
58    56    
59class Elections:57class Elections:
60    all_results = {}58    all_results = {}
6159
62    def __init__(self, year):60    def __init__(self, year):
63        self.year = year61        self.year = year
64        if year not in Elections.all_results:62        if year not in Elections.all_results:
65            Elections.all_results[year] = {}63            Elections.all_results[year] = {}
66        self.results = Elections.all_results[year]64        self.results = Elections.all_results[year]
67    65    
68    def register_party_or_coalition(self, party):66    def register_party_or_coalition(self, party):
69        if party not in self.results:67        if party not in self.results:
70            self.results[party] = 068            self.results[party] = 0
71    69    
72    def vote(self, party):70    def vote(self, party):
73        if party in self.results:71        if party in self.results:
74            self.results[party] += 172            self.results[party] += 1
75    73    
76    def rig_elections(self, party, quantity, currency):74    def rig_elections(self, party, quantity, currency):
77        votes = 075        votes = 0
78        if isinstance(party, PoliticalParty):76        if isinstance(party, PoliticalParty):
79            votes = party.convert_currency_to_voters(quantity, currency)77            votes = party.convert_currency_to_voters(quantity, currency)
80        if isinstance(party, Coalition):78        if isinstance(party, Coalition):
81            for p in party.parties:79            for p in party.parties:
82                votes = max(votes, p.convert_currency_to_voters(quantity, currency))80                votes = max(votes, p.convert_currency_to_voters(quantity, currency))
83        self.results[party] += votes81        self.results[party] += votes
84    82    
85    def get_results(self):83    def get_results(self):
t86        res = {}t84        res = {str(p): votes for p,votes in self.results.items()}
87        for p, votes in self.results.items():
88            res[str(p)] = votes
89        return res85        return res
90    86    
91    @classmethod87    @classmethod
92    def get_results_by_year(cls, year):88    def get_results_by_year(cls, year):
93        res = {}89        res = {}
94        if year in cls.all_results:90        if year in cls.all_results:
95            for p, votes in cls.all_results[year].items():91            for p, votes in cls.all_results[year].items():
96                res[str(p)] = votes92                res[str(p)] = votes
97        return res93        return res
9894
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op