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

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

8 точки общо

10 успешни теста
2 неуспешни теста
Код

  1import math
  2
  3MAX_INT = math.inf
  4MAX_URANIUM = 20
  5
  6class Candy:
  7    def __init__(self, mass, uranium):
  8        self.__mass = mass
  9        self.__uranium = uranium
 10
 11    def get_uranium_quantity(self):
 12        return self.__mass * self.__uranium
 13
 14    def get_mass(self):
 15        return self.__mass
 16
 17
 18class Person:
 19    def __init__(self, tuple_of_coordinates):
 20        self.tuple_of_coordinates = tuple_of_coordinates
 21
 22    def set_position(self, new_tuple_of_coordinates):
 23        self.tuple_of_coordinates = new_tuple_of_coordinates
 24
 25    def get_position(self):
 26        return self.tuple_of_coordinates
 27
 28
 29class Kid(Person):
 30    def __init__(self, tuple_of_coordinates, initiative):
 31        super().__init__(tuple_of_coordinates)
 32        self.__initiative = initiative
 33        self.box_of_candies = []
 34        self._level_of_uranium = 0.0
 35        self.sorted_hosts = []
 36        self.is_adult = False
 37        self.is_alive = True
 38
 39    def get_initiative(self):
 40        return self.__initiative
 41
 42    def add_candy(self, candy):
 43        self.box_of_candies.append(candy)
 44        self._level_of_uranium += candy.get_uranium_quantity()
 45
 46    def is_critical(self):
 47        return self._level_of_uranium > MAX_URANIUM
 48
 49    def __gt__(self, other):
 50        return self.get_initiative() > other.get_initiative()
 51
 52
 53class Host(Person):
 54    def __init__(self, tuple_of_coordinates, candies):
 55        super().__init__(tuple_of_coordinates)
 56        self.__candies = candies
 57        self.box_of_candies = [Candy(candy_params[0], candy_params[1]) for candy_params in candies]
 58        self.is_adult = True
 59        self.sorted_kids = []
 60
 61    @staticmethod
 62    def heaviest_candy(list_of_candies):
 63        max_candy = None
 64        max_candy_size = 0
 65        for candy in list_of_candies:
 66            if candy.get_mass() > max_candy_size:
 67                max_candy_size = candy.get_mass()
 68                max_candy = candy
 69
 70        return max_candy
 71
 72    @property
 73    def is_out_of_candy(self):
 74        return len(self.box_of_candies) == 0
 75
 76    def remove_candy(self, remove_candy_algorithm):
 77        if self.is_out_of_candy:
 78            return None
 79        return remove_candy_algorithm
 80
 81    def remove_candy_by_mass(self, list_of_candies):
 82        list_of_candies.remove(self.heaviest_candy(list_of_candies))
 83        return self.heaviest_candy(list_of_candies)
 84
 85
 86class FluxCapacitor:
 87    def __init__(self, participants):
 88        self.participants = participants
 89        self.kids = {}
 90        self.hosts = {}
 91        self.classify_participants()
 92
 93    @staticmethod
 94    def unalive_person(dict_of_people, person, is_alive):
 95        dict_of_people[person] = is_alive
 96
 97    @staticmethod
 98    def get_closest_distance_to_host(dict_of_hosts, target_coordinates):
 99        list_of_hosts = [host for host in dict_of_hosts]
100        min_distance = MAX_INT
101        min_coordinates = (MAX_INT, MAX_INT)
102        closest_host = None
103        for host in list_of_hosts:
104            tuple_of_coordinates = host.get_position()
105            distance = math.dist(tuple_of_coordinates, target_coordinates)
106            if distance < min_distance:
107                min_distance = distance
108                min_coordinates = tuple_of_coordinates
109                closest_host = host
110            elif distance == min_distance:
111                if tuple_of_coordinates[0] < min_coordinates[0]:
112                    min_coordinates = tuple_of_coordinates
113                    closest_host = host
114                elif tuple_of_coordinates[0] == min_coordinates[0]:
115                    min_coordinates = tuple_of_coordinates if tuple_of_coordinates[1] > min_coordinates[1] else min_coordinates
116                    closest_host = host if tuple_of_coordinates[1] < min_coordinates[1] else closest_host
117        return closest_host
118
119
120    def sort_hosts_by_distance_from_kid(self, kid):
121        copy_of_hosts = self.hosts.copy()
122        while len(copy_of_hosts) != 0:
123            closest_host = self.get_closest_distance_to_host(copy_of_hosts, kid.get_position())
124            if closest_host not in kid.sorted_hosts:
125                kid.sorted_hosts.append(closest_host)
126            popped = copy_of_hosts.pop(closest_host)
127
128        return kid.sorted_hosts
129
130    def sort_kids_by_initiative_for_host(self, host):
131        copy_of_kids = self.kids.copy()
132        for kid in copy_of_kids:
133            if host == self.get_closest_distance_to_host(self.hosts, kid.get_position()):
134                if kid not in host.sorted_kids and self.kids[kid] == False:
135                    host.sorted_kids.append(kid)
136        host.sorted_kids.sort(key=lambda kid:kid.get_initiative(), reverse=True)
137        return host.sorted_kids
138
139    def classify_participants(self):
140        for person in self.participants:
141            if not person.is_adult:
142                self.unalive_person(self.kids, person, False)
143            else:
144                if len(person.box_of_candies) != 0:
145                   self.unalive_person(self.hosts, person, False)
146
147    def get_victim(self):
148        dead_children = set()
149
150        while True:
151            if not self.hosts:
152                if bool(dead_children) != 0:
153                    return dead_children
154                else:
155                    return None
156            for kid in self.kids:
157                self.sort_hosts_by_distance_from_kid(kid)
158                closest_host = self.get_closest_distance_to_host(self.hosts, kid.get_position())
159
160                if self.kids[kid] is True:
161                    if kid in closest_host.sorted_kids:
162                        closest_host.sorted_kids.remove(kid)
163                    continue
164
165                try:
166                    out_of_candy = closest_host.is_out_of_candy
167                except AttributeError:
168                    continue
169
170                if closest_host.is_out_of_candy:
171                    kid.set_position(closest_host.get_position())
172
173                    if closest_host in kid.sorted_hosts:
174                        kid.sorted_hosts.remove(closest_host)
175                    continue
176
177                self.sort_kids_by_initiative_for_host(closest_host)
178                if kid == closest_host.sorted_kids[0]:
179
180                    given_candy = closest_host.heaviest_candy(closest_host.box_of_candies)
181                    kid.add_candy(given_candy)
182                    closest_host.remove_candy_by_mass(closest_host.box_of_candies)
183
184                    kid.set_position(closest_host.get_position())
185
186                    closest_host.sorted_kids.remove(kid)
187                    kid.sorted_hosts.remove(closest_host)
188
189                if kid.is_critical():
190                    dead_children.add(kid)
191                    self.unalive_person(self.kids, kid, True)
192
193                if closest_host.is_out_of_candy:
194                    self.hosts.pop(closest_host)
195
196            if bool(dead_children) != 0:
197                 return dead_children
198
199        return dead_children

...F..E.....
======================================================================
ERROR: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 66, in test_basic_usage
self.assertEqual(candy.get_mass(), 456)
AttributeError: 'function' object has no attribute 'get_mass'

======================================================================
FAIL: test_empty_kids (test.FluxCapacitorTest)
Test with empty kids.
----------------------------------------------------------------------
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 96, in test_empty_kids
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 196, in get_victim
if bool(dead_children) != 0:
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 69, in handler
_raise_exception(timeout_exception, exception_message)
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 45, in _raise_exception
raise exception()
timeout_decorator.timeout_decorator.TimeoutError: 'Timed Out'

----------------------------------------------------------------------
Ran 12 tests in 0.201s

FAILED (failures=1, errors=1)

Дискусия
Георги Кунчев
07.11.2023 16:04

Логиката в последния ти метод е доста дълга. Не мога да я проследя. Бих посъветвал да делигираш малко от тази работа на отделни методи. Разбира се, като се отключат домашните, можеш да видиш и работата на останалите, където има доста кратки версии.
История

f1import mathf1import math
22
3MAX_INT = math.inf3MAX_INT = math.inf
4MAX_URANIUM = 204MAX_URANIUM = 20
55
6class Candy:6class Candy:
7    def __init__(self, mass, uranium):7    def __init__(self, mass, uranium):
8        self.__mass = mass8        self.__mass = mass
9        self.__uranium = uranium9        self.__uranium = uranium
1010
11    def get_uranium_quantity(self):11    def get_uranium_quantity(self):
12        return self.__mass * self.__uranium12        return self.__mass * self.__uranium
1313
14    def get_mass(self):14    def get_mass(self):
15        return self.__mass15        return self.__mass
1616
1717
18class Person:18class Person:
19    def __init__(self, tuple_of_coordinates):19    def __init__(self, tuple_of_coordinates):
20        self.tuple_of_coordinates = tuple_of_coordinates20        self.tuple_of_coordinates = tuple_of_coordinates
2121
22    def set_position(self, new_tuple_of_coordinates):22    def set_position(self, new_tuple_of_coordinates):
23        self.tuple_of_coordinates = new_tuple_of_coordinates23        self.tuple_of_coordinates = new_tuple_of_coordinates
2424
25    def get_position(self):25    def get_position(self):
26        return self.tuple_of_coordinates26        return self.tuple_of_coordinates
2727
2828
29class Kid(Person):29class Kid(Person):
30    def __init__(self, tuple_of_coordinates, initiative):30    def __init__(self, tuple_of_coordinates, initiative):
31        super().__init__(tuple_of_coordinates)31        super().__init__(tuple_of_coordinates)
32        self.__initiative = initiative32        self.__initiative = initiative
33        self.box_of_candies = []33        self.box_of_candies = []
34        self._level_of_uranium = 0.034        self._level_of_uranium = 0.0
35        self.sorted_hosts = []35        self.sorted_hosts = []
36        self.is_adult = False36        self.is_adult = False
37        self.is_alive = True37        self.is_alive = True
3838
39    def get_initiative(self):39    def get_initiative(self):
40        return self.__initiative40        return self.__initiative
4141
42    def add_candy(self, candy):42    def add_candy(self, candy):
43        self.box_of_candies.append(candy)43        self.box_of_candies.append(candy)
44        self._level_of_uranium += candy.get_uranium_quantity()44        self._level_of_uranium += candy.get_uranium_quantity()
4545
46    def is_critical(self):46    def is_critical(self):
47        return self._level_of_uranium > MAX_URANIUM47        return self._level_of_uranium > MAX_URANIUM
4848
49    def __gt__(self, other):49    def __gt__(self, other):
50        return self.get_initiative() > other.get_initiative()50        return self.get_initiative() > other.get_initiative()
5151
5252
53class Host(Person):53class Host(Person):
54    def __init__(self, tuple_of_coordinates, candies):54    def __init__(self, tuple_of_coordinates, candies):
55        super().__init__(tuple_of_coordinates)55        super().__init__(tuple_of_coordinates)
56        self.__candies = candies56        self.__candies = candies
57        self.box_of_candies = [Candy(candy_params[0], candy_params[1]) for candy_params in candies]57        self.box_of_candies = [Candy(candy_params[0], candy_params[1]) for candy_params in candies]
58        self.is_adult = True58        self.is_adult = True
59        self.sorted_kids = []59        self.sorted_kids = []
6060
61    @staticmethod61    @staticmethod
62    def heaviest_candy(list_of_candies):62    def heaviest_candy(list_of_candies):
63        max_candy = None63        max_candy = None
64        max_candy_size = 064        max_candy_size = 0
65        for candy in list_of_candies:65        for candy in list_of_candies:
66            if candy.get_mass() > max_candy_size:66            if candy.get_mass() > max_candy_size:
67                max_candy_size = candy.get_mass()67                max_candy_size = candy.get_mass()
68                max_candy = candy68                max_candy = candy
6969
70        return max_candy70        return max_candy
7171
72    @property72    @property
73    def is_out_of_candy(self):73    def is_out_of_candy(self):
74        return len(self.box_of_candies) == 074        return len(self.box_of_candies) == 0
7575
76    def remove_candy(self, remove_candy_algorithm):76    def remove_candy(self, remove_candy_algorithm):
77        if self.is_out_of_candy:77        if self.is_out_of_candy:
78            return None78            return None
79        return remove_candy_algorithm79        return remove_candy_algorithm
8080
81    def remove_candy_by_mass(self, list_of_candies):81    def remove_candy_by_mass(self, list_of_candies):
82        list_of_candies.remove(self.heaviest_candy(list_of_candies))82        list_of_candies.remove(self.heaviest_candy(list_of_candies))
83        return self.heaviest_candy(list_of_candies)83        return self.heaviest_candy(list_of_candies)
8484
8585
86class FluxCapacitor:86class FluxCapacitor:
87    def __init__(self, participants):87    def __init__(self, participants):
88        self.participants = participants88        self.participants = participants
89        self.kids = {}89        self.kids = {}
90        self.hosts = {}90        self.hosts = {}
91        self.classify_participants()91        self.classify_participants()
9292
93    @staticmethod93    @staticmethod
94    def unalive_person(dict_of_people, person, is_alive):94    def unalive_person(dict_of_people, person, is_alive):
95        dict_of_people[person] = is_alive95        dict_of_people[person] = is_alive
9696
97    @staticmethod97    @staticmethod
98    def get_closest_distance_to_host(dict_of_hosts, target_coordinates):98    def get_closest_distance_to_host(dict_of_hosts, target_coordinates):
99        list_of_hosts = [host for host in dict_of_hosts]99        list_of_hosts = [host for host in dict_of_hosts]
t100        #print(list_of_hosts)t
101        min_distance = MAX_INT100        min_distance = MAX_INT
102        min_coordinates = (MAX_INT, MAX_INT)101        min_coordinates = (MAX_INT, MAX_INT)
103        closest_host = None102        closest_host = None
104        for host in list_of_hosts:103        for host in list_of_hosts:
105            tuple_of_coordinates = host.get_position()104            tuple_of_coordinates = host.get_position()
106            distance = math.dist(tuple_of_coordinates, target_coordinates)105            distance = math.dist(tuple_of_coordinates, target_coordinates)
107            if distance < min_distance:106            if distance < min_distance:
108                min_distance = distance107                min_distance = distance
109                min_coordinates = tuple_of_coordinates108                min_coordinates = tuple_of_coordinates
110                closest_host = host109                closest_host = host
111            elif distance == min_distance:110            elif distance == min_distance:
112                if tuple_of_coordinates[0] < min_coordinates[0]:111                if tuple_of_coordinates[0] < min_coordinates[0]:
113                    min_coordinates = tuple_of_coordinates112                    min_coordinates = tuple_of_coordinates
114                    closest_host = host113                    closest_host = host
115                elif tuple_of_coordinates[0] == min_coordinates[0]:114                elif tuple_of_coordinates[0] == min_coordinates[0]:
116                    min_coordinates = tuple_of_coordinates if tuple_of_coordinates[1] > min_coordinates[1] else min_coordinates115                    min_coordinates = tuple_of_coordinates if tuple_of_coordinates[1] > min_coordinates[1] else min_coordinates
117                    closest_host = host if tuple_of_coordinates[1] < min_coordinates[1] else closest_host116                    closest_host = host if tuple_of_coordinates[1] < min_coordinates[1] else closest_host
118        return closest_host117        return closest_host
119118
120119
121    def sort_hosts_by_distance_from_kid(self, kid):120    def sort_hosts_by_distance_from_kid(self, kid):
122        copy_of_hosts = self.hosts.copy()121        copy_of_hosts = self.hosts.copy()
123        while len(copy_of_hosts) != 0:122        while len(copy_of_hosts) != 0:
124            closest_host = self.get_closest_distance_to_host(copy_of_hosts, kid.get_position())123            closest_host = self.get_closest_distance_to_host(copy_of_hosts, kid.get_position())
125            if closest_host not in kid.sorted_hosts:124            if closest_host not in kid.sorted_hosts:
126                kid.sorted_hosts.append(closest_host)125                kid.sorted_hosts.append(closest_host)
127            popped = copy_of_hosts.pop(closest_host)126            popped = copy_of_hosts.pop(closest_host)
128127
129        return kid.sorted_hosts128        return kid.sorted_hosts
130129
131    def sort_kids_by_initiative_for_host(self, host):130    def sort_kids_by_initiative_for_host(self, host):
132        copy_of_kids = self.kids.copy()131        copy_of_kids = self.kids.copy()
133        for kid in copy_of_kids:132        for kid in copy_of_kids:
134            if host == self.get_closest_distance_to_host(self.hosts, kid.get_position()):133            if host == self.get_closest_distance_to_host(self.hosts, kid.get_position()):
135                if kid not in host.sorted_kids and self.kids[kid] == False:134                if kid not in host.sorted_kids and self.kids[kid] == False:
136                    host.sorted_kids.append(kid)135                    host.sorted_kids.append(kid)
137        host.sorted_kids.sort(key=lambda kid:kid.get_initiative(), reverse=True)136        host.sorted_kids.sort(key=lambda kid:kid.get_initiative(), reverse=True)
138        return host.sorted_kids137        return host.sorted_kids
139138
140    def classify_participants(self):139    def classify_participants(self):
141        for person in self.participants:140        for person in self.participants:
142            if not person.is_adult:141            if not person.is_adult:
143                self.unalive_person(self.kids, person, False)142                self.unalive_person(self.kids, person, False)
144            else:143            else:
145                if len(person.box_of_candies) != 0:144                if len(person.box_of_candies) != 0:
146                   self.unalive_person(self.hosts, person, False)145                   self.unalive_person(self.hosts, person, False)
147146
148    def get_victim(self):147    def get_victim(self):
149        dead_children = set()148        dead_children = set()
150149
151        while True:150        while True:
152            if not self.hosts:151            if not self.hosts:
153                if bool(dead_children) != 0:152                if bool(dead_children) != 0:
154                    return dead_children153                    return dead_children
155                else:154                else:
156                    return None155                    return None
157            for kid in self.kids:156            for kid in self.kids:
158                self.sort_hosts_by_distance_from_kid(kid)157                self.sort_hosts_by_distance_from_kid(kid)
159                closest_host = self.get_closest_distance_to_host(self.hosts, kid.get_position())158                closest_host = self.get_closest_distance_to_host(self.hosts, kid.get_position())
160159
161                if self.kids[kid] is True:160                if self.kids[kid] is True:
162                    if kid in closest_host.sorted_kids:161                    if kid in closest_host.sorted_kids:
163                        closest_host.sorted_kids.remove(kid)162                        closest_host.sorted_kids.remove(kid)
164                    continue163                    continue
165164
166                try:165                try:
167                    out_of_candy = closest_host.is_out_of_candy166                    out_of_candy = closest_host.is_out_of_candy
168                except AttributeError:167                except AttributeError:
169                    continue168                    continue
170169
171                if closest_host.is_out_of_candy:170                if closest_host.is_out_of_candy:
172                    kid.set_position(closest_host.get_position())171                    kid.set_position(closest_host.get_position())
173172
174                    if closest_host in kid.sorted_hosts:173                    if closest_host in kid.sorted_hosts:
175                        kid.sorted_hosts.remove(closest_host)174                        kid.sorted_hosts.remove(closest_host)
176                    continue175                    continue
177176
178                self.sort_kids_by_initiative_for_host(closest_host)177                self.sort_kids_by_initiative_for_host(closest_host)
179                if kid == closest_host.sorted_kids[0]:178                if kid == closest_host.sorted_kids[0]:
180179
181                    given_candy = closest_host.heaviest_candy(closest_host.box_of_candies)180                    given_candy = closest_host.heaviest_candy(closest_host.box_of_candies)
182                    kid.add_candy(given_candy)181                    kid.add_candy(given_candy)
183                    closest_host.remove_candy_by_mass(closest_host.box_of_candies)182                    closest_host.remove_candy_by_mass(closest_host.box_of_candies)
184183
185                    kid.set_position(closest_host.get_position())184                    kid.set_position(closest_host.get_position())
186185
187                    closest_host.sorted_kids.remove(kid)186                    closest_host.sorted_kids.remove(kid)
188                    kid.sorted_hosts.remove(closest_host)187                    kid.sorted_hosts.remove(closest_host)
189188
190                if kid.is_critical():189                if kid.is_critical():
191                    dead_children.add(kid)190                    dead_children.add(kid)
192                    self.unalive_person(self.kids, kid, True)191                    self.unalive_person(self.kids, kid, True)
193192
194                if closest_host.is_out_of_candy:193                if closest_host.is_out_of_candy:
195                    self.hosts.pop(closest_host)194                    self.hosts.pop(closest_host)
196195
197            if bool(dead_children) != 0:196            if bool(dead_children) != 0:
198                 return dead_children197                 return dead_children
199198
200        return dead_children199        return dead_children
201200
202201
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
22
3MAX_INT = math.inf3MAX_INT = math.inf
4MAX_URANIUM = 204MAX_URANIUM = 20
55
6class Candy:6class Candy:
7    def __init__(self, mass, uranium):7    def __init__(self, mass, uranium):
8        self.__mass = mass8        self.__mass = mass
9        self.__uranium = uranium9        self.__uranium = uranium
1010
11    def get_uranium_quantity(self):11    def get_uranium_quantity(self):
12        return self.__mass * self.__uranium12        return self.__mass * self.__uranium
1313
14    def get_mass(self):14    def get_mass(self):
15        return self.__mass15        return self.__mass
1616
1717
18class Person:18class Person:
19    def __init__(self, tuple_of_coordinates):19    def __init__(self, tuple_of_coordinates):
20        self.tuple_of_coordinates = tuple_of_coordinates20        self.tuple_of_coordinates = tuple_of_coordinates
2121
22    def set_position(self, new_tuple_of_coordinates):22    def set_position(self, new_tuple_of_coordinates):
23        self.tuple_of_coordinates = new_tuple_of_coordinates23        self.tuple_of_coordinates = new_tuple_of_coordinates
2424
25    def get_position(self):25    def get_position(self):
26        return self.tuple_of_coordinates26        return self.tuple_of_coordinates
2727
2828
29class Kid(Person):29class Kid(Person):
30    def __init__(self, tuple_of_coordinates, initiative):30    def __init__(self, tuple_of_coordinates, initiative):
31        super().__init__(tuple_of_coordinates)31        super().__init__(tuple_of_coordinates)
32        self.__initiative = initiative32        self.__initiative = initiative
33        self.box_of_candies = []33        self.box_of_candies = []
34        self._level_of_uranium = 0.034        self._level_of_uranium = 0.0
35        self.sorted_hosts = []35        self.sorted_hosts = []
36        self.is_adult = False36        self.is_adult = False
37        self.is_alive = True37        self.is_alive = True
3838
39    def get_initiative(self):39    def get_initiative(self):
40        return self.__initiative40        return self.__initiative
4141
42    def add_candy(self, candy):42    def add_candy(self, candy):
43        self.box_of_candies.append(candy)43        self.box_of_candies.append(candy)
44        self._level_of_uranium += candy.get_uranium_quantity()44        self._level_of_uranium += candy.get_uranium_quantity()
4545
46    def is_critical(self):46    def is_critical(self):
47        return self._level_of_uranium > MAX_URANIUM47        return self._level_of_uranium > MAX_URANIUM
4848
49    def __gt__(self, other):49    def __gt__(self, other):
50        return self.get_initiative() > other.get_initiative()50        return self.get_initiative() > other.get_initiative()
5151
5252
53class Host(Person):53class Host(Person):
54    def __init__(self, tuple_of_coordinates, candies):54    def __init__(self, tuple_of_coordinates, candies):
55        super().__init__(tuple_of_coordinates)55        super().__init__(tuple_of_coordinates)
56        self.__candies = candies56        self.__candies = candies
57        self.box_of_candies = [Candy(candy_params[0], candy_params[1]) for candy_params in candies]57        self.box_of_candies = [Candy(candy_params[0], candy_params[1]) for candy_params in candies]
58        self.is_adult = True58        self.is_adult = True
59        self.sorted_kids = []59        self.sorted_kids = []
6060
61    @staticmethod61    @staticmethod
62    def heaviest_candy(list_of_candies):62    def heaviest_candy(list_of_candies):
63        max_candy = None63        max_candy = None
64        max_candy_size = 064        max_candy_size = 0
65        for candy in list_of_candies:65        for candy in list_of_candies:
66            if candy.get_mass() > max_candy_size:66            if candy.get_mass() > max_candy_size:
67                max_candy_size = candy.get_mass()67                max_candy_size = candy.get_mass()
68                max_candy = candy68                max_candy = candy
6969
70        return max_candy70        return max_candy
7171
72    @property72    @property
73    def is_out_of_candy(self):73    def is_out_of_candy(self):
74        return len(self.box_of_candies) == 074        return len(self.box_of_candies) == 0
7575
76    def remove_candy(self, remove_candy_algorithm):76    def remove_candy(self, remove_candy_algorithm):
77        if self.is_out_of_candy:77        if self.is_out_of_candy:
78            return None78            return None
79        return remove_candy_algorithm79        return remove_candy_algorithm
8080
81    def remove_candy_by_mass(self, list_of_candies):81    def remove_candy_by_mass(self, list_of_candies):
82        list_of_candies.remove(self.heaviest_candy(list_of_candies))82        list_of_candies.remove(self.heaviest_candy(list_of_candies))
83        return self.heaviest_candy(list_of_candies)83        return self.heaviest_candy(list_of_candies)
8484
8585
86class FluxCapacitor:86class FluxCapacitor:
87    def __init__(self, participants):87    def __init__(self, participants):
88        self.participants = participants88        self.participants = participants
89        self.kids = {}89        self.kids = {}
90        self.hosts = {}90        self.hosts = {}
91        self.classify_participants()91        self.classify_participants()
9292
93    @staticmethod93    @staticmethod
94    def unalive_person(dict_of_people, person, is_alive):94    def unalive_person(dict_of_people, person, is_alive):
95        dict_of_people[person] = is_alive95        dict_of_people[person] = is_alive
9696
97    @staticmethod97    @staticmethod
98    def get_closest_distance_to_host(dict_of_hosts, target_coordinates):98    def get_closest_distance_to_host(dict_of_hosts, target_coordinates):
99        list_of_hosts = [host for host in dict_of_hosts]99        list_of_hosts = [host for host in dict_of_hosts]
100        #print(list_of_hosts)100        #print(list_of_hosts)
101        min_distance = MAX_INT101        min_distance = MAX_INT
102        min_coordinates = (MAX_INT, MAX_INT)102        min_coordinates = (MAX_INT, MAX_INT)
103        closest_host = None103        closest_host = None
104        for host in list_of_hosts:104        for host in list_of_hosts:
105            tuple_of_coordinates = host.get_position()105            tuple_of_coordinates = host.get_position()
106            distance = math.dist(tuple_of_coordinates, target_coordinates)106            distance = math.dist(tuple_of_coordinates, target_coordinates)
107            if distance < min_distance:107            if distance < min_distance:
108                min_distance = distance108                min_distance = distance
109                min_coordinates = tuple_of_coordinates109                min_coordinates = tuple_of_coordinates
110                closest_host = host110                closest_host = host
111            elif distance == min_distance:111            elif distance == min_distance:
112                if tuple_of_coordinates[0] < min_coordinates[0]:112                if tuple_of_coordinates[0] < min_coordinates[0]:
113                    min_coordinates = tuple_of_coordinates113                    min_coordinates = tuple_of_coordinates
114                    closest_host = host114                    closest_host = host
115                elif tuple_of_coordinates[0] == min_coordinates[0]:115                elif tuple_of_coordinates[0] == min_coordinates[0]:
n116                    min_coordinates = tuple_of_coordinates if tuple_of_coordinates[1] < min_coordinates[1] else min_coordinatesn116                    min_coordinates = tuple_of_coordinates if tuple_of_coordinates[1] > min_coordinates[1] else min_coordinates
117                    closest_host = host if tuple_of_coordinates[1] < min_coordinates[1] else closest_host117                    closest_host = host if tuple_of_coordinates[1] < min_coordinates[1] else closest_host
118        return closest_host118        return closest_host
119119
120120
121    def sort_hosts_by_distance_from_kid(self, kid):121    def sort_hosts_by_distance_from_kid(self, kid):
122        copy_of_hosts = self.hosts.copy()122        copy_of_hosts = self.hosts.copy()
123        while len(copy_of_hosts) != 0:123        while len(copy_of_hosts) != 0:
124            closest_host = self.get_closest_distance_to_host(copy_of_hosts, kid.get_position())124            closest_host = self.get_closest_distance_to_host(copy_of_hosts, kid.get_position())
125            if closest_host not in kid.sorted_hosts:125            if closest_host not in kid.sorted_hosts:
126                kid.sorted_hosts.append(closest_host)126                kid.sorted_hosts.append(closest_host)
127            popped = copy_of_hosts.pop(closest_host)127            popped = copy_of_hosts.pop(closest_host)
128128
129        return kid.sorted_hosts129        return kid.sorted_hosts
130130
131    def sort_kids_by_initiative_for_host(self, host):131    def sort_kids_by_initiative_for_host(self, host):
132        copy_of_kids = self.kids.copy()132        copy_of_kids = self.kids.copy()
133        for kid in copy_of_kids:133        for kid in copy_of_kids:
134            if host == self.get_closest_distance_to_host(self.hosts, kid.get_position()):134            if host == self.get_closest_distance_to_host(self.hosts, kid.get_position()):
135                if kid not in host.sorted_kids and self.kids[kid] == False:135                if kid not in host.sorted_kids and self.kids[kid] == False:
136                    host.sorted_kids.append(kid)136                    host.sorted_kids.append(kid)
137        host.sorted_kids.sort(key=lambda kid:kid.get_initiative(), reverse=True)137        host.sorted_kids.sort(key=lambda kid:kid.get_initiative(), reverse=True)
138        return host.sorted_kids138        return host.sorted_kids
139139
140    def classify_participants(self):140    def classify_participants(self):
141        for person in self.participants:141        for person in self.participants:
142            if not person.is_adult:142            if not person.is_adult:
143                self.unalive_person(self.kids, person, False)143                self.unalive_person(self.kids, person, False)
144            else:144            else:
145                if len(person.box_of_candies) != 0:145                if len(person.box_of_candies) != 0:
146                   self.unalive_person(self.hosts, person, False)146                   self.unalive_person(self.hosts, person, False)
147147
148    def get_victim(self):148    def get_victim(self):
149        dead_children = set()149        dead_children = set()
150150
151        while True:151        while True:
152            if not self.hosts:152            if not self.hosts:
153                if bool(dead_children) != 0:153                if bool(dead_children) != 0:
154                    return dead_children154                    return dead_children
155                else:155                else:
156                    return None156                    return None
157            for kid in self.kids:157            for kid in self.kids:
158                self.sort_hosts_by_distance_from_kid(kid)158                self.sort_hosts_by_distance_from_kid(kid)
159                closest_host = self.get_closest_distance_to_host(self.hosts, kid.get_position())159                closest_host = self.get_closest_distance_to_host(self.hosts, kid.get_position())
160160
161                if self.kids[kid] is True:161                if self.kids[kid] is True:
162                    if kid in closest_host.sorted_kids:162                    if kid in closest_host.sorted_kids:
163                        closest_host.sorted_kids.remove(kid)163                        closest_host.sorted_kids.remove(kid)
164                    continue164                    continue
165165
166                try:166                try:
167                    out_of_candy = closest_host.is_out_of_candy167                    out_of_candy = closest_host.is_out_of_candy
168                except AttributeError:168                except AttributeError:
169                    continue169                    continue
170170
171                if closest_host.is_out_of_candy:171                if closest_host.is_out_of_candy:
172                    kid.set_position(closest_host.get_position())172                    kid.set_position(closest_host.get_position())
173173
174                    if closest_host in kid.sorted_hosts:174                    if closest_host in kid.sorted_hosts:
175                        kid.sorted_hosts.remove(closest_host)175                        kid.sorted_hosts.remove(closest_host)
176                    continue176                    continue
177177
178                self.sort_kids_by_initiative_for_host(closest_host)178                self.sort_kids_by_initiative_for_host(closest_host)
179                if kid == closest_host.sorted_kids[0]:179                if kid == closest_host.sorted_kids[0]:
180180
181                    given_candy = closest_host.heaviest_candy(closest_host.box_of_candies)181                    given_candy = closest_host.heaviest_candy(closest_host.box_of_candies)
182                    kid.add_candy(given_candy)182                    kid.add_candy(given_candy)
183                    closest_host.remove_candy_by_mass(closest_host.box_of_candies)183                    closest_host.remove_candy_by_mass(closest_host.box_of_candies)
184184
185                    kid.set_position(closest_host.get_position())185                    kid.set_position(closest_host.get_position())
186186
187                    closest_host.sorted_kids.remove(kid)187                    closest_host.sorted_kids.remove(kid)
188                    kid.sorted_hosts.remove(closest_host)188                    kid.sorted_hosts.remove(closest_host)
189189
190                if kid.is_critical():190                if kid.is_critical():
191                    dead_children.add(kid)191                    dead_children.add(kid)
192                    self.unalive_person(self.kids, kid, True)192                    self.unalive_person(self.kids, kid, True)
193193
194                if closest_host.is_out_of_candy:194                if closest_host.is_out_of_candy:
195                    self.hosts.pop(closest_host)195                    self.hosts.pop(closest_host)
196196
197            if bool(dead_children) != 0:197            if bool(dead_children) != 0:
198                 return dead_children198                 return dead_children
199199
200        return dead_children200        return dead_children
201201
202202
t203 t
204 
205 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
n2import sysn
32
n4MAX_INT = sys.maxsizen3MAX_INT = math.inf
4MAX_URANIUM = 20
55
6class Candy:6class Candy:
7    def __init__(self, mass, uranium):7    def __init__(self, mass, uranium):
8        self.__mass = mass8        self.__mass = mass
9        self.__uranium = uranium9        self.__uranium = uranium
1010
11    def get_uranium_quantity(self):11    def get_uranium_quantity(self):
12        return self.__mass * self.__uranium12        return self.__mass * self.__uranium
1313
14    def get_mass(self):14    def get_mass(self):
15        return self.__mass15        return self.__mass
nn16 
1617
17class Person:18class Person:
18    def __init__(self, tuple_of_coordinates):19    def __init__(self, tuple_of_coordinates):
19        self.tuple_of_coordinates = tuple_of_coordinates20        self.tuple_of_coordinates = tuple_of_coordinates
2021
21    def set_position(self, new_tuple_of_coordinates):22    def set_position(self, new_tuple_of_coordinates):
22        self.tuple_of_coordinates = new_tuple_of_coordinates23        self.tuple_of_coordinates = new_tuple_of_coordinates
2324
24    def get_position(self):25    def get_position(self):
25        return self.tuple_of_coordinates26        return self.tuple_of_coordinates
2627
2728
28class Kid(Person):29class Kid(Person):
29    def __init__(self, tuple_of_coordinates, initiative):30    def __init__(self, tuple_of_coordinates, initiative):
30        super().__init__(tuple_of_coordinates)31        super().__init__(tuple_of_coordinates)
31        self.__initiative = initiative32        self.__initiative = initiative
32        self.box_of_candies = []33        self.box_of_candies = []
33        self._level_of_uranium = 0.034        self._level_of_uranium = 0.0
34        self.sorted_hosts = []35        self.sorted_hosts = []
35        self.is_adult = False36        self.is_adult = False
36        self.is_alive = True37        self.is_alive = True
3738
38    def get_initiative(self):39    def get_initiative(self):
39        return self.__initiative40        return self.__initiative
4041
41    def add_candy(self, candy):42    def add_candy(self, candy):
42        self.box_of_candies.append(candy)43        self.box_of_candies.append(candy)
43        self._level_of_uranium += candy.get_uranium_quantity()44        self._level_of_uranium += candy.get_uranium_quantity()
4445
45    def is_critical(self):46    def is_critical(self):
n46        if self._level_of_uranium > 20:n47        return self._level_of_uranium > MAX_URANIUM
47            return True
48        return False
4948
50    def __gt__(self, other):49    def __gt__(self, other):
51        return self.get_initiative() > other.get_initiative()50        return self.get_initiative() > other.get_initiative()
5251
5352
54class Host(Person):53class Host(Person):
55    def __init__(self, tuple_of_coordinates, candies):54    def __init__(self, tuple_of_coordinates, candies):
56        super().__init__(tuple_of_coordinates)55        super().__init__(tuple_of_coordinates)
57        self.__candies = candies56        self.__candies = candies
58        self.box_of_candies = [Candy(candy_params[0], candy_params[1]) for candy_params in candies]57        self.box_of_candies = [Candy(candy_params[0], candy_params[1]) for candy_params in candies]
59        self.is_adult = True58        self.is_adult = True
60        self.sorted_kids = []59        self.sorted_kids = []
6160
62    @staticmethod61    @staticmethod
63    def heaviest_candy(list_of_candies):62    def heaviest_candy(list_of_candies):
64        max_candy = None63        max_candy = None
65        max_candy_size = 064        max_candy_size = 0
66        for candy in list_of_candies:65        for candy in list_of_candies:
67            if candy.get_mass() > max_candy_size:66            if candy.get_mass() > max_candy_size:
68                max_candy_size = candy.get_mass()67                max_candy_size = candy.get_mass()
69                max_candy = candy68                max_candy = candy
7069
71        return max_candy70        return max_candy
7271
73    @property72    @property
74    def is_out_of_candy(self):73    def is_out_of_candy(self):
75        return len(self.box_of_candies) == 074        return len(self.box_of_candies) == 0
7675
77    def remove_candy(self, remove_candy_algorithm):76    def remove_candy(self, remove_candy_algorithm):
78        if self.is_out_of_candy:77        if self.is_out_of_candy:
79            return None78            return None
80        return remove_candy_algorithm79        return remove_candy_algorithm
8180
82    def remove_candy_by_mass(self, list_of_candies):81    def remove_candy_by_mass(self, list_of_candies):
83        list_of_candies.remove(self.heaviest_candy(list_of_candies))82        list_of_candies.remove(self.heaviest_candy(list_of_candies))
84        return self.heaviest_candy(list_of_candies)83        return self.heaviest_candy(list_of_candies)
8584
8685
87class FluxCapacitor:86class FluxCapacitor:
88    def __init__(self, participants):87    def __init__(self, participants):
89        self.participants = participants88        self.participants = participants
90        self.kids = {}89        self.kids = {}
91        self.hosts = {}90        self.hosts = {}
92        self.classify_participants()91        self.classify_participants()
9392
94    @staticmethod93    @staticmethod
95    def unalive_person(dict_of_people, person, is_alive):94    def unalive_person(dict_of_people, person, is_alive):
96        dict_of_people[person] = is_alive95        dict_of_people[person] = is_alive
9796
98    @staticmethod97    @staticmethod
99    def get_closest_distance_to_host(dict_of_hosts, target_coordinates):98    def get_closest_distance_to_host(dict_of_hosts, target_coordinates):
100        list_of_hosts = [host for host in dict_of_hosts]99        list_of_hosts = [host for host in dict_of_hosts]
101        #print(list_of_hosts)100        #print(list_of_hosts)
102        min_distance = MAX_INT101        min_distance = MAX_INT
103        min_coordinates = (MAX_INT, MAX_INT)102        min_coordinates = (MAX_INT, MAX_INT)
104        closest_host = None103        closest_host = None
105        for host in list_of_hosts:104        for host in list_of_hosts:
106            tuple_of_coordinates = host.get_position()105            tuple_of_coordinates = host.get_position()
107            distance = math.dist(tuple_of_coordinates, target_coordinates)106            distance = math.dist(tuple_of_coordinates, target_coordinates)
108            if distance < min_distance:107            if distance < min_distance:
109                min_distance = distance108                min_distance = distance
110                min_coordinates = tuple_of_coordinates109                min_coordinates = tuple_of_coordinates
111                closest_host = host110                closest_host = host
112            elif distance == min_distance:111            elif distance == min_distance:
113                if tuple_of_coordinates[0] < min_coordinates[0]:112                if tuple_of_coordinates[0] < min_coordinates[0]:
114                    min_coordinates = tuple_of_coordinates113                    min_coordinates = tuple_of_coordinates
115                    closest_host = host114                    closest_host = host
116                elif tuple_of_coordinates[0] == min_coordinates[0]:115                elif tuple_of_coordinates[0] == min_coordinates[0]:
117                    min_coordinates = tuple_of_coordinates if tuple_of_coordinates[1] < min_coordinates[1] else min_coordinates116                    min_coordinates = tuple_of_coordinates if tuple_of_coordinates[1] < min_coordinates[1] else min_coordinates
118                    closest_host = host if tuple_of_coordinates[1] < min_coordinates[1] else closest_host117                    closest_host = host if tuple_of_coordinates[1] < min_coordinates[1] else closest_host
119        return closest_host118        return closest_host
120119
121120
122    def sort_hosts_by_distance_from_kid(self, kid):121    def sort_hosts_by_distance_from_kid(self, kid):
123        copy_of_hosts = self.hosts.copy()122        copy_of_hosts = self.hosts.copy()
124        while len(copy_of_hosts) != 0:123        while len(copy_of_hosts) != 0:
125            closest_host = self.get_closest_distance_to_host(copy_of_hosts, kid.get_position())124            closest_host = self.get_closest_distance_to_host(copy_of_hosts, kid.get_position())
126            if closest_host not in kid.sorted_hosts:125            if closest_host not in kid.sorted_hosts:
127                kid.sorted_hosts.append(closest_host)126                kid.sorted_hosts.append(closest_host)
128            popped = copy_of_hosts.pop(closest_host)127            popped = copy_of_hosts.pop(closest_host)
129128
130        return kid.sorted_hosts129        return kid.sorted_hosts
131130
132    def sort_kids_by_initiative_for_host(self, host):131    def sort_kids_by_initiative_for_host(self, host):
133        copy_of_kids = self.kids.copy()132        copy_of_kids = self.kids.copy()
n134        #print(copy_of_kids)n
135        for kid in copy_of_kids:133        for kid in copy_of_kids:
136            if host == self.get_closest_distance_to_host(self.hosts, kid.get_position()):134            if host == self.get_closest_distance_to_host(self.hosts, kid.get_position()):
n137                if kid not in host.sorted_kids:n135                if kid not in host.sorted_kids and self.kids[kid] == False:
138                    host.sorted_kids.append(kid)136                    host.sorted_kids.append(kid)
n139                #popped = copy_of_kids.pop(kid)n
140        #print(host.sorted_kids)
141        host.sorted_kids.sort(key=lambda kid:kid.get_initiative(), reverse=True)137        host.sorted_kids.sort(key=lambda kid:kid.get_initiative(), reverse=True)
142        return host.sorted_kids138        return host.sorted_kids
143139
144    def classify_participants(self):140    def classify_participants(self):
145        for person in self.participants:141        for person in self.participants:
146            if not person.is_adult:142            if not person.is_adult:
147                self.unalive_person(self.kids, person, False)143                self.unalive_person(self.kids, person, False)
148            else:144            else:
nn145                if len(person.box_of_candies) != 0:
149                self.unalive_person(self.hosts, person, False)146                   self.unalive_person(self.hosts, person, False)
147 
150    def get_victim(self):148    def get_victim(self):
151        dead_children = set()149        dead_children = set()
152150
153        while True:151        while True:
154            if not self.hosts:152            if not self.hosts:
155                if bool(dead_children) != 0:153                if bool(dead_children) != 0:
156                    return dead_children154                    return dead_children
157                else:155                else:
158                    return None156                    return None
159            for kid in self.kids:157            for kid in self.kids:
160                self.sort_hosts_by_distance_from_kid(kid)158                self.sort_hosts_by_distance_from_kid(kid)
161                closest_host = self.get_closest_distance_to_host(self.hosts, kid.get_position())159                closest_host = self.get_closest_distance_to_host(self.hosts, kid.get_position())
162160
n163                if self.kids[kid] == True:n161                if self.kids[kid] is True:
164                    if kid in closest_host.sorted_kids:162                    if kid in closest_host.sorted_kids:
165                        closest_host.sorted_kids.remove(kid)163                        closest_host.sorted_kids.remove(kid)
166                    continue164                    continue
167165
nn166                try:
167                    out_of_candy = closest_host.is_out_of_candy
168                except AttributeError:
169                    continue
170 
168                if closest_host.is_out_of_candy:171                if closest_host.is_out_of_candy:
169                    kid.set_position(closest_host.get_position())172                    kid.set_position(closest_host.get_position())
170173
171                    if closest_host in kid.sorted_hosts:174                    if closest_host in kid.sorted_hosts:
172                        kid.sorted_hosts.remove(closest_host)175                        kid.sorted_hosts.remove(closest_host)
173                    continue176                    continue
174177
175                self.sort_kids_by_initiative_for_host(closest_host)178                self.sort_kids_by_initiative_for_host(closest_host)
176                if kid == closest_host.sorted_kids[0]:179                if kid == closest_host.sorted_kids[0]:
177180
178                    given_candy = closest_host.heaviest_candy(closest_host.box_of_candies)181                    given_candy = closest_host.heaviest_candy(closest_host.box_of_candies)
179                    kid.add_candy(given_candy)182                    kid.add_candy(given_candy)
180                    closest_host.remove_candy_by_mass(closest_host.box_of_candies)183                    closest_host.remove_candy_by_mass(closest_host.box_of_candies)
181184
182                    kid.set_position(closest_host.get_position())185                    kid.set_position(closest_host.get_position())
183186
184                    closest_host.sorted_kids.remove(kid)187                    closest_host.sorted_kids.remove(kid)
185                    kid.sorted_hosts.remove(closest_host)188                    kid.sorted_hosts.remove(closest_host)
186189
187                if kid.is_critical():190                if kid.is_critical():
188                    dead_children.add(kid)191                    dead_children.add(kid)
189                    self.unalive_person(self.kids, kid, True)192                    self.unalive_person(self.kids, kid, True)
190193
191                if closest_host.is_out_of_candy:194                if closest_host.is_out_of_candy:
192                    self.hosts.pop(closest_host)195                    self.hosts.pop(closest_host)
193196
194            if bool(dead_children) != 0:197            if bool(dead_children) != 0:
195                 return dead_children198                 return dead_children
196199
197        return dead_children200        return dead_children
198201
199202
200203
201204
tt205 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op