1class Currency:
2 def __init__(self, currency_name, ammount):
3 self.currency = currency_name
4 self.ammount = ammount
5
6 def __eq__(self, other):
7 if not isinstance(other, Currency): #прочетох, че е добра практика да се проверява
8 return False
9
10 return self.currency == other.currency and self.ammount == other.ammount
11
12
13class PoliticalParty:
14 def __init__(self, name, motto, members = None, preferred_currency = None):
15 self.party_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, ammount, other_currency):
25 if not isinstance(other_currency, Currency):
26 return 0
27
28 voters_count = int(round(ammount / other_currency.ammount, 10))
29
30 if self.preferred_currency == other_currency:
31 return voters_count * 2
32 return voters_count
33
34 def __str__(self):
35 return self.party_name
36
37 def __add__(self, other):
38 if isinstance(other, PoliticalParty):
39 return Coalition(self, other)
40 return None
41
42
43class Coalition():
44 def __init__(self, *args):
45 self.members = {}
46 self.parties = []
47 #пазя отделно партиите, за да мога да проверя за по удобна валута при купени (кхъм обичайни) изборите
48
49 for party in args:
50 if isinstance(party, PoliticalParty):
51 self.members[party.party_name] = party.members
52 self.parties.append(party)
53
54 def __add__(self, other):
55 new_coalition = Coalition()
56 new_coalition.members = self.members.copy()
57 new_coalition.parties = self.parties.copy()
58
59 if isinstance(other, PoliticalParty):
60 new_coalition.members[other.party_name] = other.members
61 new_coalition.parties.append(other)
62
63 elif isinstance(other, Coalition):
64 new_coalition.members.update(other.members)
65 new_coalition.parties.extend(other.parties)
66
67 return new_coalition
68
69 def __str__(self):
70 return '-'.join(self.members.keys())
71
72
73class Elections:
74 _elections_by_year = {}
75
76 def __init__(self, year):
77 self.year = year
78 self.candidates = {}
79 Elections._elections_by_year[year] = self
80
81 def register_party_or_coalition(self, to_add):
82 if isinstance(to_add, PoliticalParty) or isinstance(to_add, Coalition):
83 self.candidates[to_add] = 0
84 else:
85 return None
86
87 def vote(self, vote_for):
88 if vote_for in self.candidates:
89 self.candidates[vote_for] += 1
90 else:
91 return None
92 #бихте ли ми казали какво да връщам в такива случаи,
93 #или да очаквам, че ще говорим за exceptions в някакъв момента
94
95 def rig_elections(self, candidate, ammount, currency):
96 if not isinstance(currency, Currency):
97 return None
98
99 if candidate not in self.candidates:
100 return None
101
102 #тук добре ли е да има празен ред между if-а И elif-а с цел четимост
103 if isinstance(candidate, PoliticalParty):
104 self.candidates[candidate] += candidate.convert_currency_to_voters(ammount, currency)
105 elif isinstance(candidate, Coalition):
106 best_party = candidate.parties[0]
107 for party in candidate.parties:
108 if party.preferred_currency == currency:
109 best_party = party
110 break
111
112 self.candidates[candidate] += best_party.convert_currency_to_voters(ammount, currency)
113
114 def get_results(self):
115 #знам, че може и с dict comprehension, но смятам, че ще стане твърде сложно
116 res = {}
117
118 for candidate in self.candidates:
119 res[str(candidate)] = self.candidates[candidate]
120 return res
121
122 @classmethod
123 def get_results_by_year(cls, year):
124 election = cls._elections_by_year.get(year)
125 if election:
126 return election.get_results()
127 return f"Няма избори за година {year}."
.....................
----------------------------------------------------------------------
Ran 21 tests in 0.001s
OK
24.03.2026 07:59
24.03.2026 07:59
24.03.2026 08:00
24.03.2026 08:01
24.03.2026 08:03
24.03.2026 08:03
24.03.2026 08:04
24.03.2026 08:06
24.03.2026 08:06