Домашни > Хелоуин в Припят > Решения > Решението на Мария Марковска

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

8 точки общо

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

  1import math
  2
  3class Candy:
  4    def __init__(self, mass, uranium):
  5        self._mass = mass
  6        self._uranium = uranium
  7    
  8    def get_uranium_quantity(self):
  9        return self._mass * self._uranium
 10
 11    def get_mass(self):
 12        return self._mass
 13
 14
 15class Person:
 16    def __init__(self, position):
 17        self._position = position
 18    
 19    def get_position(self):
 20        return self._position
 21    
 22    def set_position(self, position):
 23        self._position = position
 24    
 25
 26class Kid(Person):
 27    CRITICAL_AMOUND = 20 
 28
 29    def __init__(self, position, initiative):
 30        super().__init__(position)
 31        self._initiative = initiative
 32        self._bag = []
 33        self._only_hosts = []
 34    
 35    def get_initiative(self):
 36        return self._initiative
 37    
 38    def get_hosts(self):
 39        return len(self._only_hosts)
 40
 41    def set_only_hosts(self, kids_and_hosts):
 42        self.__only_hosts = {host for host in kids_and_hosts if not isinstance(host, Kid)}
 43    
 44    def add_candy(self, Candy):     
 45        self._bag.append(Candy)
 46
 47    def is_critical(self):
 48        uranium_in_bag = 0
 49        for candy in self._bag:
 50            uranium_in_bag += candy.get_uranium_quantity()
 51        return uranium_in_bag > self.CRITICAL_AMOUND
 52    
 53    @staticmethod
 54    def distance(p1, p2):
 55        return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
 56
 57    def sort_all_distances(self):
 58        distance_nearest_host = []
 59        for host in self._only_hosts:
 60            distance_host_kid = self.distance(self.get_position(), host.get_position())
 61            distance_nearest_host.append((distance_host_kid, host))
 62        sorted_list = sorted(distance_nearest_host, key=lambda x: x[0])
 63        return sorted_list
 64
 65    def next_host(self):
 66        return self.sort_all_distances()[0][1]
 67        
 68    def is_hosted(self, host):                            
 69        self.set_position(host.get_position())
 70        self._only_hosts.remove(host)
 71
 72
 73class Host(Person):
 74    def __init__(self, position, candies):
 75        super().__init__(position)
 76        self._list_kids = []
 77        self._bag = [Candy(*candy) for candy in candies]
 78        self.sorted_initiative_kids = []
 79
 80    def add_kid(self, kid):
 81        self._list_kids.append(kid)
 82    def get_bag(self):
 83        return self._bag
 84    def is_empty_bag(self):
 85        return len(self._bag) < 1
 86
 87    @staticmethod
 88    def max_candy(candies):
 89        if len(candies) != 0:
 90            max_mass = candies[0].get_mass()
 91        for candy in candies:
 92            if max_mass < candy.get_mass():
 93                max_mass = candy.get_mass()
 94        for candy in candies:
 95            if candy.get_mass() == max_mass:
 96                return candy
 97
 98    def choose_kid(self):
 99        self.sorted_initiative_kids = []
100        for kid in self._list_kids:
101            self.sorted_initiative_kids.append((kid.get_initiative(), kid))
102        self.sorted_initiative_kids = sorted(self.sorted_initiative_kids, key = lambda x: x[0])
103        for _, kid in self.sorted_initiative_kids:
104            if not self.is_empty_bag():
105                candy = self.remove_candy(self.max_candy)
106                kid.add_candy(candy) 
107            kid.is_hosted(self)  
108            self._list_kids.remove(kid)
109            self.sorted_initiative_kids = self.sorted_initiative_kids[1:]                    
110
111    def remove_candy(self, max_candy):       
112        if len(self._bag) == 0:
113            return None         
114        candy_to_remove = max_candy(self._bag)               
115        self._bag.remove(candy_to_remove)
116        return candy_to_remove
117
118
119class FluxCapacitor:
120    VICTIM = 1
121
122    def __init__(self, participants):
123        self._participants = participants
124        self._victims = [] 
125        
126    def get_victim(self):
127        for participant in self._participants:
128            if isinstance(participant, Kid):
129                participant.set_only_hosts(self._participants)
130        while len(self._victims) < 1:
131            for participant in self._participants:
132                if isinstance(participant, Kid):
133                    participant.next_host().add_kid(participant)
134            for participant in self._participants:
135                if isinstance(participant, Host):
136                    participant.choose_kid()
137            for participant in self._participants:
138                if isinstance(participant, Kid) and participant.get_hosts() != 0:
139                    if participant.is_critical():
140                        self._victims.append(participant)
141            else: 
142                return None
143        return self._victims

..E.EE......
======================================================================
ERROR: test_empty_hosts (test.FluxCapacitorTest)
Test with empty hosts.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
File "/tmp/test.py", line 88, in test_empty_hosts
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 133, in get_victim
participant.next_host().add_kid(participant)
File "/tmp/solution.py", line 66, in next_host
return self.sort_all_distances()[0][1]
IndexError: list index out of range

======================================================================
ERROR: test_no_candies (test.FluxCapacitorTest)
Test with no candies.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
File "/tmp/test.py", line 106, in test_no_candies
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 133, in get_victim
participant.next_host().add_kid(participant)
File "/tmp/solution.py", line 66, in next_host
return self.sort_all_distances()[0][1]
IndexError: list index out of range

======================================================================
ERROR: test_real_case (test.FluxCapacitorTest)
Test with real case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
File "/tmp/test.py", line 115, in test_real_case
self.assertEqual(FluxCapacitor({kid1, kid2, host1, host2}).get_victim(), {kid1, kid2})
File "/tmp/solution.py", line 133, in get_victim
participant.next_host().add_kid(participant)
File "/tmp/solution.py", line 66, in next_host
return self.sort_all_distances()[0][1]
IndexError: list index out of range

----------------------------------------------------------------------
Ran 12 tests in 0.001s

FAILED (errors=3)

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

f1import mathf1import math
t2def max_not_candy(candies):t
3    if len(candies) != 0:
4        max_mass = candies[0].get_mass()
5    for candy in candies:
6        if max_mass < candy.get_mass():
7            max_mass = candy.get_mass()
8    for candy in candies:
9        if candy.get_mass() == max_mass:
10            return candy
112
12class Candy:3class Candy:
13    def __init__(self, mass, uranium):4    def __init__(self, mass, uranium):
14        self._mass = mass5        self._mass = mass
15        self._uranium = uranium6        self._uranium = uranium
16    7    
17    def get_uranium_quantity(self):8    def get_uranium_quantity(self):
18        return self._mass * self._uranium9        return self._mass * self._uranium
1910
20    def get_mass(self):11    def get_mass(self):
21        return self._mass12        return self._mass
2213
2314
24class Person:15class Person:
25    def __init__(self, position):16    def __init__(self, position):
26        self._position = position17        self._position = position
27    18    
28    def get_position(self):19    def get_position(self):
29        return self._position20        return self._position
30    21    
31    def set_position(self, position):22    def set_position(self, position):
32        self._position = position23        self._position = position
33    24    
3425
35class Kid(Person):26class Kid(Person):
36    CRITICAL_AMOUND = 20 27    CRITICAL_AMOUND = 20 
3728
38    def __init__(self, position, initiative):29    def __init__(self, position, initiative):
39        super().__init__(position)30        super().__init__(position)
40        self._initiative = initiative31        self._initiative = initiative
41        self._bag = []32        self._bag = []
42        self._only_hosts = []33        self._only_hosts = []
43    34    
44    def get_initiative(self):35    def get_initiative(self):
45        return self._initiative36        return self._initiative
46    37    
47    def get_hosts(self):38    def get_hosts(self):
48        return len(self._only_hosts)39        return len(self._only_hosts)
4940
50    def set_only_hosts(self, kids_and_hosts):41    def set_only_hosts(self, kids_and_hosts):
51        self.__only_hosts = {host for host in kids_and_hosts if not isinstance(host, Kid)}42        self.__only_hosts = {host for host in kids_and_hosts if not isinstance(host, Kid)}
52    43    
53    def add_candy(self, Candy):     44    def add_candy(self, Candy):     
54        self._bag.append(Candy)45        self._bag.append(Candy)
5546
56    def is_critical(self):47    def is_critical(self):
57        uranium_in_bag = 048        uranium_in_bag = 0
58        for candy in self._bag:49        for candy in self._bag:
59            uranium_in_bag += candy.get_uranium_quantity()50            uranium_in_bag += candy.get_uranium_quantity()
60        return uranium_in_bag > self.CRITICAL_AMOUND51        return uranium_in_bag > self.CRITICAL_AMOUND
61    52    
62    @staticmethod53    @staticmethod
63    def distance(p1, p2):54    def distance(p1, p2):
64        return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)55        return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
6556
66    def sort_all_distances(self):57    def sort_all_distances(self):
67        distance_nearest_host = []58        distance_nearest_host = []
68        for host in self._only_hosts:59        for host in self._only_hosts:
69            distance_host_kid = self.distance(self.get_position(), host.get_position())60            distance_host_kid = self.distance(self.get_position(), host.get_position())
70            distance_nearest_host.append((distance_host_kid, host))61            distance_nearest_host.append((distance_host_kid, host))
71        sorted_list = sorted(distance_nearest_host, key=lambda x: x[0])62        sorted_list = sorted(distance_nearest_host, key=lambda x: x[0])
72        return sorted_list63        return sorted_list
7364
74    def next_host(self):65    def next_host(self):
75        return self.sort_all_distances()[0][1]66        return self.sort_all_distances()[0][1]
76        67        
77    def is_hosted(self, host):                            68    def is_hosted(self, host):                            
78        self.set_position(host.get_position())69        self.set_position(host.get_position())
79        self._only_hosts.remove(host)70        self._only_hosts.remove(host)
8071
8172
82class Host(Person):73class Host(Person):
83    def __init__(self, position, candies):74    def __init__(self, position, candies):
84        super().__init__(position)75        super().__init__(position)
85        self._list_kids = []76        self._list_kids = []
86        self._bag = [Candy(*candy) for candy in candies]77        self._bag = [Candy(*candy) for candy in candies]
87        self.sorted_initiative_kids = []78        self.sorted_initiative_kids = []
8879
89    def add_kid(self, kid):80    def add_kid(self, kid):
90        self._list_kids.append(kid)81        self._list_kids.append(kid)
91    def get_bag(self):82    def get_bag(self):
92        return self._bag83        return self._bag
93    def is_empty_bag(self):84    def is_empty_bag(self):
94        return len(self._bag) < 185        return len(self._bag) < 1
9586
96    @staticmethod87    @staticmethod
97    def max_candy(candies):88    def max_candy(candies):
98        if len(candies) != 0:89        if len(candies) != 0:
99            max_mass = candies[0].get_mass()90            max_mass = candies[0].get_mass()
100        for candy in candies:91        for candy in candies:
101            if max_mass < candy.get_mass():92            if max_mass < candy.get_mass():
102                max_mass = candy.get_mass()93                max_mass = candy.get_mass()
103        for candy in candies:94        for candy in candies:
104            if candy.get_mass() == max_mass:95            if candy.get_mass() == max_mass:
105                return candy96                return candy
10697
107    def choose_kid(self):98    def choose_kid(self):
108        self.sorted_initiative_kids = []99        self.sorted_initiative_kids = []
109        for kid in self._list_kids:100        for kid in self._list_kids:
110            self.sorted_initiative_kids.append((kid.get_initiative(), kid))101            self.sorted_initiative_kids.append((kid.get_initiative(), kid))
111        self.sorted_initiative_kids = sorted(self.sorted_initiative_kids, key = lambda x: x[0])102        self.sorted_initiative_kids = sorted(self.sorted_initiative_kids, key = lambda x: x[0])
112        for _, kid in self.sorted_initiative_kids:103        for _, kid in self.sorted_initiative_kids:
113            if not self.is_empty_bag():104            if not self.is_empty_bag():
114                candy = self.remove_candy(self.max_candy)105                candy = self.remove_candy(self.max_candy)
115                kid.add_candy(candy) 106                kid.add_candy(candy) 
116            kid.is_hosted(self)  107            kid.is_hosted(self)  
117            self._list_kids.remove(kid)108            self._list_kids.remove(kid)
118            self.sorted_initiative_kids = self.sorted_initiative_kids[1:]                    109            self.sorted_initiative_kids = self.sorted_initiative_kids[1:]                    
119110
120    def remove_candy(self, max_candy):       111    def remove_candy(self, max_candy):       
121        if len(self._bag) == 0:112        if len(self._bag) == 0:
122            return None         113            return None         
123        candy_to_remove = max_candy(self._bag)               114        candy_to_remove = max_candy(self._bag)               
124        self._bag.remove(candy_to_remove)115        self._bag.remove(candy_to_remove)
125        return candy_to_remove116        return candy_to_remove
126117
127118
128class FluxCapacitor:119class FluxCapacitor:
129    VICTIM = 1120    VICTIM = 1
130121
131    def __init__(self, participants):122    def __init__(self, participants):
132        self._participants = participants123        self._participants = participants
133        self._victims = [] 124        self._victims = [] 
134        125        
135    def get_victim(self):126    def get_victim(self):
136        for participant in self._participants:127        for participant in self._participants:
137            if isinstance(participant, Kid):128            if isinstance(participant, Kid):
138                participant.set_only_hosts(self._participants)129                participant.set_only_hosts(self._participants)
139        while len(self._victims) < 1:130        while len(self._victims) < 1:
140            for participant in self._participants:131            for participant in self._participants:
141                if isinstance(participant, Kid):132                if isinstance(participant, Kid):
142                    participant.next_host().add_kid(participant)133                    participant.next_host().add_kid(participant)
143            for participant in self._participants:134            for participant in self._participants:
144                if isinstance(participant, Host):135                if isinstance(participant, Host):
145                    participant.choose_kid()136                    participant.choose_kid()
146            for participant in self._participants:137            for participant in self._participants:
147                if isinstance(participant, Kid) and participant.get_hosts() != 0:138                if isinstance(participant, Kid) and participant.get_hosts() != 0:
148                    if participant.is_critical():139                    if participant.is_critical():
149                        self._victims.append(participant)140                        self._victims.append(participant)
150            else: 141            else: 
151                return None142                return None
152        return self._victims143        return self._victims
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
n2 n
3def max_candy(candies):2def max_not_candy(candies):
4    if len(candies) != 0:3    if len(candies) != 0:
n5        max_mass =  candies[0].get_massn4        max_mass = candies[0].get_mass()
6    for candy in candies:5    for candy in candies:
n7        if max_mass < candy.get_mass:n6        if max_mass < candy.get_mass():
8            max_mass = candy.get_mass7            max_mass = candy.get_mass()
8    for candy in candies:
9        if candy.get_mass() == max_mass:
9    return candy10            return candy
10 
11    
1211
13class Candy:12class Candy:
14    def __init__(self, mass, uranium):13    def __init__(self, mass, uranium):
n15        self.__mass = massn14        self._mass = mass
16        self.__uranium = uranium15        self._uranium = uranium
17    16    
18    def get_uranium_quantity(self):17    def get_uranium_quantity(self):
n19        return self.__mass * self.__uraniumn18        return self._mass * self._uranium
2019
21    def get_mass(self):20    def get_mass(self):
n22        return self.__massn21        return self._mass
2322
2423
25class Person:24class Person:
26    def __init__(self, position):25    def __init__(self, position):
n27        self.__position = positionn26        self._position = position
28    27    
29    def get_position(self):28    def get_position(self):
n30        return self.__positionn29        return self._position
31    30    
32    def set_position(self, position):31    def set_position(self, position):
n33        self.__position = positionn32        self._position = position
34    33    
3534
36class Kid(Person):35class Kid(Person):
nn36    CRITICAL_AMOUND = 20 
37 
37    def __init__(self, position, initiative):38    def __init__(self, position, initiative):
38        super().__init__(position)39        super().__init__(position)
n39        self.__initiative = initiativen40        self._initiative = initiative
40        self.__bag = []41        self._bag = []
41        self.__only_hosts = []42        self._only_hosts = []
42    43    
43    def get_initiative(self):44    def get_initiative(self):
n44        return self.__initiativen45        return self._initiative
45    46    
46    def get_hosts(self):47    def get_hosts(self):
n47        return len(self.__only_hosts)n48        return len(self._only_hosts)
4849
49    def set_only_hosts(self, kids_and_hosts):50    def set_only_hosts(self, kids_and_hosts):
50        self.__only_hosts = {host for host in kids_and_hosts if not isinstance(host, Kid)}51        self.__only_hosts = {host for host in kids_and_hosts if not isinstance(host, Kid)}
51    52    
52    def add_candy(self, Candy):     53    def add_candy(self, Candy):     
n53        self.__bag.append(Candy)n54        self._bag.append(Candy)
5455
55    def is_critical(self):56    def is_critical(self):
56        uranium_in_bag = 057        uranium_in_bag = 0
n57        for candy in self.__bag:n58        for candy in self._bag:
58            uranium_in_bag += candy.get_uranium_quantity()59            uranium_in_bag += candy.get_uranium_quantity()
n59        return uranium_in_bag > 20n60        return uranium_in_bag > self.CRITICAL_AMOUND
60 61    
62    @staticmethod
61    def distance(p1, p2):63    def distance(p1, p2):
62        return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)64        return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
6365
n64    def sort_all_distances(self):                    # make sorted list of distance and hostsn66    def sort_all_distances(self):
65        distance_nearest_host = []67        distance_nearest_host = []
n66        for host in self.__only_hosts:n68        for host in self._only_hosts:
67            distance_host_kid = self.distance(self.get_position(), host.get_position())69            distance_host_kid = self.distance(self.get_position(), host.get_position())
n68            distance_nearest_host.append(distance_host_kid, host)n70            distance_nearest_host.append((distance_host_kid, host))
69        sorted_list = sorted(distance_nearest_host, key=lambda x: x[0])71        sorted_list = sorted(distance_nearest_host, key=lambda x: x[0])
70        return sorted_list72        return sorted_list
7173
72    def next_host(self):74    def next_host(self):
n73        sorted_distances = self.sort_all_distances()n
74        if sorted_distances:
75            return sorted_distances[0][1]75        return self.sort_all_distances()[0][1]
76        else:76        
77            return None         #return the host which is the nearest distance
78 
79    def is_hosted(self, host):                            77    def is_hosted(self, host):                            
80        self.set_position(host.get_position())78        self.set_position(host.get_position())
n81        self.__only_hosts.remove(host)n79        self._only_hosts.remove(host)
82 
8380
8481
85class Host(Person):82class Host(Person):
86    def __init__(self, position, candies):83    def __init__(self, position, candies):
87        super().__init__(position)84        super().__init__(position)
n88        self.__list_kids = []n85        self._list_kids = []
89        self.__bag = []86        self._bag = [Candy(*candy) for candy in candies]
90        for x in candies:87        self.sorted_initiative_kids = []
91            self.__bag.append(Candy(x[0], x[1]))
9288
93    def add_kid(self, kid):89    def add_kid(self, kid):
n94        self.__list_kids.append(kid)n90        self._list_kids.append(kid)
91    def get_bag(self):
92        return self._bag
93    def is_empty_bag(self):
94        return len(self._bag) < 1
9595
n96    def is_empty_bag(self):n96    @staticmethod
97        return len(self.__bag) < 197    def max_candy(candies):
98        if len(candies) != 0:
99            max_mass = candies[0].get_mass()
100        for candy in candies:
101            if max_mass < candy.get_mass():
102                max_mass = candy.get_mass()
103        for candy in candies:
104            if candy.get_mass() == max_mass:
105                return candy
98106
99    def choose_kid(self):107    def choose_kid(self):
n100        sorted_initiative_kids = []n108        self.sorted_initiative_kids = []
101        for kid in self.__list_kids:109        for kid in self._list_kids:
102            sorted_initiative_kids.append(kid.initiative(), kid)110            self.sorted_initiative_kids.append((kid.get_initiative(), kid))
103        sorted_initiative_kids = sorted(sorted_initiative_kids, key=lambda x: x[0])111        self.sorted_initiative_kids = sorted(self.sorted_initiative_kids, key = lambda x: x[0])
104        for _, kid in sorted_initiative_kids:112        for _, kid in self.sorted_initiative_kids:
105            if self.is_empty_bag() == False:113            if not self.is_empty_bag():
106                kid.add_candy(self.remove_candy(max_candy)114                candy = self.remove_candy(self.max_candy)
115                kid.add_candy(candy) 
107            kid.is_hosted(self)  116            kid.is_hosted(self)  
n108            self.__list_kids.remove(kid)                                  #remove host from list of hosts in the kidn117            self._list_kids.remove(kid)
109            sorted_initiative_kids = sorted_initiative_kids[1:]                    118            self.sorted_initiative_kids = self.sorted_initiative_kids[1:]                    
110119
n111        n
112    def remove_candy(self, max_candy):       120    def remove_candy(self, max_candy):       
n113        if len(self.__bag) == 0:n121        if len(self._bag) == 0:
114            return None                        122            return None         
123        candy_to_remove = max_candy(self._bag)               
115        self.__bag.remove(max_candy)124        self._bag.remove(candy_to_remove)
116        return max_candy125        return candy_to_remove
117126
118127
119class FluxCapacitor:128class FluxCapacitor:
nn129    VICTIM = 1
130 
120    def __init__(self, participants):131    def __init__(self, participants):
n121        self.__participants = participantsn132        self._participants = participants
122        self.__victims = [] 133        self._victims = [] 
123 134        
124    def get_victim(self):135    def get_victim(self):
n125        for participant in self.__participants:n136        for participant in self._participants:
126            if type(participant) == Kid:137            if isinstance(participant, Kid):
127                participant.set_only_hosts(self.__participants)  #each kid is getting list of initially all hosts138                participant.set_only_hosts(self._participants)
128        while len(self.__victims) < 1:139        while len(self._victims) < 1:
129            for participant in self.__participants:140            for participant in self._participants:
130                if isinstance(participant, Kid):141                if isinstance(participant, Kid):
131                    participant.next_host().add_kid(participant)142                    participant.next_host().add_kid(participant)
n132            for participant in self.__participants:n143            for participant in self._participants:
133                if isinstance(participant, Host):144                if isinstance(participant, Host):
134                    participant.choose_kid()145                    participant.choose_kid()
n135            for participant in self.__participants:n146            for participant in self._participants:
136                if isinstance(participant, Kid) and participant.get_hosts() != 0:147                if isinstance(participant, Kid) and participant.get_hosts() != 0:
137                    if participant.is_critical():148                    if participant.is_critical():
n138                        self.__victims.append(participant)n149                        self._victims.append(participant)
139            else: 150            else: 
140                return None151                return None
t141        return self.__victimst152        return self._victims
142        
143 
144 
145            
146        
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
22
3def max_candy(candies):3def max_candy(candies):
4    if len(candies) != 0:4    if len(candies) != 0:
5        max_mass =  candies[0].get_mass5        max_mass =  candies[0].get_mass
6    for candy in candies:6    for candy in candies:
7        if max_mass < candy.get_mass:7        if max_mass < candy.get_mass:
8            max_mass = candy.get_mass8            max_mass = candy.get_mass
9    return candy9    return candy
1010
11    11    
1212
13class Candy:13class Candy:
14    def __init__(self, mass, uranium):14    def __init__(self, mass, uranium):
15        self.__mass = mass15        self.__mass = mass
16        self.__uranium = uranium16        self.__uranium = uranium
17    17    
18    def get_uranium_quantity(self):18    def get_uranium_quantity(self):
19        return self.__mass * self.__uranium19        return self.__mass * self.__uranium
2020
21    def get_mass(self):21    def get_mass(self):
22        return self.__mass22        return self.__mass
2323
2424
25class Person:25class Person:
26    def __init__(self, position):26    def __init__(self, position):
27        self.__position = position27        self.__position = position
28    28    
29    def get_position(self):29    def get_position(self):
30        return self.__position30        return self.__position
31    31    
32    def set_position(self, position):32    def set_position(self, position):
33        self.__position = position33        self.__position = position
34    34    
3535
36class Kid(Person):36class Kid(Person):
37    def __init__(self, position, initiative):37    def __init__(self, position, initiative):
38        super().__init__(position)38        super().__init__(position)
39        self.__initiative = initiative39        self.__initiative = initiative
40        self.__bag = []40        self.__bag = []
41        self.__only_hosts = []41        self.__only_hosts = []
42    42    
43    def get_initiative(self):43    def get_initiative(self):
44        return self.__initiative44        return self.__initiative
45    45    
46    def get_hosts(self):46    def get_hosts(self):
47        return len(self.__only_hosts)47        return len(self.__only_hosts)
4848
49    def set_only_hosts(self, kids_and_hosts):49    def set_only_hosts(self, kids_and_hosts):
50        self.__only_hosts = {host for host in kids_and_hosts if not isinstance(host, Kid)}50        self.__only_hosts = {host for host in kids_and_hosts if not isinstance(host, Kid)}
51    51    
52    def add_candy(self, Candy):     52    def add_candy(self, Candy):     
53        self.__bag.append(Candy)53        self.__bag.append(Candy)
5454
55    def is_critical(self):55    def is_critical(self):
56        uranium_in_bag = 056        uranium_in_bag = 0
57        for candy in self.__bag:57        for candy in self.__bag:
58            uranium_in_bag += candy.get_uranium_quantity()58            uranium_in_bag += candy.get_uranium_quantity()
59        return uranium_in_bag > 2059        return uranium_in_bag > 20
6060
61    def distance(p1, p2):61    def distance(p1, p2):
62        return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)62        return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
6363
64    def sort_all_distances(self):                    # make sorted list of distance and hosts64    def sort_all_distances(self):                    # make sorted list of distance and hosts
65        distance_nearest_host = []65        distance_nearest_host = []
66        for host in self.__only_hosts:66        for host in self.__only_hosts:
67            distance_host_kid = self.distance(self.get_position(), host.get_position())67            distance_host_kid = self.distance(self.get_position(), host.get_position())
68            distance_nearest_host.append(distance_host_kid, host)68            distance_nearest_host.append(distance_host_kid, host)
69        sorted_list = sorted(distance_nearest_host, key=lambda x: x[0])69        sorted_list = sorted(distance_nearest_host, key=lambda x: x[0])
70        return sorted_list70        return sorted_list
7171
72    def next_host(self):72    def next_host(self):
73        sorted_distances = self.sort_all_distances()73        sorted_distances = self.sort_all_distances()
74        if sorted_distances:74        if sorted_distances:
75            return sorted_distances[0][1]75            return sorted_distances[0][1]
76        else:76        else:
77            return None         #return the host which is the nearest distance77            return None         #return the host which is the nearest distance
7878
n79    def is_hosted(self, host):                            #ne znam dali e ok Host kato paramn79    def is_hosted(self, host):                            
80        self.set_position(host.get_position())80        self.set_position(host.get_position())
81        self.__only_hosts.remove(host)81        self.__only_hosts.remove(host)
8282
8383
8484
85class Host(Person):85class Host(Person):
86    def __init__(self, position, candies):86    def __init__(self, position, candies):
87        super().__init__(position)87        super().__init__(position)
88        self.__list_kids = []88        self.__list_kids = []
89        self.__bag = []89        self.__bag = []
90        for x in candies:90        for x in candies:
91            self.__bag.append(Candy(x[0], x[1]))91            self.__bag.append(Candy(x[0], x[1]))
9292
93    def add_kid(self, kid):93    def add_kid(self, kid):
94        self.__list_kids.append(kid)94        self.__list_kids.append(kid)
9595
96    def is_empty_bag(self):96    def is_empty_bag(self):
97        return len(self.__bag) < 197        return len(self.__bag) < 1
9898
99    def choose_kid(self):99    def choose_kid(self):
100        sorted_initiative_kids = []100        sorted_initiative_kids = []
101        for kid in self.__list_kids:101        for kid in self.__list_kids:
102            sorted_initiative_kids.append(kid.initiative(), kid)102            sorted_initiative_kids.append(kid.initiative(), kid)
103        sorted_initiative_kids = sorted(sorted_initiative_kids, key=lambda x: x[0])103        sorted_initiative_kids = sorted(sorted_initiative_kids, key=lambda x: x[0])
104        for _, kid in sorted_initiative_kids:104        for _, kid in sorted_initiative_kids:
105            if self.is_empty_bag() == False:105            if self.is_empty_bag() == False:
106                kid.add_candy(self.remove_candy(max_candy)) 106                kid.add_candy(self.remove_candy(max_candy)) 
107            kid.is_hosted(self)  107            kid.is_hosted(self)  
108            self.__list_kids.remove(kid)                                  #remove host from list of hosts in the kid108            self.__list_kids.remove(kid)                                  #remove host from list of hosts in the kid
109            sorted_initiative_kids = sorted_initiative_kids[1:]                    109            sorted_initiative_kids = sorted_initiative_kids[1:]                    
110110
111        111        
t112    def remove_candy(self, max_candy):           #ne znam dali trqbva az da pisha tazi funkciqt112    def remove_candy(self, max_candy):       
113        if len(self.__bag) == 0:113        if len(self.__bag) == 0:
114            return None                        114            return None                        
115        self.__bag.remove(max_candy)115        self.__bag.remove(max_candy)
116        return max_candy116        return max_candy
117117
118118
119class FluxCapacitor:119class FluxCapacitor:
120    def __init__(self, participants):120    def __init__(self, participants):
121        self.__participants = participants121        self.__participants = participants
122        self.__victims = [] 122        self.__victims = [] 
123123
124    def get_victim(self):124    def get_victim(self):
125        for participant in self.__participants:125        for participant in self.__participants:
126            if type(participant) == Kid:126            if type(participant) == Kid:
127                participant.set_only_hosts(self.__participants)  #each kid is getting list of initially all hosts127                participant.set_only_hosts(self.__participants)  #each kid is getting list of initially all hosts
128        while len(self.__victims) < 1:128        while len(self.__victims) < 1:
129            for participant in self.__participants:129            for participant in self.__participants:
130                if isinstance(participant, Kid):130                if isinstance(participant, Kid):
131                    participant.next_host().add_kid(participant)131                    participant.next_host().add_kid(participant)
132            for participant in self.__participants:132            for participant in self.__participants:
133                if isinstance(participant, Host):133                if isinstance(participant, Host):
134                    participant.choose_kid()134                    participant.choose_kid()
135            for participant in self.__participants:135            for participant in self.__participants:
136                if isinstance(participant, Kid) and participant.get_hosts() != 0:136                if isinstance(participant, Kid) and participant.get_hosts() != 0:
137                    if participant.is_critical():137                    if participant.is_critical():
138                        self.__victims.append(participant)138                        self.__victims.append(participant)
139            else: 139            else: 
140                return None140                return None
141        return self.__victims141        return self.__victims
142        142        
143143
144144
145            145            
146        146        
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op