Домашни > Предизборно ООП > Решения > Решението на Александър Ангелов

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

6 точки общо

18 успешни теста
3 неуспешни теста
Код

  1class Currency:
  2
  3    def __init__(self, currency, count):
  4        self.currency = currency
  5        self.count = count
  6
  7    def __eq__(self, other):
  8        if not isinstance(other, Currency):
  9            return False
 10        return self.currency == other.currency and self.count == other.count
 11
 12
 13class PoliticalParty:
 14
 15    def __init__(self, name, motto, members=None, preferred_currency = None):
 16        self.name = name
 17        self.__motto = motto
 18        self.members = members if members is not None else []
 19        self.preferred_currency = preferred_currency
 20
 21    @property
 22    def motto(self):
 23        return self.__motto
 24
 25    def convert_currency_to_voters(self, count, currency):
 26        coefficient_for_preferred_currency = 2
 27        if currency == self.preferred_currency:
 28            return int((count / currency.count) * coefficient_for_preferred_currency)
 29        return int(count / currency.count)
 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
 40    def __init__(self, *args):
 41
 42        self.members = {}
 43        self.parties = set()
 44        for party in args:
 45            self.members[party.name] = party.members
 46            self.parties.add(party)
 47
 48    def __add__(self, other):
 49        all_parties = set(self.parties)
 50
 51        if isinstance(other, Coalition):
 52            all_parties.update(other.parties)
 53        if isinstance(other, PoliticalParty):
 54            all_parties.add(other)
 55        return Coalition(*all_parties)
 56
 57    def __str__(self):
 58        parties = list(self.parties)
 59        name = ""
 60        for party in parties:
 61            name += party.name + '-'
 62        return name[: len(name) - 1]
 63
 64class Elections:
 65
 66    __election_history = {}
 67
 68    def __init__(self, year):
 69        self.year = year
 70        self.parties_and_votes = {}
 71        self.__add_election(self, year)
 72
 73    def register_party_or_coalition(self, participant):
 74
 75        if str(participant) in self.parties_and_votes:
 76            return
 77        self.parties_and_votes[str(participant)] = 0
 78
 79    def vote(self, party):
 80        if str(party) not in self.parties_and_votes:
 81            return
 82        self.parties_and_votes[str(party)] += 1
 83
 84    def rig_elections(self, party, amount, currency):
 85
 86        if str(party) not in self.parties_and_votes:
 87            return
 88        best_trade = None
 89        if isinstance(party, Coalition):
 90
 91            for p in party.parties:
 92                best_trade = p
 93                if p.preferred_currency == currency.currency:
 94                    break
 95
 96        if isinstance(party, PoliticalParty):
 97            best_trade = party
 98
 99        self.parties_and_votes[str(party)] += best_trade.convert_currency_to_voters(amount, currency)
100
101    def get_results(self):
102        return dict(self.parties_and_votes)
103
104    @classmethod
105    def __add_election(cls,election,  year):
106        cls.__election_history[year] = election
107
108    @classmethod
109    def get_results_by_year(cls, year):
110        if year not in cls.__election_history:
111            return None
112        return cls.__election_history[year]

....F...F..F.........
======================================================================
FAIL: test_coalition_str (test.TestCoalition.test_coalition_str)
Coalition should convert to a dash-separated list of party names.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 128, in test_coalition_str
self.assertEqual("Party 1-Party 2", str(coalition))
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 'Party 1-Party 2' != 'Party 2-Party 1'
- Party 1-Party 2
? ^ ^
+ Party 2-Party 1
? ^ ^

======================================================================
FAIL: test_get_results_by_year (test.TestElections.test_get_results_by_year)
get_results_by_year should return the results for the given election year.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 171, in test_get_results_by_year
self.assertEqual({"Party 1": 1}, Elections.get_results_by_year(2026))
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: {'Party 1': 1} != <solution.Elections object at 0x7609f7afeba0>

======================================================================
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 3-Party 2': 13}
- {'Party 2-Party 3': 21}
? ^ ^ -

+ {'Party 3-Party 2': 13}
? ^ ^ +

----------------------------------------------------------------------
Ran 21 tests in 0.002s

FAILED (failures=3)

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

f1class Currency:f1class Currency:
22
3    def __init__(self, currency, count):3    def __init__(self, currency, count):
4        self.currency = currency4        self.currency = currency
5        self.count = count5        self.count = count
66
7    def __eq__(self, other):7    def __eq__(self, other):
8        if not isinstance(other, Currency):8        if not isinstance(other, Currency):
9            return False9            return False
10        return self.currency == other.currency and self.count == other.count10        return self.currency == other.currency and self.count == other.count
1111
1212
13class PoliticalParty:13class PoliticalParty:
1414
15    def __init__(self, name, motto, members=None, preferred_currency = None):15    def __init__(self, name, motto, members=None, preferred_currency = None):
n16        if members is None:n
17            members = []
18        self.name = name16        self.name = name
19        self.__motto = motto17        self.__motto = motto
n20        self.members = membersn18        self.members = members if members is not None else []
21        self.preferred_currency = preferred_currency19        self.preferred_currency = preferred_currency
2220
23    @property21    @property
24    def motto(self):22    def motto(self):
25        return self.__motto23        return self.__motto
2624
27    def convert_currency_to_voters(self, count, currency):25    def convert_currency_to_voters(self, count, currency):
28        coefficient_for_preferred_currency = 226        coefficient_for_preferred_currency = 2
29        if currency == self.preferred_currency:27        if currency == self.preferred_currency:
30            return int((count / currency.count) * coefficient_for_preferred_currency)28            return int((count / currency.count) * coefficient_for_preferred_currency)
31        return int(count / currency.count)29        return int(count / currency.count)
3230
33    def __str__(self):31    def __str__(self):
34        return self.name32        return self.name
3533
36    def __add__(self, other):34    def __add__(self, other):
37        return Coalition(self, other)35        return Coalition(self, other)
3836
3937
40class Coalition:38class Coalition:
4139
42    def __init__(self, *args):40    def __init__(self, *args):
4341
44        self.members = {}42        self.members = {}
45        self.parties = set()43        self.parties = set()
46        for party in args:44        for party in args:
47            self.members[party.name] = party.members45            self.members[party.name] = party.members
48            self.parties.add(party)46            self.parties.add(party)
4947
50    def __add__(self, other):48    def __add__(self, other):
51        all_parties = set(self.parties)49        all_parties = set(self.parties)
5250
53        if isinstance(other, Coalition):51        if isinstance(other, Coalition):
54            all_parties.update(other.parties)52            all_parties.update(other.parties)
55        if isinstance(other, PoliticalParty):53        if isinstance(other, PoliticalParty):
56            all_parties.add(other)54            all_parties.add(other)
57        return Coalition(*all_parties)55        return Coalition(*all_parties)
5856
59    def __str__(self):57    def __str__(self):
60        parties = list(self.parties)58        parties = list(self.parties)
61        name = ""59        name = ""
62        for party in parties:60        for party in parties:
63            name += party.name + '-'61            name += party.name + '-'
n64        return name[0:len(name) - 1]n62        return name[: len(name) - 1]
6563
66class Elections:64class Elections:
6765
68    __election_history = {}66    __election_history = {}
6967
70    def __init__(self, year):68    def __init__(self, year):
71        self.year = year69        self.year = year
72        self.parties_and_votes = {}70        self.parties_and_votes = {}
73        self.__add_election(self, year)71        self.__add_election(self, year)
7472
75    def register_party_or_coalition(self, participant):73    def register_party_or_coalition(self, participant):
7674
n77        if self.parties_and_votes.__contains__(str(participant)):n75        if str(participant) in self.parties_and_votes:
78            return76            return
79        self.parties_and_votes[str(participant)] = 077        self.parties_and_votes[str(participant)] = 0
8078
81    def vote(self, party):79    def vote(self, party):
n82        if not self.parties_and_votes.__contains__(str(party)):n80        if str(party) not in self.parties_and_votes:
83            return81            return
84        self.parties_and_votes[str(party)] += 182        self.parties_and_votes[str(party)] += 1
8583
86    def rig_elections(self, party, amount, currency):84    def rig_elections(self, party, amount, currency):
8785
n88        if not self.parties_and_votes.__contains__(str(party)):n86        if str(party) not in self.parties_and_votes:
89            return87            return
90        best_trade = None88        best_trade = None
91        if isinstance(party, Coalition):89        if isinstance(party, Coalition):
9290
93            for p in party.parties:91            for p in party.parties:
94                best_trade = p92                best_trade = p
95                if p.preferred_currency == currency.currency:93                if p.preferred_currency == currency.currency:
96                    break94                    break
9795
98        if isinstance(party, PoliticalParty):96        if isinstance(party, PoliticalParty):
99            best_trade = party97            best_trade = party
10098
101        self.parties_and_votes[str(party)] += best_trade.convert_currency_to_voters(amount, currency)99        self.parties_and_votes[str(party)] += best_trade.convert_currency_to_voters(amount, currency)
102100
103    def get_results(self):101    def get_results(self):
104        return dict(self.parties_and_votes)102        return dict(self.parties_and_votes)
105103
106    @classmethod104    @classmethod
107    def __add_election(cls,election,  year):105    def __add_election(cls,election,  year):
108        cls.__election_history[year] = election106        cls.__election_history[year] = election
109107
110    @classmethod108    @classmethod
111    def get_results_by_year(cls, year):109    def get_results_by_year(cls, year):
n112        if not cls.__election_history.__contains__(year):n110        if year not in cls.__election_history:
113            return None111            return None
114        return cls.__election_history[year]112        return cls.__election_history[year]
115113
t116elections_2026 = Elections(2026)t
117 
118BSP = PoliticalParty("БСП",
119                     "Ще вдигнем пенсиите и тази година!",
120                     members=["Крум Еди-Кой-Си", "Атанас Еди-Кой-Си", "Онзи другия"])
121 
122elections_2026.register_party_or_coalition(BSP)
123 
124 
125gold_bars = Currency("мини-златни кюлчета", 0.2)
126GERB = PoliticalParty("ГЕРБ",
127                      "Ту-тууу!",
128                      members=["Бат'", "Бойко", "и", "сам", "стига"],
129                      preferred_currency=gold_bars)
130SDS = PoliticalParty("СДС",
131                     "Ние също сме шокирани, че все още съществуваме...",
132                     members=["Румен, ама не Радев"])
133 
134 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op