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

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

9 точки общо

11 успешни теста
1 неуспешни теста
Код

  1class Candy:
  2    def __init__(self, mass, uranium):
  3        self.mass = mass
  4        self.uranium = uranium
  5        
  6    def get_uranium_quantity(self):
  7        return self.mass * self.uranium
  8    
  9    def get_mass(self):
 10        return self.mass
 11    
 12
 13class Person:
 14    def __init__(self, position):
 15        self.position = position
 16        
 17    def get_position(self):
 18        return self.position
 19    
 20    def set_postition(self, new_position):
 21        self.position = new_position
 22        
 23    def get_distance(self, other_person):
 24        return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2)
 25        
 26
 27class Kid(Person):
 28    MAX_URANIUM = 20
 29    def __init__(self, position, initiative):
 30        self.list_of_candies = []
 31        self.list_of_hosts = []
 32        self.sum_of_candies = 0
 33        super().__init__(position)
 34        self.initiative = initiative
 35    
 36    def get_initiative(self):
 37        return self.initiative
 38    
 39    def add_candy(self, candy):
 40        if candy is not None:
 41            self.candy = candy
 42            self.list_of_candies.append(candy)
 43        
 44    def go_to_host(self):
 45        if not self.list_of_hosts:
 46            return None
 47        
 48        min_dist = self.get_distance(self.list_of_hosts[0])
 49        min_host = self.list_of_hosts[0]
 50        
 51        for host in self.list_of_hosts:
 52            next_dist = self.get_distance(host)
 53            if min_dist > next_dist:
 54                min_dist = next_dist
 55                min_host = host
 56            elif min_dist == next_dist:
 57                if host.position[0] < min_host.position[0]:
 58                    min_dist = next_dist
 59                    min_host = host
 60                elif host.position[0] == min_host.position[0]:
 61                    if host.position[1] < min_host.position[1]:
 62                        min_dist = next_dist
 63                        min_host = host
 64        
 65        self.add_candy(min_host.remove_candy(Host.function_for_removal))
 66        self.position = min_host.position
 67        self.list_of_hosts.remove(min_host)
 68        
 69    def is_critical(self):
 70        self.sum_of_candies = 0
 71        for candy in self.list_of_candies:
 72            self.sum_of_candies += candy.get_uranium_quantity()
 73        return self.sum_of_candies > self.MAX_URANIUM
 74    
 75
 76class Host(Person):
 77    
 78    @staticmethod
 79    def function_for_removal(my_list_of_candies):
 80        return max(my_list_of_candies, key=lambda candy: candy.get_mass())
 81
 82    def __init__(self, position, candies):
 83        super().__init__(position)
 84        self.candies = []
 85        
 86        for candy in candies:
 87            self.candies.append(Candy(*candy))
 88        
 89    def remove_candy(self, remove_function):
 90        if len(self.candies) == 0:
 91            return None
 92        else:
 93            candy_to_remove = remove_function(self.candies)
 94            self.candies.remove(candy_to_remove)
 95            return candy_to_remove
 96      
 97      
 98class FluxCapacitor:
 99    def __init__(self, set_of_people):
100        self.set_of_dead = set()
101        self.list_of_children = []
102        self.list_of_hosts = []
103        
104        for person in set_of_people:
105            if isinstance(person, Kid):
106                self.list_of_children.append(person)
107            else:
108                self.list_of_hosts.append(person)
109                
110        for kid in self.list_of_children:
111            for host in self.list_of_hosts:
112                kid.list_of_hosts.append(host)
113        
114        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)
115    
116    def get_victim(self):
117        while True:
118            is_there_someone_to_visit = False
119            
120            for kid in self.list_of_children:
121                if len(kid.list_of_hosts) != 0:
122                    is_there_someone_to_visit = True
123                    break
124                
125            if is_there_someone_to_visit == False:
126                return None
127            
128            for kid in self.list_of_children:
129                kid.go_to_host()
130            
131            for kid in self.list_of_children:
132                if kid.is_critical():
133                    self.set_of_dead.add(kid)
134                    
135            if len(self.set_of_dead) != 0:
136                return self.set_of_dead
137            

...........E
======================================================================
ERROR: test_basic_usage (test.PersonTest)
Test basic usage of Person class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 25, in test_basic_usage
person.set_position((2, 2))
AttributeError: 'Person' object has no attribute 'set_position'

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

FAILED (errors=1)

Дискусия
Георги Кунчев
06.11.2023 21:39

Трябва да я оставиш, да. Но е по-добре да е статичен метод на класа.
Данаил Тодоров
06.11.2023 18:35

Не знам дали тази функция function_for_removal(гледа кой бонбон да махне, според масата) трябва да я оставя или не. П.П качих го втори път, защото не променях позицията на децата, когато отидат при някой хост
История

f1class Candy:f1class Candy:
2    def __init__(self, mass, uranium):2    def __init__(self, mass, uranium):
3        self.mass = mass3        self.mass = mass
4        self.uranium = uranium4        self.uranium = uranium
5        5        
6    def get_uranium_quantity(self):6    def get_uranium_quantity(self):
7        return self.mass * self.uranium7        return self.mass * self.uranium
8    8    
9    def get_mass(self):9    def get_mass(self):
10        return self.mass10        return self.mass
11    11    
1212
13class Person:13class Person:
14    def __init__(self, position):14    def __init__(self, position):
15        self.position = position15        self.position = position
16        16        
17    def get_position(self):17    def get_position(self):
18        return self.position18        return self.position
19    19    
20    def set_postition(self, new_position):20    def set_postition(self, new_position):
21        self.position = new_position21        self.position = new_position
22        22        
23    def get_distance(self, other_person):23    def get_distance(self, other_person):
24        return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2)24        return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2)
25        25        
2626
27class Kid(Person):27class Kid(Person):
28    MAX_URANIUM = 2028    MAX_URANIUM = 20
29    def __init__(self, position, initiative):29    def __init__(self, position, initiative):
30        self.list_of_candies = []30        self.list_of_candies = []
31        self.list_of_hosts = []31        self.list_of_hosts = []
32        self.sum_of_candies = 032        self.sum_of_candies = 0
33        super().__init__(position)33        super().__init__(position)
34        self.initiative = initiative34        self.initiative = initiative
35    35    
36    def get_initiative(self):36    def get_initiative(self):
37        return self.initiative37        return self.initiative
38    38    
39    def add_candy(self, candy):39    def add_candy(self, candy):
40        if candy is not None:40        if candy is not None:
41            self.candy = candy41            self.candy = candy
42            self.list_of_candies.append(candy)42            self.list_of_candies.append(candy)
43        43        
44    def go_to_host(self):44    def go_to_host(self):
45        if not self.list_of_hosts:45        if not self.list_of_hosts:
46            return None46            return None
47        47        
48        min_dist = self.get_distance(self.list_of_hosts[0])48        min_dist = self.get_distance(self.list_of_hosts[0])
n49        index_of_host = 0n49        min_host = self.list_of_hosts[0]
50        50        
n51        for curr_host_ind in range(1, len(self.list_of_hosts)):n51        for host in self.list_of_hosts:
52            next_dist = self.get_distance(self.list_of_hosts[curr_host_ind])52            next_dist = self.get_distance(host)
53            if min_dist > next_dist:53            if min_dist > next_dist:
54                min_dist = next_dist54                min_dist = next_dist
n55                index_of_host = curr_host_indn55                min_host = host
56            elif min_dist == next_dist:56            elif min_dist == next_dist:
n57                if self.list_of_hosts[curr_host_ind].position[0] < self.list_of_hosts[index_of_host].position[0]:n57                if host.position[0] < min_host.position[0]:
58                    min_dist = next_dist58                    min_dist = next_dist
n59                    index_of_host = curr_host_indn59                    min_host = host
60                elif self.list_of_hosts[curr_host_ind].position[0] == self.list_of_hosts[index_of_host].position[0]:60                elif host.position[0] == min_host.position[0]:
61                    if self.list_of_hosts[curr_host_ind].position[1] < self.list_of_hosts[index_of_host].position[1]:61                    if host.position[1] < min_host.position[1]:
62                        min_dist = next_dist62                        min_dist = next_dist
n63                        index_of_host = curr_host_indn63                        min_host = host
64        64        
n65        self.add_candy(self.list_of_hosts[index_of_host].remove_candy(Host.function_for_removal))n65        self.add_candy(min_host.remove_candy(Host.function_for_removal))
66        self.position = self.list_of_hosts[index_of_host].position66        self.position = min_host.position
67        self.list_of_hosts.pop(index_of_host)67        self.list_of_hosts.remove(min_host)
68        68        
69    def is_critical(self):69    def is_critical(self):
70        self.sum_of_candies = 070        self.sum_of_candies = 0
71        for candy in self.list_of_candies:71        for candy in self.list_of_candies:
72            self.sum_of_candies += candy.get_uranium_quantity()72            self.sum_of_candies += candy.get_uranium_quantity()
73        return self.sum_of_candies > self.MAX_URANIUM73        return self.sum_of_candies > self.MAX_URANIUM
74    74    
7575
76class Host(Person):76class Host(Person):
77    77    
78    @staticmethod78    @staticmethod
79    def function_for_removal(my_list_of_candies):79    def function_for_removal(my_list_of_candies):
80        return max(my_list_of_candies, key=lambda candy: candy.get_mass())80        return max(my_list_of_candies, key=lambda candy: candy.get_mass())
8181
82    def __init__(self, position, candies):82    def __init__(self, position, candies):
83        super().__init__(position)83        super().__init__(position)
84        self.candies = []84        self.candies = []
85        85        
86        for candy in candies:86        for candy in candies:
87            self.candies.append(Candy(*candy))87            self.candies.append(Candy(*candy))
88        88        
89    def remove_candy(self, remove_function):89    def remove_candy(self, remove_function):
90        if len(self.candies) == 0:90        if len(self.candies) == 0:
91            return None91            return None
92        else:92        else:
93            candy_to_remove = remove_function(self.candies)93            candy_to_remove = remove_function(self.candies)
94            self.candies.remove(candy_to_remove)94            self.candies.remove(candy_to_remove)
95            return candy_to_remove95            return candy_to_remove
96      96      
97      97      
98class FluxCapacitor:98class FluxCapacitor:
99    def __init__(self, set_of_people):99    def __init__(self, set_of_people):
100        self.set_of_dead = set()100        self.set_of_dead = set()
101        self.list_of_children = []101        self.list_of_children = []
102        self.list_of_hosts = []102        self.list_of_hosts = []
103        103        
104        for person in set_of_people:104        for person in set_of_people:
105            if isinstance(person, Kid):105            if isinstance(person, Kid):
106                self.list_of_children.append(person)106                self.list_of_children.append(person)
107            else:107            else:
108                self.list_of_hosts.append(person)108                self.list_of_hosts.append(person)
109                109                
110        for kid in self.list_of_children:110        for kid in self.list_of_children:
111            for host in self.list_of_hosts:111            for host in self.list_of_hosts:
112                kid.list_of_hosts.append(host)112                kid.list_of_hosts.append(host)
113        113        
114        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)114        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)
115    115    
116    def get_victim(self):116    def get_victim(self):
117        while True:117        while True:
118            is_there_someone_to_visit = False118            is_there_someone_to_visit = False
119            119            
120            for kid in self.list_of_children:120            for kid in self.list_of_children:
121                if len(kid.list_of_hosts) != 0:121                if len(kid.list_of_hosts) != 0:
122                    is_there_someone_to_visit = True122                    is_there_someone_to_visit = True
123                    break123                    break
124                124                
125            if is_there_someone_to_visit == False:125            if is_there_someone_to_visit == False:
126                return None126                return None
127            127            
128            for kid in self.list_of_children:128            for kid in self.list_of_children:
129                kid.go_to_host()129                kid.go_to_host()
130            130            
131            for kid in self.list_of_children:131            for kid in self.list_of_children:
132                if kid.is_critical():132                if kid.is_critical():
n133                    print(kid.initiative)n
134                    self.set_of_dead.add(kid)133                    self.set_of_dead.add(kid)
135                    134                    
136            if len(self.set_of_dead) != 0:135            if len(self.set_of_dead) != 0:
137                return self.set_of_dead136                return self.set_of_dead
138            137            
t139        t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class Candy:f1class Candy:
2    def __init__(self, mass, uranium):2    def __init__(self, mass, uranium):
3        self.mass = mass3        self.mass = mass
4        self.uranium = uranium4        self.uranium = uranium
5        5        
6    def get_uranium_quantity(self):6    def get_uranium_quantity(self):
7        return self.mass * self.uranium7        return self.mass * self.uranium
8    8    
9    def get_mass(self):9    def get_mass(self):
10        return self.mass10        return self.mass
11    11    
1212
13class Person:13class Person:
14    def __init__(self, position):14    def __init__(self, position):
15        self.position = position15        self.position = position
16        16        
17    def get_position(self):17    def get_position(self):
18        return self.position18        return self.position
19    19    
20    def set_postition(self, new_position):20    def set_postition(self, new_position):
21        self.position = new_position21        self.position = new_position
22        22        
23    def get_distance(self, other_person):23    def get_distance(self, other_person):
t24        return ((self.position[0] - other_person.position[0]) * 2 + (self.position[1] - other_person.position[1]) * 2) ** (1/2)t24        return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2)
25        25        
2626
27class Kid(Person):27class Kid(Person):
28    MAX_URANIUM = 2028    MAX_URANIUM = 20
29    def __init__(self, position, initiative):29    def __init__(self, position, initiative):
30        self.list_of_candies = []30        self.list_of_candies = []
31        self.list_of_hosts = []31        self.list_of_hosts = []
32        self.sum_of_candies = 032        self.sum_of_candies = 0
33        super().__init__(position)33        super().__init__(position)
34        self.initiative = initiative34        self.initiative = initiative
35    35    
36    def get_initiative(self):36    def get_initiative(self):
37        return self.initiative37        return self.initiative
38    38    
39    def add_candy(self, candy):39    def add_candy(self, candy):
40        if candy is not None:40        if candy is not None:
41            self.candy = candy41            self.candy = candy
42            self.list_of_candies.append(candy)42            self.list_of_candies.append(candy)
43        43        
44    def go_to_host(self):44    def go_to_host(self):
45        if not self.list_of_hosts:45        if not self.list_of_hosts:
46            return None46            return None
47        47        
48        min_dist = self.get_distance(self.list_of_hosts[0])48        min_dist = self.get_distance(self.list_of_hosts[0])
49        index_of_host = 049        index_of_host = 0
50        50        
51        for curr_host_ind in range(1, len(self.list_of_hosts)):51        for curr_host_ind in range(1, len(self.list_of_hosts)):
52            next_dist = self.get_distance(self.list_of_hosts[curr_host_ind])52            next_dist = self.get_distance(self.list_of_hosts[curr_host_ind])
53            if min_dist > next_dist:53            if min_dist > next_dist:
54                min_dist = next_dist54                min_dist = next_dist
55                index_of_host = curr_host_ind55                index_of_host = curr_host_ind
56            elif min_dist == next_dist:56            elif min_dist == next_dist:
57                if self.list_of_hosts[curr_host_ind].position[0] < self.list_of_hosts[index_of_host].position[0]:57                if self.list_of_hosts[curr_host_ind].position[0] < self.list_of_hosts[index_of_host].position[0]:
58                    min_dist = next_dist58                    min_dist = next_dist
59                    index_of_host = curr_host_ind59                    index_of_host = curr_host_ind
60                elif self.list_of_hosts[curr_host_ind].position[0] == self.list_of_hosts[index_of_host].position[0]:60                elif self.list_of_hosts[curr_host_ind].position[0] == self.list_of_hosts[index_of_host].position[0]:
61                    if self.list_of_hosts[curr_host_ind].position[1] < self.list_of_hosts[index_of_host].position[1]:61                    if self.list_of_hosts[curr_host_ind].position[1] < self.list_of_hosts[index_of_host].position[1]:
62                        min_dist = next_dist62                        min_dist = next_dist
63                        index_of_host = curr_host_ind63                        index_of_host = curr_host_ind
64        64        
65        self.add_candy(self.list_of_hosts[index_of_host].remove_candy(Host.function_for_removal))65        self.add_candy(self.list_of_hosts[index_of_host].remove_candy(Host.function_for_removal))
66        self.position = self.list_of_hosts[index_of_host].position66        self.position = self.list_of_hosts[index_of_host].position
67        self.list_of_hosts.pop(index_of_host)67        self.list_of_hosts.pop(index_of_host)
68        68        
69    def is_critical(self):69    def is_critical(self):
70        self.sum_of_candies = 070        self.sum_of_candies = 0
71        for candy in self.list_of_candies:71        for candy in self.list_of_candies:
72            self.sum_of_candies += candy.get_uranium_quantity()72            self.sum_of_candies += candy.get_uranium_quantity()
73        return self.sum_of_candies > self.MAX_URANIUM73        return self.sum_of_candies > self.MAX_URANIUM
74    74    
7575
76class Host(Person):76class Host(Person):
77    77    
78    @staticmethod78    @staticmethod
79    def function_for_removal(my_list_of_candies):79    def function_for_removal(my_list_of_candies):
80        return max(my_list_of_candies, key=lambda candy: candy.get_mass())80        return max(my_list_of_candies, key=lambda candy: candy.get_mass())
8181
82    def __init__(self, position, candies):82    def __init__(self, position, candies):
83        super().__init__(position)83        super().__init__(position)
84        self.candies = []84        self.candies = []
85        85        
86        for candy in candies:86        for candy in candies:
87            self.candies.append(Candy(*candy))87            self.candies.append(Candy(*candy))
88        88        
89    def remove_candy(self, remove_function):89    def remove_candy(self, remove_function):
90        if len(self.candies) == 0:90        if len(self.candies) == 0:
91            return None91            return None
92        else:92        else:
93            candy_to_remove = remove_function(self.candies)93            candy_to_remove = remove_function(self.candies)
94            self.candies.remove(candy_to_remove)94            self.candies.remove(candy_to_remove)
95            return candy_to_remove95            return candy_to_remove
96      96      
97      97      
98class FluxCapacitor:98class FluxCapacitor:
99    def __init__(self, set_of_people):99    def __init__(self, set_of_people):
100        self.set_of_dead = set()100        self.set_of_dead = set()
101        self.list_of_children = []101        self.list_of_children = []
102        self.list_of_hosts = []102        self.list_of_hosts = []
103        103        
104        for person in set_of_people:104        for person in set_of_people:
105            if isinstance(person, Kid):105            if isinstance(person, Kid):
106                self.list_of_children.append(person)106                self.list_of_children.append(person)
107            else:107            else:
108                self.list_of_hosts.append(person)108                self.list_of_hosts.append(person)
109                109                
110        for kid in self.list_of_children:110        for kid in self.list_of_children:
111            for host in self.list_of_hosts:111            for host in self.list_of_hosts:
112                kid.list_of_hosts.append(host)112                kid.list_of_hosts.append(host)
113        113        
114        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)114        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)
115    115    
116    def get_victim(self):116    def get_victim(self):
117        while True:117        while True:
118            is_there_someone_to_visit = False118            is_there_someone_to_visit = False
119            119            
120            for kid in self.list_of_children:120            for kid in self.list_of_children:
121                if len(kid.list_of_hosts) != 0:121                if len(kid.list_of_hosts) != 0:
122                    is_there_someone_to_visit = True122                    is_there_someone_to_visit = True
123                    break123                    break
124                124                
125            if is_there_someone_to_visit == False:125            if is_there_someone_to_visit == False:
126                return None126                return None
127            127            
128            for kid in self.list_of_children:128            for kid in self.list_of_children:
129                kid.go_to_host()129                kid.go_to_host()
130            130            
131            for kid in self.list_of_children:131            for kid in self.list_of_children:
132                if kid.is_critical():132                if kid.is_critical():
133                    print(kid.initiative)133                    print(kid.initiative)
134                    self.set_of_dead.add(kid)134                    self.set_of_dead.add(kid)
135                    135                    
136            if len(self.set_of_dead) != 0:136            if len(self.set_of_dead) != 0:
137                return self.set_of_dead137                return self.set_of_dead
138            138            
139        139        
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class Candy:f1class Candy:
2    def __init__(self, mass, uranium):2    def __init__(self, mass, uranium):
3        self.mass = mass3        self.mass = mass
4        self.uranium = uranium4        self.uranium = uranium
5        5        
6    def get_uranium_quantity(self):6    def get_uranium_quantity(self):
7        return self.mass * self.uranium7        return self.mass * self.uranium
8    8    
9    def get_mass(self):9    def get_mass(self):
10        return self.mass10        return self.mass
11    11    
1212
13class Person:13class Person:
14    def __init__(self, position):14    def __init__(self, position):
15        self.position = position15        self.position = position
16        16        
17    def get_position(self):17    def get_position(self):
18        return self.position18        return self.position
19    19    
20    def set_postition(self, new_position):20    def set_postition(self, new_position):
21        self.position = new_position21        self.position = new_position
22        22        
23    def get_distance(self, other_person):23    def get_distance(self, other_person):
n24        return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2)n24        return ((self.position[0] - other_person.position[0]) * 2 + (self.position[1] - other_person.position[1]) * 2) ** (1/2)
25        25        
2626
27class Kid(Person):27class Kid(Person):
n28    n28    MAX_URANIUM = 20
29    def __init__(self, position, initiative):29    def __init__(self, position, initiative):
30        self.list_of_candies = []30        self.list_of_candies = []
31        self.list_of_hosts = []31        self.list_of_hosts = []
32        self.sum_of_candies = 032        self.sum_of_candies = 0
33        super().__init__(position)33        super().__init__(position)
34        self.initiative = initiative34        self.initiative = initiative
35    35    
36    def get_initiative(self):36    def get_initiative(self):
37        return self.initiative37        return self.initiative
38    38    
39    def add_candy(self, candy):39    def add_candy(self, candy):
40        if candy is not None:40        if candy is not None:
41            self.candy = candy41            self.candy = candy
42            self.list_of_candies.append(candy)42            self.list_of_candies.append(candy)
43        43        
44    def go_to_host(self):44    def go_to_host(self):
n45        if len(self.list_of_hosts) == 0:n45        if not self.list_of_hosts:
46            return None46            return None
47        47        
48        min_dist = self.get_distance(self.list_of_hosts[0])48        min_dist = self.get_distance(self.list_of_hosts[0])
49        index_of_host = 049        index_of_host = 0
50        50        
51        for curr_host_ind in range(1, len(self.list_of_hosts)):51        for curr_host_ind in range(1, len(self.list_of_hosts)):
52            next_dist = self.get_distance(self.list_of_hosts[curr_host_ind])52            next_dist = self.get_distance(self.list_of_hosts[curr_host_ind])
53            if min_dist > next_dist:53            if min_dist > next_dist:
54                min_dist = next_dist54                min_dist = next_dist
55                index_of_host = curr_host_ind55                index_of_host = curr_host_ind
56            elif min_dist == next_dist:56            elif min_dist == next_dist:
57                if self.list_of_hosts[curr_host_ind].position[0] < self.list_of_hosts[index_of_host].position[0]:57                if self.list_of_hosts[curr_host_ind].position[0] < self.list_of_hosts[index_of_host].position[0]:
58                    min_dist = next_dist58                    min_dist = next_dist
59                    index_of_host = curr_host_ind59                    index_of_host = curr_host_ind
60                elif self.list_of_hosts[curr_host_ind].position[0] == self.list_of_hosts[index_of_host].position[0]:60                elif self.list_of_hosts[curr_host_ind].position[0] == self.list_of_hosts[index_of_host].position[0]:
61                    if self.list_of_hosts[curr_host_ind].position[1] < self.list_of_hosts[index_of_host].position[1]:61                    if self.list_of_hosts[curr_host_ind].position[1] < self.list_of_hosts[index_of_host].position[1]:
62                        min_dist = next_dist62                        min_dist = next_dist
63                        index_of_host = curr_host_ind63                        index_of_host = curr_host_ind
64        64        
n65        self.add_candy(self.list_of_hosts[index_of_host].remove_candy(function_for_removal))n65        self.add_candy(self.list_of_hosts[index_of_host].remove_candy(Host.function_for_removal))
66        self.position = self.list_of_hosts[index_of_host].position66        self.position = self.list_of_hosts[index_of_host].position
67        self.list_of_hosts.pop(index_of_host)67        self.list_of_hosts.pop(index_of_host)
68        68        
69    def is_critical(self):69    def is_critical(self):
nn70        self.sum_of_candies = 0
70        for candy in self.list_of_candies:71        for candy in self.list_of_candies:
71            self.sum_of_candies += candy.get_uranium_quantity()72            self.sum_of_candies += candy.get_uranium_quantity()
n72        return self.sum_of_candies > 20n73        return self.sum_of_candies > self.MAX_URANIUM
73    74    
7475
75class Host(Person):76class Host(Person):
nn77    
78    @staticmethod
79    def function_for_removal(my_list_of_candies):
80        return max(my_list_of_candies, key=lambda candy: candy.get_mass())
81 
76    def __init__(self, position, candies):82    def __init__(self, position, candies):
77        super().__init__(position)83        super().__init__(position)
78        self.candies = []84        self.candies = []
79        85        
80        for candy in candies:86        for candy in candies:
n81            self.candies.append(Candy(candy[0], candy[1]))n87            self.candies.append(Candy(*candy))
82        88        
83    def remove_candy(self, remove_function):89    def remove_candy(self, remove_function):
84        if len(self.candies) == 0:90        if len(self.candies) == 0:
85            return None91            return None
86        else:92        else:
87            candy_to_remove = remove_function(self.candies)93            candy_to_remove = remove_function(self.candies)
88            self.candies.remove(candy_to_remove)94            self.candies.remove(candy_to_remove)
89            return candy_to_remove95            return candy_to_remove
90      96      
91      97      
92class FluxCapacitor:98class FluxCapacitor:
93    def __init__(self, set_of_people):99    def __init__(self, set_of_people):
94        self.set_of_dead = set()100        self.set_of_dead = set()
95        self.list_of_children = []101        self.list_of_children = []
96        self.list_of_hosts = []102        self.list_of_hosts = []
97        103        
98        for person in set_of_people:104        for person in set_of_people:
99            if isinstance(person, Kid):105            if isinstance(person, Kid):
100                self.list_of_children.append(person)106                self.list_of_children.append(person)
101            else:107            else:
102                self.list_of_hosts.append(person)108                self.list_of_hosts.append(person)
103                109                
104        for kid in self.list_of_children:110        for kid in self.list_of_children:
105            for host in self.list_of_hosts:111            for host in self.list_of_hosts:
106                kid.list_of_hosts.append(host)112                kid.list_of_hosts.append(host)
107        113        
108        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)114        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)
109    115    
110    def get_victim(self):116    def get_victim(self):
111        while True:117        while True:
112            is_there_someone_to_visit = False118            is_there_someone_to_visit = False
113            119            
114            for kid in self.list_of_children:120            for kid in self.list_of_children:
115                if len(kid.list_of_hosts) != 0:121                if len(kid.list_of_hosts) != 0:
116                    is_there_someone_to_visit = True122                    is_there_someone_to_visit = True
117                    break123                    break
118                124                
119            if is_there_someone_to_visit == False:125            if is_there_someone_to_visit == False:
120                return None126                return None
121            127            
122            for kid in self.list_of_children:128            for kid in self.list_of_children:
123                kid.go_to_host()129                kid.go_to_host()
124            130            
125            for kid in self.list_of_children:131            for kid in self.list_of_children:
126                if kid.is_critical():132                if kid.is_critical():
nn133                    print(kid.initiative)
127                    self.set_of_dead.add(kid)134                    self.set_of_dead.add(kid)
128                    135                    
129            if len(self.set_of_dead) != 0:136            if len(self.set_of_dead) != 0:
130                return self.set_of_dead137                return self.set_of_dead
131            138            
132        139        
t133 t
134# nqkva primerna funkciq
135def function_for_removal(my_list_of_candies):
136    max_mass_candy = my_list_of_candies[0]
137    for candy in my_list_of_candies:
138        if candy.get_mass() > max_mass_candy.get_mass():
139            max_mass_candy = candy
140    
141    return max_mass_candy
142 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class Candy:f1class Candy:
2    def __init__(self, mass, uranium):2    def __init__(self, mass, uranium):
3        self.mass = mass3        self.mass = mass
4        self.uranium = uranium4        self.uranium = uranium
5        5        
6    def get_uranium_quantity(self):6    def get_uranium_quantity(self):
7        return self.mass * self.uranium7        return self.mass * self.uranium
8    8    
9    def get_mass(self):9    def get_mass(self):
10        return self.mass10        return self.mass
11    11    
1212
13class Person:13class Person:
14    def __init__(self, position):14    def __init__(self, position):
15        self.position = position15        self.position = position
16        16        
17    def get_position(self):17    def get_position(self):
18        return self.position18        return self.position
19    19    
20    def set_postition(self, new_position):20    def set_postition(self, new_position):
21        self.position = new_position21        self.position = new_position
22        22        
23    def get_distance(self, other_person):23    def get_distance(self, other_person):
24        return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2)24        return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2)
25        25        
2626
27class Kid(Person):27class Kid(Person):
28    28    
29    def __init__(self, position, initiative):29    def __init__(self, position, initiative):
30        self.list_of_candies = []30        self.list_of_candies = []
31        self.list_of_hosts = []31        self.list_of_hosts = []
32        self.sum_of_candies = 032        self.sum_of_candies = 0
33        super().__init__(position)33        super().__init__(position)
34        self.initiative = initiative34        self.initiative = initiative
35    35    
36    def get_initiative(self):36    def get_initiative(self):
37        return self.initiative37        return self.initiative
38    38    
39    def add_candy(self, candy):39    def add_candy(self, candy):
40        if candy is not None:40        if candy is not None:
41            self.candy = candy41            self.candy = candy
42            self.list_of_candies.append(candy)42            self.list_of_candies.append(candy)
43        43        
44    def go_to_host(self):44    def go_to_host(self):
45        if len(self.list_of_hosts) == 0:45        if len(self.list_of_hosts) == 0:
46            return None46            return None
47        47        
48        min_dist = self.get_distance(self.list_of_hosts[0])48        min_dist = self.get_distance(self.list_of_hosts[0])
49        index_of_host = 049        index_of_host = 0
50        50        
51        for curr_host_ind in range(1, len(self.list_of_hosts)):51        for curr_host_ind in range(1, len(self.list_of_hosts)):
52            next_dist = self.get_distance(self.list_of_hosts[curr_host_ind])52            next_dist = self.get_distance(self.list_of_hosts[curr_host_ind])
53            if min_dist > next_dist:53            if min_dist > next_dist:
54                min_dist = next_dist54                min_dist = next_dist
55                index_of_host = curr_host_ind55                index_of_host = curr_host_ind
56            elif min_dist == next_dist:56            elif min_dist == next_dist:
57                if self.list_of_hosts[curr_host_ind].position[0] < self.list_of_hosts[index_of_host].position[0]:57                if self.list_of_hosts[curr_host_ind].position[0] < self.list_of_hosts[index_of_host].position[0]:
58                    min_dist = next_dist58                    min_dist = next_dist
59                    index_of_host = curr_host_ind59                    index_of_host = curr_host_ind
60                elif self.list_of_hosts[curr_host_ind].position[0] == self.list_of_hosts[index_of_host].position[0]:60                elif self.list_of_hosts[curr_host_ind].position[0] == self.list_of_hosts[index_of_host].position[0]:
61                    if self.list_of_hosts[curr_host_ind].position[1] < self.list_of_hosts[index_of_host].position[1]:61                    if self.list_of_hosts[curr_host_ind].position[1] < self.list_of_hosts[index_of_host].position[1]:
62                        min_dist = next_dist62                        min_dist = next_dist
63                        index_of_host = curr_host_ind63                        index_of_host = curr_host_ind
64        64        
65        self.add_candy(self.list_of_hosts[index_of_host].remove_candy(function_for_removal))65        self.add_candy(self.list_of_hosts[index_of_host].remove_candy(function_for_removal))
nn66        self.position = self.list_of_hosts[index_of_host].position
66        self.list_of_hosts.pop(index_of_host)67        self.list_of_hosts.pop(index_of_host)
67        68        
68    def is_critical(self):69    def is_critical(self):
69        for candy in self.list_of_candies:70        for candy in self.list_of_candies:
70            self.sum_of_candies += candy.get_uranium_quantity()71            self.sum_of_candies += candy.get_uranium_quantity()
71        return self.sum_of_candies > 2072        return self.sum_of_candies > 20
72    73    
7374
74class Host(Person):75class Host(Person):
75    def __init__(self, position, candies):76    def __init__(self, position, candies):
76        super().__init__(position)77        super().__init__(position)
77        self.candies = []78        self.candies = []
78        79        
79        for candy in candies:80        for candy in candies:
80            self.candies.append(Candy(candy[0], candy[1]))81            self.candies.append(Candy(candy[0], candy[1]))
81        82        
82    def remove_candy(self, remove_function):83    def remove_candy(self, remove_function):
83        if len(self.candies) == 0:84        if len(self.candies) == 0:
84            return None85            return None
85        else:86        else:
86            candy_to_remove = remove_function(self.candies)87            candy_to_remove = remove_function(self.candies)
87            self.candies.remove(candy_to_remove)88            self.candies.remove(candy_to_remove)
88            return candy_to_remove89            return candy_to_remove
89      90      
90      91      
91class FluxCapacitor:92class FluxCapacitor:
92    def __init__(self, set_of_people):93    def __init__(self, set_of_people):
93        self.set_of_dead = set()94        self.set_of_dead = set()
94        self.list_of_children = []95        self.list_of_children = []
95        self.list_of_hosts = []96        self.list_of_hosts = []
96        97        
97        for person in set_of_people:98        for person in set_of_people:
98            if isinstance(person, Kid):99            if isinstance(person, Kid):
99                self.list_of_children.append(person)100                self.list_of_children.append(person)
100            else:101            else:
101                self.list_of_hosts.append(person)102                self.list_of_hosts.append(person)
102                103                
103        for kid in self.list_of_children:104        for kid in self.list_of_children:
104            for host in self.list_of_hosts:105            for host in self.list_of_hosts:
105                kid.list_of_hosts.append(host)106                kid.list_of_hosts.append(host)
106        107        
107        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)108        self.list_of_children.sort(reverse=True, key=lambda kid: kid.initiative)
108    109    
109    def get_victim(self):110    def get_victim(self):
110        while True:111        while True:
111            is_there_someone_to_visit = False112            is_there_someone_to_visit = False
112            113            
113            for kid in self.list_of_children:114            for kid in self.list_of_children:
114                if len(kid.list_of_hosts) != 0:115                if len(kid.list_of_hosts) != 0:
115                    is_there_someone_to_visit = True116                    is_there_someone_to_visit = True
116                    break117                    break
117                118                
118            if is_there_someone_to_visit == False:119            if is_there_someone_to_visit == False:
119                return None120                return None
120            121            
121            for kid in self.list_of_children:122            for kid in self.list_of_children:
122                kid.go_to_host()123                kid.go_to_host()
123            124            
124            for kid in self.list_of_children:125            for kid in self.list_of_children:
125                if kid.is_critical():126                if kid.is_critical():
126                    self.set_of_dead.add(kid)127                    self.set_of_dead.add(kid)
127                    128                    
128            if len(self.set_of_dead) != 0:129            if len(self.set_of_dead) != 0:
129                return self.set_of_dead130                return self.set_of_dead
130            131            
131        132        
nn133 
132# nqkva primerna funkciq134# nqkva primerna funkciq
133def function_for_removal(my_list_of_candies):135def function_for_removal(my_list_of_candies):
134    max_mass_candy = my_list_of_candies[0]136    max_mass_candy = my_list_of_candies[0]
135    for candy in my_list_of_candies:137    for candy in my_list_of_candies:
136        if candy.get_mass() > max_mass_candy.get_mass():138        if candy.get_mass() > max_mass_candy.get_mass():
137            max_mass_candy = candy139            max_mass_candy = candy
138    140    
139    return max_mass_candy141    return max_mass_candy
140142
t141 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op