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

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

6 точки общо

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

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

................EF...
======================================================================
ERROR: test_motto (test.TestPoliticalParty.test_motto)
Motto should be accessible with the PoliticalParty.motto attribute.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 33, in test_motto
self.assertEqual("Some motto", self.party.motto)
^^^^^^^^^^^^^^^^
AttributeError: 'PoliticalParty' object has no attribute 'motto'

======================================================================
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, errors=1)

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