Домашни > Предизборно ООП > Решения > Решението на Ариф Мехмедали

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

7 точки общо

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

 1class Currency:
 2    def __init__(self, name, amount):
 3        self.name = name
 4        self.amount = amount
 5
 6    def __eq__(self, other):
 7        return self.name == other.name and self.amount == other.amount
 8
 9
10class PoliticalParty:
11    def __init__(self, name, motto, members=None, preferred_currency=None):
12        self.name = name
13        self.deviz = motto
14        self.members = members if members else []
15        self.preferred_currency = preferred_currency
16
17    @property
18    def motto(self):
19        return self.deviz
20
21    @motto.setter
22    def motto(self, other):
23        print("ТЦ! Не може! Грешка.")
24        return "ТЦ! Не може! Грешка."
25
26    def convert_currency_to_voters(self, amount, currency):
27        voters = int(amount / currency.amount)
28        return (1 + (self.preferred_currency == currency)) * voters if self.preferred_currency else voters
29
30    def __str__(self):
31        return self.name
32
33    def __add__(self, other):
34        return Coalition(self, other)
35
36
37class Coalition:
38
39    def __init__(self, *args):
40        self.members = {party.name: party.members for party in args}
41        self.parties = list(args)
42
43    def __add__(self, other):
44        if type(other) == PoliticalParty:
45            return Coalition(*self.parties, other)
46        return Coalition(*self.parties, *other.parties)
47
48    def __str__(self):
49        return "-".join(self.members.keys())
50
51
52class Elections:
53    results_by_year = {}
54
55    def __init__(self, year):
56        self.year = year
57        self.votes = {}
58        Elections.results_by_year[year] = self.votes
59
60    def register_party_or_coalition(self, party):
61        if str(party) not in self.votes:
62            self.votes[str(party)] = 0
63
64    def vote(self, party):
65        self.votes[str(party)] += 1
66
67    def rig_elections(self, party, amount, name):
68        res = 0
69        if type(party) == PoliticalParty:
70            res = party.convert_currency_to_voters(amount, name)
71        elif type(party) == Coalition:
72            for p in party.parties:
73                res = max(res, p.convert_currency_to_voters(amount, name))
74        self.votes[str(party)] += res
75
76    def get_results(self):
77        return self.votes
78
79    @classmethod
80    def get_results_by_year(cls, year):
81        return cls.results_by_year.get(year)

.................F
Stdout:
ТЦ! Не може! Грешка.
...
======================================================================
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

Stdout:
ТЦ! Не може! Грешка.

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

FAILED (failures=1)

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