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

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

7 точки общо

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

  1class Currency:
  2    """Class that defines different currencies that help political parties byu votes."""
  3    def __init__(self, name, rate):
  4        self.name = name
  5        self.rate = rate
  6
  7    def __eq__(self, other):
  8        return self.name == other.name and self.rate == other.rate
  9        
 10
 11class PoliticalParty:
 12    """Class that defines political parties and currency-voters' conversion."""
 13    def __init__(self, name, motto, members=[], preferred_currency=None):
 14        self.name = name
 15        self.__motto = motto
 16        self.members = members
 17        self.preferred_currency = preferred_currency
 18
 19    def convert_currency_to_voters(self, currency_count, currency):
 20        multiplier = 1
 21        if self.preferred_currency and currency == self.preferred_currency:
 22            multiplier *= 2
 23        
 24        return int(currency_count / currency.rate) * multiplier
 25    
 26    @property
 27    def motto(self):
 28        return self.__motto
 29    
 30    def __str__(self):
 31        return self.name
 32
 33    def __add__(self, other):
 34        return Coalition(self, other)
 35
 36class Coalition:
 37    """Class that defines coalitions and how they add parties."""
 38    def __init__(self, *parties):
 39        self.parties = list(parties)
 40
 41    @property
 42    def members(self):
 43        return {party.name : party.members for party in self.parties}
 44        
 45    def __add__(self, other):
 46        new_parties = self.parties.copy()
 47        if isinstance(other, PoliticalParty):
 48            new_parties.append(other)
 49        elif isinstance(other, Coalition):
 50            new_parties.extend(other.parties)
 51        else:
 52            raise TypeError("Invalid type for Coalition.")
 53        
 54        return Coalition(*new_parties)
 55                         
 56    def __str__(self):
 57        return "-".join([party.name for party in self.parties])
 58
 59
 60class Elections:
 61    """Class that defines the election's process."""
 62    _history = {}
 63
 64    def __init__(self, date):
 65        self.date = date
 66        self.participants = {}
 67        Elections._history[date] = self.participants
 68
 69    def register_party_or_coalition(self, party_or_coalition):
 70        if not isinstance(party_or_coalition, (Coalition, PoliticalParty)):
 71            raise TypeError("Invalid type for Elections.")
 72        self.participants[party_or_coalition] = 0
 73        
 74    def vote(self, party_or_coalition):
 75        if party_or_coalition not in self.participants:
 76            # хубаво да се направи custom exception, but time is ticking away
 77            raise RuntimeError("Party or coalition not registered in the elections.")
 78        self.participants[party_or_coalition] += 1
 79
 80    def rig_elections(self, party_or_coalition, count, currency):
 81        if isinstance(party_or_coalition, PoliticalParty):
 82            voters = party_or_coalition.convert_currency_to_voters(count, currency)
 83            self.participants[party_or_coalition] += voters
 84        elif isinstance(party_or_coalition, Coalition):
 85            max_voters = 0
 86
 87            for party in party_or_coalition.parties:
 88                voters = party.convert_currency_to_voters(count, currency)
 89                if voters > max_voters:
 90                    max_voters = voters
 91            self.participants[party_or_coalition] += max_voters
 92        else:
 93            raise TypeError("Invalid type for rig_elections.")
 94        
 95    def get_results(self):
 96        return {str(party_or_coalition) : count for party_or_coalition, count in self.participants.items()}
 97
 98    @staticmethod
 99    def get_results_by_year(year):
100        if year in Elections._history:
101            return {str(k): v for k, v in Elections._history[year].items()}
102        return {}    
103    

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

OK

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

f1class Currency:f1class Currency:
2    """Class that defines different currencies that help political parties byu votes."""2    """Class that defines different currencies that help political parties byu votes."""
3    def __init__(self, name, rate):3    def __init__(self, name, rate):
4        self.name = name4        self.name = name
5        self.rate = rate5        self.rate = rate
66
7    def __eq__(self, other):7    def __eq__(self, other):
8        return self.name == other.name and self.rate == other.rate8        return self.name == other.name and self.rate == other.rate
9        9        
1010
11class PoliticalParty:11class PoliticalParty:
n12    """Class which defines political parties."""n12    """Class that defines political parties and currency-voters' conversion."""
13    def __init__(self, name, motto, members=[], preferred_currency=None):13    def __init__(self, name, motto, members=[], preferred_currency=None):
14        self.name = name14        self.name = name
15        self.__motto = motto15        self.__motto = motto
16        self.members = members16        self.members = members
17        self.preferred_currency = preferred_currency17        self.preferred_currency = preferred_currency
1818
19    def convert_currency_to_voters(self, currency_count, currency):19    def convert_currency_to_voters(self, currency_count, currency):
20        multiplier = 120        multiplier = 1
21        if self.preferred_currency and currency == self.preferred_currency:21        if self.preferred_currency and currency == self.preferred_currency:
22            multiplier *= 222            multiplier *= 2
23        23        
24        return int(currency_count / currency.rate) * multiplier24        return int(currency_count / currency.rate) * multiplier
25    25    
26    @property26    @property
27    def motto(self):27    def motto(self):
28        return self.__motto28        return self.__motto
29    29    
30    def __str__(self):30    def __str__(self):
31        return self.name31        return self.name
3232
33    def __add__(self, other):33    def __add__(self, other):
34        return Coalition(self, other)34        return Coalition(self, other)
3535
36class Coalition:36class Coalition:
n37    """Class which defines coalitions."""n37    """Class that defines coalitions and how they add parties."""
38    def __init__(self, *parties):38    def __init__(self, *parties):
39        self.parties = list(parties)39        self.parties = list(parties)
4040
41    @property41    @property
42    def members(self):42    def members(self):
43        return {party.name : party.members for party in self.parties}43        return {party.name : party.members for party in self.parties}
44        44        
45    def __add__(self, other):45    def __add__(self, other):
46        new_parties = self.parties.copy()46        new_parties = self.parties.copy()
47        if isinstance(other, PoliticalParty):47        if isinstance(other, PoliticalParty):
48            new_parties.append(other)48            new_parties.append(other)
49        elif isinstance(other, Coalition):49        elif isinstance(other, Coalition):
50            new_parties.extend(other.parties)50            new_parties.extend(other.parties)
51        else:51        else:
52            raise TypeError("Invalid type for Coalition.")52            raise TypeError("Invalid type for Coalition.")
53        53        
54        return Coalition(*new_parties)54        return Coalition(*new_parties)
55                         55                         
56    def __str__(self):56    def __str__(self):
57        return "-".join([party.name for party in self.parties])57        return "-".join([party.name for party in self.parties])
5858
5959
60class Elections:60class Elections:
t61    """Class which defines the election's process."""t61    """Class that defines the election's process."""
62    _history = {}62    _history = {}
6363
64    def __init__(self, date):64    def __init__(self, date):
65        self.date = date65        self.date = date
66        self.participants = {}66        self.participants = {}
67        Elections._history[date] = self.participants67        Elections._history[date] = self.participants
6868
69    def register_party_or_coalition(self, party_or_coalition):69    def register_party_or_coalition(self, party_or_coalition):
70        if not isinstance(party_or_coalition, (Coalition, PoliticalParty)):70        if not isinstance(party_or_coalition, (Coalition, PoliticalParty)):
71            raise TypeError("Invalid type for Elections.")71            raise TypeError("Invalid type for Elections.")
72        self.participants[party_or_coalition] = 072        self.participants[party_or_coalition] = 0
73        73        
74    def vote(self, party_or_coalition):74    def vote(self, party_or_coalition):
75        if party_or_coalition not in self.participants:75        if party_or_coalition not in self.participants:
76            # хубаво да се направи custom exception, but time is ticking away76            # хубаво да се направи custom exception, but time is ticking away
77            raise RuntimeError("Party or coalition not registered in the elections.")77            raise RuntimeError("Party or coalition not registered in the elections.")
78        self.participants[party_or_coalition] += 178        self.participants[party_or_coalition] += 1
7979
80    def rig_elections(self, party_or_coalition, count, currency):80    def rig_elections(self, party_or_coalition, count, currency):
81        if isinstance(party_or_coalition, PoliticalParty):81        if isinstance(party_or_coalition, PoliticalParty):
82            voters = party_or_coalition.convert_currency_to_voters(count, currency)82            voters = party_or_coalition.convert_currency_to_voters(count, currency)
83            self.participants[party_or_coalition] += voters83            self.participants[party_or_coalition] += voters
84        elif isinstance(party_or_coalition, Coalition):84        elif isinstance(party_or_coalition, Coalition):
85            max_voters = 085            max_voters = 0
8686
87            for party in party_or_coalition.parties:87            for party in party_or_coalition.parties:
88                voters = party.convert_currency_to_voters(count, currency)88                voters = party.convert_currency_to_voters(count, currency)
89                if voters > max_voters:89                if voters > max_voters:
90                    max_voters = voters90                    max_voters = voters
91            self.participants[party_or_coalition] += max_voters91            self.participants[party_or_coalition] += max_voters
92        else:92        else:
93            raise TypeError("Invalid type for rig_elections.")93            raise TypeError("Invalid type for rig_elections.")
94        94        
95    def get_results(self):95    def get_results(self):
96        return {str(party_or_coalition) : count for party_or_coalition, count in self.participants.items()}96        return {str(party_or_coalition) : count for party_or_coalition, count in self.participants.items()}
9797
98    @staticmethod98    @staticmethod
99    def get_results_by_year(year):99    def get_results_by_year(year):
100        if year in Elections._history:100        if year in Elections._history:
101            return {str(k): v for k, v in Elections._history[year].items()}101            return {str(k): v for k, v in Elections._history[year].items()}
102        return {}    102        return {}    
103    103    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op