Домашни > Предизборно ООП > Решения > Решението на Никола Нейчев

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

7 точки общо

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

  1class Currency:
  2    '''
  3    Represents currencies used in election contexts.
  4    '''
  5    def __init__(self, name, exchange_rate): 
  6        '''
  7        Initializes the currency.
  8        
  9        :param name: Name of the currency
 10        :param exchange_rate: Units required for 1 vote
 11        '''
 12        self.name = name
 13        self.exchange_rate = exchange_rate
 14
 15    def __str__(self):
 16        return f"Name: {self.name}, Exchange rate: {self.exchange_rate}"
 17
 18    def __eq__(self, other):
 19        # Check if the other object is also a Currency type
 20        if not isinstance(other, Currency):
 21            return False
 22        
 23        return self.name == other.name and self.exchange_rate == other.exchange_rate
 24
 25class PoliticalParty:
 26    '''
 27    Defines the properties and behavior of a political party.
 28    '''
 29    def __init__(self, name, motto, members=None, preferred_currency=None):
 30        self.name = name
 31        self._motto = motto
 32        self.members = members
 33        self.preferred_currency = preferred_currency
 34
 35    def __str__(self):
 36        #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}"
 37        return self.name
 38
 39    @property
 40    def motto(self):
 41        '''
 42        Allows reading the motto via party.motto without modification
 43        '''
 44        return self._motto
 45
 46    def convert_currency_to_voters(self, amount, currency):
 47        '''
 48        Converts a given amount of currency into a number of voters.
 49
 50        If this is the party's preferred currency, its efficiency doubles, 
 51        allowing it to acquire twice as many votes.
 52        '''
 53        if self.preferred_currency == currency:
 54            return int(amount / currency.exchange_rate) * 2
 55        else:
 56            return int(amount / currency.exchange_rate)
 57
 58    def __add__(self, other):
 59        '''
 60        Merges two parties to create and return a new Coalition instance.
 61        '''
 62        return Coalition(self, other)
 63
 64    
 65class Coalition:
 66    '''
 67    Represents an electoral coalition of political parties.
 68    '''
 69    def __init__(self, *parties):
 70        self.parties = parties
 71
 72    def __str__(self):
 73
 74        names = [str(party) for party in self.parties]
 75
 76        return "-".join(names) 
 77
 78    @property
 79    def members(self):
 80        return {party.name: party.members for party in self.parties}
 81
 82    def __add__(self, other):
 83        '''
 84        Merges two coalitions or a coalition with a party.
 85        '''
 86        if isinstance(other, PoliticalParty):
 87            return Coalition(*self.parties, other)
 88        elif isinstance(other, Coalition):
 89            return Coalition(*self.parties, *other.parties)
 90        
 91
 92class Elections:
 93    '''
 94    Handles the organization and execution of the election process.
 95    '''
 96    
 97    all_elections = []
 98
 99    def __init__(self, year):
100        self.year = year
101        self.contestants = dict()
102        Elections.all_elections.append(self)
103
104    def register_party_or_coalition(self, contestant):
105        self.contestants[contestant] = 0
106
107    def vote(self, contestant):
108        self.contestants[contestant] += 1
109
110    def rig_elections(self, contestant, amount, currency):
111        amount_bought = 0
112        
113        if isinstance(contestant, PoliticalParty):
114            amount_bought = contestant.convert_currency_to_voters(amount, currency)
115        else:
116            found = False
117            for party in contestant.parties:
118                if currency == party.preferred_currency:
119                    found = True
120                    amount_bought = party.convert_currency_to_voters(amount, currency)
121                    break
122            
123            if not found:
124                amount_bought = int(amount / currency.exchange_rate)
125        
126        self.contestants[contestant] += amount_bought   
127
128    def get_results(self):
129        return {str(contestant): votes for contestant, votes in self.contestants.items()}
130    
131    @classmethod
132    def get_results_by_year(cls, year):
133        for election in cls.all_elections:
134            if election.year == year:
135                return election.get_results()

...F.................
======================================================================
FAIL: test_coalition_members_include_empty_lists (test.TestCoalition.test_coalition_members_include_empty_lists)
Coalition.members should return an empty list for parties without members.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 96, in test_coalition_members_include_empty_lists
self.assertEqual({"Party 1": [], "Party 2": ["a"]}, coalition.members)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: {'Party 1': [], 'Party 2': ['a']} != {'Party 1': None, 'Party 2': ['a']}
- {'Party 1': [], 'Party 2': ['a']}
? ^^

+ {'Party 1': None, 'Party 2': ['a']}
? ^^^^

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

FAILED (failures=1)

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

t1class Currency:t1class Currency:
2    '''2    '''
3    Represents currencies used in election contexts.3    Represents currencies used in election contexts.
4    '''4    '''
5    def __init__(self, name, exchange_rate): 5    def __init__(self, name, exchange_rate): 
6        '''6        '''
7        Initializes the currency.7        Initializes the currency.
8        8        
9        :param name: Name of the currency9        :param name: Name of the currency
10        :param exchange_rate: Units required for 1 vote10        :param exchange_rate: Units required for 1 vote
11        '''11        '''
12        self.name = name12        self.name = name
13        self.exchange_rate = exchange_rate13        self.exchange_rate = exchange_rate
1414
15    def __str__(self):15    def __str__(self):
16        return f"Name: {self.name}, Exchange rate: {self.exchange_rate}"16        return f"Name: {self.name}, Exchange rate: {self.exchange_rate}"
1717
18    def __eq__(self, other):18    def __eq__(self, other):
19        # Check if the other object is also a Currency type19        # Check if the other object is also a Currency type
20        if not isinstance(other, Currency):20        if not isinstance(other, Currency):
21            return False21            return False
22        22        
23        return self.name == other.name and self.exchange_rate == other.exchange_rate23        return self.name == other.name and self.exchange_rate == other.exchange_rate
2424
25class PoliticalParty:25class PoliticalParty:
26    '''26    '''
27    Defines the properties and behavior of a political party.27    Defines the properties and behavior of a political party.
28    '''28    '''
29    def __init__(self, name, motto, members=None, preferred_currency=None):29    def __init__(self, name, motto, members=None, preferred_currency=None):
30        self.name = name30        self.name = name
31        self._motto = motto31        self._motto = motto
32        self.members = members32        self.members = members
33        self.preferred_currency = preferred_currency33        self.preferred_currency = preferred_currency
3434
35    def __str__(self):35    def __str__(self):
36        #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}"36        #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}"
37        return self.name37        return self.name
3838
39    @property39    @property
40    def motto(self):40    def motto(self):
41        '''41        '''
42        Allows reading the motto via party.motto without modification42        Allows reading the motto via party.motto without modification
43        '''43        '''
44        return self._motto44        return self._motto
4545
46    def convert_currency_to_voters(self, amount, currency):46    def convert_currency_to_voters(self, amount, currency):
47        '''47        '''
48        Converts a given amount of currency into a number of voters.48        Converts a given amount of currency into a number of voters.
4949
50        If this is the party's preferred currency, its efficiency doubles, 50        If this is the party's preferred currency, its efficiency doubles, 
51        allowing it to acquire twice as many votes.51        allowing it to acquire twice as many votes.
52        '''52        '''
53        if self.preferred_currency == currency:53        if self.preferred_currency == currency:
54            return int(amount / currency.exchange_rate) * 254            return int(amount / currency.exchange_rate) * 2
55        else:55        else:
56            return int(amount / currency.exchange_rate)56            return int(amount / currency.exchange_rate)
5757
58    def __add__(self, other):58    def __add__(self, other):
59        '''59        '''
60        Merges two parties to create and return a new Coalition instance.60        Merges two parties to create and return a new Coalition instance.
61        '''61        '''
62        return Coalition(self, other)62        return Coalition(self, other)
6363
64    64    
65class Coalition:65class Coalition:
66    '''66    '''
67    Represents an electoral coalition of political parties.67    Represents an electoral coalition of political parties.
68    '''68    '''
69    def __init__(self, *parties):69    def __init__(self, *parties):
70        self.parties = parties70        self.parties = parties
7171
72    def __str__(self):72    def __str__(self):
7373
74        names = [str(party) for party in self.parties]74        names = [str(party) for party in self.parties]
7575
76        return "-".join(names) 76        return "-".join(names) 
7777
78    @property78    @property
79    def members(self):79    def members(self):
80        return {party.name: party.members for party in self.parties}80        return {party.name: party.members for party in self.parties}
8181
82    def __add__(self, other):82    def __add__(self, other):
83        '''83        '''
84        Merges two coalitions or a coalition with a party.84        Merges two coalitions or a coalition with a party.
85        '''85        '''
86        if isinstance(other, PoliticalParty):86        if isinstance(other, PoliticalParty):
87            return Coalition(*self.parties, other)87            return Coalition(*self.parties, other)
88        elif isinstance(other, Coalition):88        elif isinstance(other, Coalition):
89            return Coalition(*self.parties, *other.parties)89            return Coalition(*self.parties, *other.parties)
90        90        
9191
92class Elections:92class Elections:
93    '''93    '''
94    Handles the organization and execution of the election process.94    Handles the organization and execution of the election process.
95    '''95    '''
96    96    
97    all_elections = []97    all_elections = []
9898
99    def __init__(self, year):99    def __init__(self, year):
100        self.year = year100        self.year = year
101        self.contestants = dict()101        self.contestants = dict()
102        Elections.all_elections.append(self)102        Elections.all_elections.append(self)
103103
104    def register_party_or_coalition(self, contestant):104    def register_party_or_coalition(self, contestant):
105        self.contestants[contestant] = 0105        self.contestants[contestant] = 0
106106
107    def vote(self, contestant):107    def vote(self, contestant):
108        self.contestants[contestant] += 1108        self.contestants[contestant] += 1
109109
110    def rig_elections(self, contestant, amount, currency):110    def rig_elections(self, contestant, amount, currency):
111        amount_bought = 0111        amount_bought = 0
112        112        
113        if isinstance(contestant, PoliticalParty):113        if isinstance(contestant, PoliticalParty):
114            amount_bought = contestant.convert_currency_to_voters(amount, currency)114            amount_bought = contestant.convert_currency_to_voters(amount, currency)
115        else:115        else:
116            found = False116            found = False
117            for party in contestant.parties:117            for party in contestant.parties:
118                if currency == party.preferred_currency:118                if currency == party.preferred_currency:
119                    found = True119                    found = True
120                    amount_bought = party.convert_currency_to_voters(amount, currency)120                    amount_bought = party.convert_currency_to_voters(amount, currency)
121                    break121                    break
122            122            
123            if not found:123            if not found:
124                amount_bought = int(amount / currency.exchange_rate)124                amount_bought = int(amount / currency.exchange_rate)
125        125        
126        self.contestants[contestant] += amount_bought   126        self.contestants[contestant] += amount_bought   
127127
128    def get_results(self):128    def get_results(self):
129        return {str(contestant): votes for contestant, votes in self.contestants.items()}129        return {str(contestant): votes for contestant, votes in self.contestants.items()}
130    130    
131    @classmethod131    @classmethod
132    def get_results_by_year(cls, year):132    def get_results_by_year(cls, year):
133        for election in cls.all_elections:133        for election in cls.all_elections:
134            if election.year == year:134            if election.year == year:
135                return election.get_results()135                return election.get_results()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class Currency:f1class Currency:
2    '''2    '''
3    Represents currencies used in election contexts.3    Represents currencies used in election contexts.
4    '''4    '''
5    def __init__(self, name, exchange_rate): 5    def __init__(self, name, exchange_rate): 
6        '''6        '''
7        Initializes the currency.7        Initializes the currency.
8        8        
9        :param name: Name of the currency9        :param name: Name of the currency
10        :param exchange_rate: Units required for 1 vote10        :param exchange_rate: Units required for 1 vote
11        '''11        '''
12        self.name = name12        self.name = name
13        self.exchange_rate = exchange_rate13        self.exchange_rate = exchange_rate
1414
15    def __str__(self):15    def __str__(self):
16        return f"Name: {self.name}, Exchange rate: {self.exchange_rate}"16        return f"Name: {self.name}, Exchange rate: {self.exchange_rate}"
1717
18    def __eq__(self, other):18    def __eq__(self, other):
19        # Check if the other object is also a Currency type19        # Check if the other object is also a Currency type
20        if not isinstance(other, Currency):20        if not isinstance(other, Currency):
21            return False21            return False
22        22        
23        return self.name == other.name and self.exchange_rate == other.exchange_rate23        return self.name == other.name and self.exchange_rate == other.exchange_rate
2424
25class PoliticalParty:25class PoliticalParty:
26    '''26    '''
27    Defines the properties and behavior of a political party.27    Defines the properties and behavior of a political party.
28    '''28    '''
29    def __init__(self, name, motto, members=None, preferred_currency=None):29    def __init__(self, name, motto, members=None, preferred_currency=None):
30        self.name = name30        self.name = name
31        self._motto = motto31        self._motto = motto
32        self.members = members32        self.members = members
33        self.preferred_currency = preferred_currency33        self.preferred_currency = preferred_currency
3434
35    def __str__(self):35    def __str__(self):
36        #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}"36        #return f"Name: {self.name}, Motto: {self.motto}, Members: {self.members}, Preferred currency: {self.preferred_currency}"
37        return self.name37        return self.name
3838
39    @property39    @property
40    def motto(self):40    def motto(self):
41        '''41        '''
42        Allows reading the motto via party.motto without modification42        Allows reading the motto via party.motto without modification
43        '''43        '''
44        return self._motto44        return self._motto
4545
46    def convert_currency_to_voters(self, amount, currency):46    def convert_currency_to_voters(self, amount, currency):
47        '''47        '''
48        Converts a given amount of currency into a number of voters.48        Converts a given amount of currency into a number of voters.
4949
50        If this is the party's preferred currency, its efficiency doubles, 50        If this is the party's preferred currency, its efficiency doubles, 
51        allowing it to acquire twice as many votes.51        allowing it to acquire twice as many votes.
52        '''52        '''
53        if self.preferred_currency == currency:53        if self.preferred_currency == currency:
54            return int(amount / currency.exchange_rate) * 254            return int(amount / currency.exchange_rate) * 2
55        else:55        else:
56            return int(amount / currency.exchange_rate)56            return int(amount / currency.exchange_rate)
5757
58    def __add__(self, other):58    def __add__(self, other):
59        '''59        '''
60        Merges two parties to create and return a new Coalition instance.60        Merges two parties to create and return a new Coalition instance.
61        '''61        '''
62        return Coalition(self, other)62        return Coalition(self, other)
6363
64    64    
65class Coalition:65class Coalition:
66    '''66    '''
67    Represents an electoral coalition of political parties.67    Represents an electoral coalition of political parties.
68    '''68    '''
69    def __init__(self, *parties):69    def __init__(self, *parties):
70        self.parties = parties70        self.parties = parties
7171
72    def __str__(self):72    def __str__(self):
7373
74        names = [str(party) for party in self.parties]74        names = [str(party) for party in self.parties]
7575
76        return "-".join(names) 76        return "-".join(names) 
7777
78    @property78    @property
79    def members(self):79    def members(self):
80        return {party.name: party.members for party in self.parties}80        return {party.name: party.members for party in self.parties}
8181
82    def __add__(self, other):82    def __add__(self, other):
83        '''83        '''
84        Merges two coalitions or a coalition with a party.84        Merges two coalitions or a coalition with a party.
85        '''85        '''
86        if isinstance(other, PoliticalParty):86        if isinstance(other, PoliticalParty):
87            return Coalition(*self.parties, other)87            return Coalition(*self.parties, other)
88        elif isinstance(other, Coalition):88        elif isinstance(other, Coalition):
89            return Coalition(*self.parties, *other.parties)89            return Coalition(*self.parties, *other.parties)
90        90        
9191
92class Elections:92class Elections:
93    '''93    '''
94    Handles the organization and execution of the election process.94    Handles the organization and execution of the election process.
95    '''95    '''
96    96    
97    all_elections = []97    all_elections = []
9898
99    def __init__(self, year):99    def __init__(self, year):
100        self.year = year100        self.year = year
101        self.contestants = dict()101        self.contestants = dict()
102        Elections.all_elections.append(self)102        Elections.all_elections.append(self)
103103
104    def register_party_or_coalition(self, contestant):104    def register_party_or_coalition(self, contestant):
105        self.contestants[contestant] = 0105        self.contestants[contestant] = 0
106106
107    def vote(self, contestant):107    def vote(self, contestant):
108        self.contestants[contestant] += 1108        self.contestants[contestant] += 1
109109
110    def rig_elections(self, contestant, amount, currency):110    def rig_elections(self, contestant, amount, currency):
111        amount_bought = 0111        amount_bought = 0
112        112        
113        if isinstance(contestant, PoliticalParty):113        if isinstance(contestant, PoliticalParty):
114            amount_bought = contestant.convert_currency_to_voters(amount, currency)114            amount_bought = contestant.convert_currency_to_voters(amount, currency)
115        else:115        else:
116            found = False116            found = False
117            for party in contestant.parties:117            for party in contestant.parties:
118                if currency == party.preferred_currency:118                if currency == party.preferred_currency:
119                    found = True119                    found = True
120                    amount_bought = party.convert_currency_to_voters(amount, currency)120                    amount_bought = party.convert_currency_to_voters(amount, currency)
121                    break121                    break
122            122            
123            if not found:123            if not found:
t124                amount_bought = amount // currency.exchange_ratet124                amount_bought = int(amount / currency.exchange_rate)
125        125        
126        self.contestants[contestant] += amount_bought   126        self.contestants[contestant] += amount_bought   
127127
128    def get_results(self):128    def get_results(self):
129        return {str(contestant): votes for contestant, votes in self.contestants.items()}129        return {str(contestant): votes for contestant, votes in self.contestants.items()}
130    130    
131    @classmethod131    @classmethod
132    def get_results_by_year(cls, year):132    def get_results_by_year(cls, year):
133        for election in cls.all_elections:133        for election in cls.all_elections:
134            if election.year == year:134            if election.year == year:
135                return election.get_results()135                return election.get_results()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op