1class Currency:
2 def __init__(self, name, value):
3 self.name = name
4 self.value = value
5
6 def __eq__(self, other):
7 if not isinstance(other, Currency):
8 return False
9 return self.name == other.name and self.value == other.value
10
11
12class PoliticalParty:
13 def __init__(self, name, motto, members=None, preferred_currency=None):
14 self.name = name
15 self.__motto = motto
16 self.members = members if members is not None else []
17 self.preferred_currency = preferred_currency
18
19 @property
20 def motto(self):
21 return self.__motto
22
23 def convert_currency_to_voters(self, amount, currrency):
24 result = int(amount / currrency.value)
25 if currrency == self.preferred_currency:
26 result *= 2
27 return result
28
29 def __str__(self):
30 return self.name
31
32 def __add__(self, other):
33 if isinstance(other, PoliticalParty):
34 return Coalition(self, other)
35
36
37class Coalition:
38 def __init__(self, *politicalParties):
39 self.politicalParties = politicalParties
40
41 @property
42 def members(self):
43 return {party.name: party.members for party in self.politicalParties}
44
45 def __add__(self, other):
46 if isinstance(other, PoliticalParty):
47 return Coalition(*self.politicalParties, other)
48 elif isinstance(other, Coalition):
49 return Coalition(*self.politicalParties, *other.politicalParties)
50
51 def __str__(self):
52 return "-".join(str(party) for party in self.politicalParties)
53
54
55class Elections:
56 elections = {}
57
58 def __init__(self, year):
59 self.year = year
60 self.participants = {}
61 Elections.elections[year] = self
62
63 def register_party_or_coalition(self, participant):
64 if not participant in self.participants:
65 self.participants[participant] = 0
66
67 def vote(self, participant):
68 if participant in self.participants:
69 self.participants[participant] += 1
70
71 def rig_elections(self, participant, amount, currrency):
72 votes = 0
73 if isinstance(participant, PoliticalParty):
74 votes = participant.convert_currency_to_voters(amount, currrency)
75
76 elif isinstance(participant, Coalition):
77 for party in participant.politicalParties:
78 votes = max(votes, party.convert_currency_to_voters(amount, currrency))
79
80 if participant in self.participants:
81 self.participants[participant] += votes
82
83 def get_results(self):
84 return {
85 str(participant): votes for participant, votes in self.participants.items()
86 }
87
88 @classmethod
89 def get_results_by_year(cls, year):
90 if year in cls.elections:
91 return cls.elections[year].get_results()
92 return {}
.....................
----------------------------------------------------------------------
Ran 21 tests in 0.001s
OK
|
Костадин Русалов
24.03.2026 07:15Решението е добре написано.
|
24.03.2026 07:13
24.03.2026 07:13