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)
| f | 1 | import math | f | 1 | import math |
| t | 2 | def 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 | ||||
| 11 | 2 | ||||
| 12 | class Candy: | 3 | class Candy: | ||
| 13 | def __init__(self, mass, uranium): | 4 | def __init__(self, mass, uranium): | ||
| 14 | self._mass = mass | 5 | self._mass = mass | ||
| 15 | self._uranium = uranium | 6 | self._uranium = uranium | ||
| 16 | 7 | ||||
| 17 | def get_uranium_quantity(self): | 8 | def get_uranium_quantity(self): | ||
| 18 | return self._mass * self._uranium | 9 | return self._mass * self._uranium | ||
| 19 | 10 | ||||
| 20 | def get_mass(self): | 11 | def get_mass(self): | ||
| 21 | return self._mass | 12 | return self._mass | ||
| 22 | 13 | ||||
| 23 | 14 | ||||
| 24 | class Person: | 15 | class Person: | ||
| 25 | def __init__(self, position): | 16 | def __init__(self, position): | ||
| 26 | self._position = position | 17 | self._position = position | ||
| 27 | 18 | ||||
| 28 | def get_position(self): | 19 | def get_position(self): | ||
| 29 | return self._position | 20 | return self._position | ||
| 30 | 21 | ||||
| 31 | def set_position(self, position): | 22 | def set_position(self, position): | ||
| 32 | self._position = position | 23 | self._position = position | ||
| 33 | 24 | ||||
| 34 | 25 | ||||
| 35 | class Kid(Person): | 26 | class Kid(Person): | ||
| 36 | CRITICAL_AMOUND = 20 | 27 | CRITICAL_AMOUND = 20 | ||
| 37 | 28 | ||||
| 38 | def __init__(self, position, initiative): | 29 | def __init__(self, position, initiative): | ||
| 39 | super().__init__(position) | 30 | super().__init__(position) | ||
| 40 | self._initiative = initiative | 31 | 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._initiative | 36 | 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) | ||
| 49 | 40 | ||||
| 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) | ||
| 55 | 46 | ||||
| 56 | def is_critical(self): | 47 | def is_critical(self): | ||
| 57 | uranium_in_bag = 0 | 48 | 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_AMOUND | 51 | return uranium_in_bag > self.CRITICAL_AMOUND | ||
| 61 | 52 | ||||
| 62 | @staticmethod | 53 | @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) | ||
| 65 | 56 | ||||
| 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_list | 63 | return sorted_list | ||
| 73 | 64 | ||||
| 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) | ||
| 80 | 71 | ||||
| 81 | 72 | ||||
| 82 | class Host(Person): | 73 | class 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 = [] | ||
| 88 | 79 | ||||
| 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._bag | 83 | return self._bag | ||
| 93 | def is_empty_bag(self): | 84 | def is_empty_bag(self): | ||
| 94 | return len(self._bag) < 1 | 85 | return len(self._bag) < 1 | ||
| 95 | 86 | ||||
| 96 | @staticmethod | 87 | @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 candy | 96 | return candy | ||
| 106 | 97 | ||||
| 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:] | ||
| 119 | 110 | ||||
| 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_remove | 116 | return candy_to_remove | ||
| 126 | 117 | ||||
| 127 | 118 | ||||
| 128 | class FluxCapacitor: | 119 | class FluxCapacitor: | ||
| 129 | VICTIM = 1 | 120 | VICTIM = 1 | ||
| 130 | 121 | ||||
| 131 | def __init__(self, participants): | 122 | def __init__(self, participants): | ||
| 132 | self._participants = participants | 123 | 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 None | 142 | return None | ||
| 152 | return self._victims | 143 | return self._victims |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | import math | f | 1 | import math |
| n | 2 | n | |||
| 3 | def max_candy(candies): | 2 | def max_not_candy(candies): | ||
| 4 | if len(candies) != 0: | 3 | if len(candies) != 0: | ||
| n | 5 | max_mass = candies[0].get_mass | n | 4 | max_mass = candies[0].get_mass() |
| 6 | for candy in candies: | 5 | for candy in candies: | ||
| n | 7 | if max_mass < candy.get_mass: | n | 6 | if max_mass < candy.get_mass(): |
| 8 | max_mass = candy.get_mass | 7 | max_mass = candy.get_mass() | ||
| 8 | for candy in candies: | ||||
| 9 | if candy.get_mass() == max_mass: | ||||
| 9 | return candy | 10 | return candy | ||
| 10 | |||||
| 11 | |||||
| 12 | 11 | ||||
| 13 | class Candy: | 12 | class Candy: | ||
| 14 | def __init__(self, mass, uranium): | 13 | def __init__(self, mass, uranium): | ||
| n | 15 | self.__mass = mass | n | 14 | self._mass = mass |
| 16 | self.__uranium = uranium | 15 | self._uranium = uranium | ||
| 17 | 16 | ||||
| 18 | def get_uranium_quantity(self): | 17 | def get_uranium_quantity(self): | ||
| n | 19 | return self.__mass * self.__uranium | n | 18 | return self._mass * self._uranium |
| 20 | 19 | ||||
| 21 | def get_mass(self): | 20 | def get_mass(self): | ||
| n | 22 | return self.__mass | n | 21 | return self._mass |
| 23 | 22 | ||||
| 24 | 23 | ||||
| 25 | class Person: | 24 | class Person: | ||
| 26 | def __init__(self, position): | 25 | def __init__(self, position): | ||
| n | 27 | self.__position = position | n | 26 | self._position = position |
| 28 | 27 | ||||
| 29 | def get_position(self): | 28 | def get_position(self): | ||
| n | 30 | return self.__position | n | 29 | return self._position |
| 31 | 30 | ||||
| 32 | def set_position(self, position): | 31 | def set_position(self, position): | ||
| n | 33 | self.__position = position | n | 32 | self._position = position |
| 34 | 33 | ||||
| 35 | 34 | ||||
| 36 | class Kid(Person): | 35 | class Kid(Person): | ||
| n | n | 36 | CRITICAL_AMOUND = 20 | ||
| 37 | |||||
| 37 | def __init__(self, position, initiative): | 38 | def __init__(self, position, initiative): | ||
| 38 | super().__init__(position) | 39 | super().__init__(position) | ||
| n | 39 | self.__initiative = initiative | n | 40 | 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): | ||
| n | 44 | return self.__initiative | n | 45 | return self._initiative |
| 45 | 46 | ||||
| 46 | def get_hosts(self): | 47 | def get_hosts(self): | ||
| n | 47 | return len(self.__only_hosts) | n | 48 | return len(self._only_hosts) |
| 48 | 49 | ||||
| 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): | ||
| n | 53 | self.__bag.append(Candy) | n | 54 | self._bag.append(Candy) |
| 54 | 55 | ||||
| 55 | def is_critical(self): | 56 | def is_critical(self): | ||
| 56 | uranium_in_bag = 0 | 57 | uranium_in_bag = 0 | ||
| n | 57 | for candy in self.__bag: | n | 58 | for candy in self._bag: |
| 58 | uranium_in_bag += candy.get_uranium_quantity() | 59 | uranium_in_bag += candy.get_uranium_quantity() | ||
| n | 59 | return uranium_in_bag > 20 | n | 60 | 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) | ||
| 63 | 65 | ||||
| n | 64 | def sort_all_distances(self): # make sorted list of distance and hosts | n | 66 | def sort_all_distances(self): |
| 65 | distance_nearest_host = [] | 67 | distance_nearest_host = [] | ||
| n | 66 | for host in self.__only_hosts: | n | 68 | 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()) | ||
| n | 68 | distance_nearest_host.append(distance_host_kid, host) | n | 70 | 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_list | 72 | return sorted_list | ||
| 71 | 73 | ||||
| 72 | def next_host(self): | 74 | def next_host(self): | ||
| n | 73 | 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()) | ||
| n | 81 | self.__only_hosts.remove(host) | n | 79 | self._only_hosts.remove(host) |
| 82 | |||||
| 83 | 80 | ||||
| 84 | 81 | ||||
| 85 | class Host(Person): | 82 | class Host(Person): | ||
| 86 | def __init__(self, position, candies): | 83 | def __init__(self, position, candies): | ||
| 87 | super().__init__(position) | 84 | super().__init__(position) | ||
| n | 88 | self.__list_kids = [] | n | 85 | 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])) | ||||
| 92 | 88 | ||||
| 93 | def add_kid(self, kid): | 89 | def add_kid(self, kid): | ||
| n | 94 | self.__list_kids.append(kid) | n | 90 | 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 | ||||
| 95 | 95 | ||||
| n | 96 | def is_empty_bag(self): | n | 96 | @staticmethod |
| 97 | return len(self.__bag) < 1 | 97 | 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 | ||||
| 98 | 106 | ||||
| 99 | def choose_kid(self): | 107 | def choose_kid(self): | ||
| n | 100 | sorted_initiative_kids = [] | n | 108 | 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) | ||
| n | 108 | self.__list_kids.remove(kid) #remove host from list of hosts in the kid | n | 117 | self._list_kids.remove(kid) |
| 109 | sorted_initiative_kids = sorted_initiative_kids[1:] | 118 | self.sorted_initiative_kids = self.sorted_initiative_kids[1:] | ||
| 110 | 119 | ||||
| n | 111 | n | |||
| 112 | def remove_candy(self, max_candy): | 120 | def remove_candy(self, max_candy): | ||
| n | 113 | if len(self.__bag) == 0: | n | 121 | 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_candy | 125 | return candy_to_remove | ||
| 117 | 126 | ||||
| 118 | 127 | ||||
| 119 | class FluxCapacitor: | 128 | class FluxCapacitor: | ||
| n | n | 129 | VICTIM = 1 | ||
| 130 | |||||
| 120 | def __init__(self, participants): | 131 | def __init__(self, participants): | ||
| n | 121 | self.__participants = participants | n | 132 | self._participants = participants |
| 122 | self.__victims = [] | 133 | self._victims = [] | ||
| 123 | 134 | ||||
| 124 | def get_victim(self): | 135 | def get_victim(self): | ||
| n | 125 | for participant in self.__participants: | n | 136 | 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 hosts | 138 | 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) | ||
| n | 132 | for participant in self.__participants: | n | 143 | for participant in self._participants: |
| 133 | if isinstance(participant, Host): | 144 | if isinstance(participant, Host): | ||
| 134 | participant.choose_kid() | 145 | participant.choose_kid() | ||
| n | 135 | for participant in self.__participants: | n | 146 | 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(): | ||
| n | 138 | self.__victims.append(participant) | n | 149 | self._victims.append(participant) |
| 139 | else: | 150 | else: | ||
| 140 | return None | 151 | return None | ||
| t | 141 | return self.__victims | t | 152 | return self._victims |
| 142 | |||||
| 143 | |||||
| 144 | |||||
| 145 | |||||
| 146 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | import math | f | 1 | import math |
| 2 | 2 | ||||
| 3 | def max_candy(candies): | 3 | def max_candy(candies): | ||
| 4 | if len(candies) != 0: | 4 | if len(candies) != 0: | ||
| 5 | max_mass = candies[0].get_mass | 5 | 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_mass | 8 | max_mass = candy.get_mass | ||
| 9 | return candy | 9 | return candy | ||
| 10 | 10 | ||||
| 11 | 11 | ||||
| 12 | 12 | ||||
| 13 | class Candy: | 13 | class Candy: | ||
| 14 | def __init__(self, mass, uranium): | 14 | def __init__(self, mass, uranium): | ||
| 15 | self.__mass = mass | 15 | self.__mass = mass | ||
| 16 | self.__uranium = uranium | 16 | self.__uranium = uranium | ||
| 17 | 17 | ||||
| 18 | def get_uranium_quantity(self): | 18 | def get_uranium_quantity(self): | ||
| 19 | return self.__mass * self.__uranium | 19 | return self.__mass * self.__uranium | ||
| 20 | 20 | ||||
| 21 | def get_mass(self): | 21 | def get_mass(self): | ||
| 22 | return self.__mass | 22 | return self.__mass | ||
| 23 | 23 | ||||
| 24 | 24 | ||||
| 25 | class Person: | 25 | class Person: | ||
| 26 | def __init__(self, position): | 26 | def __init__(self, position): | ||
| 27 | self.__position = position | 27 | self.__position = position | ||
| 28 | 28 | ||||
| 29 | def get_position(self): | 29 | def get_position(self): | ||
| 30 | return self.__position | 30 | return self.__position | ||
| 31 | 31 | ||||
| 32 | def set_position(self, position): | 32 | def set_position(self, position): | ||
| 33 | self.__position = position | 33 | self.__position = position | ||
| 34 | 34 | ||||
| 35 | 35 | ||||
| 36 | class Kid(Person): | 36 | class 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 = initiative | 39 | 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.__initiative | 44 | 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) | ||
| 48 | 48 | ||||
| 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) | ||
| 54 | 54 | ||||
| 55 | def is_critical(self): | 55 | def is_critical(self): | ||
| 56 | uranium_in_bag = 0 | 56 | 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 > 20 | 59 | return uranium_in_bag > 20 | ||
| 60 | 60 | ||||
| 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) | ||
| 63 | 63 | ||||
| 64 | def sort_all_distances(self): # make sorted list of distance and hosts | 64 | 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_list | 70 | return sorted_list | ||
| 71 | 71 | ||||
| 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 distance | 77 | return None #return the host which is the nearest distance | ||
| 78 | 78 | ||||
| n | 79 | def is_hosted(self, host): #ne znam dali e ok Host kato param | n | 79 | 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) | ||
| 82 | 82 | ||||
| 83 | 83 | ||||
| 84 | 84 | ||||
| 85 | class Host(Person): | 85 | class 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])) | ||
| 92 | 92 | ||||
| 93 | def add_kid(self, kid): | 93 | def add_kid(self, kid): | ||
| 94 | self.__list_kids.append(kid) | 94 | self.__list_kids.append(kid) | ||
| 95 | 95 | ||||
| 96 | def is_empty_bag(self): | 96 | def is_empty_bag(self): | ||
| 97 | return len(self.__bag) < 1 | 97 | return len(self.__bag) < 1 | ||
| 98 | 98 | ||||
| 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 kid | 108 | 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:] | ||
| 110 | 110 | ||||
| 111 | 111 | ||||
| t | 112 | def remove_candy(self, max_candy): #ne znam dali trqbva az da pisha tazi funkciq | t | 112 | 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_candy | 116 | return max_candy | ||
| 117 | 117 | ||||
| 118 | 118 | ||||
| 119 | class FluxCapacitor: | 119 | class FluxCapacitor: | ||
| 120 | def __init__(self, participants): | 120 | def __init__(self, participants): | ||
| 121 | self.__participants = participants | 121 | self.__participants = participants | ||
| 122 | self.__victims = [] | 122 | self.__victims = [] | ||
| 123 | 123 | ||||
| 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 hosts | 127 | 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 None | 140 | return None | ||
| 141 | return self.__victims | 141 | return self.__victims | ||
| 142 | 142 | ||||
| 143 | 143 | ||||
| 144 | 144 | ||||
| 145 | 145 | ||||
| 146 | 146 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||