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

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

7 точки общо

8 успешни теста
4 неуспешни теста
Код (оправил съм почти всички забележки)

  1import math
  2import sys
  3
  4
  5class Candy:
  6    def __init__(self, mass, uranium):
  7        self.mass = mass
  8        self.uranium = uranium
  9    
 10    def get_uranium_quantity(self):
 11        return self.mass * self.uranium
 12    
 13    def get_mass(self):
 14        return self.mass
 15    
 16
 17class Person:
 18    def __init__(self, position):
 19        self.x, self.y = position
 20    
 21    def get_position(self):
 22        return self.x, self.y
 23    
 24    def set_position(self, new_position):
 25        self.x = new_position[0]
 26        self.y = new_position[1]
 27
 28
 29class Kid(Person):
 30    
 31    MAX_URANIUM = 20
 32    
 33    def __init__(self, position, initiative):
 34        super().__init__(position)
 35        self.initiative = initiative
 36        self.mass_counter = 0
 37        
 38    def get_initiative(self):
 39        return self.initiative
 40    
 41    def add_candy(self, candy):
 42        self.mass_counter += candy.get_uranium_quantity()
 43                
 44    def is_critical(self):
 45        return self.mass_counter > self.MAX_URANIUM
 46    
 47
 48def get_distance(pos1, pos2):
 49    return math.sqrt((pos2[0] - pos1[0])**2 + (pos2[1] - pos1[1])**2)
 50
 51
 52class Host(Person):
 53    def __init__(self, position, candies):
 54        super().__init__(position)
 55        self.basket = []
 56        
 57        for element in candies:
 58            self.basket.append(Candy(*element))
 59
 60    def remove_candy(self, func):
 61        if self.basket.count == 0:
 62            return None
 63        else:
 64            result = func(self.basket)
 65            self.basket.remove(result)
 66            return result
 67        
 68
 69class FluxCapacitor:
 70    def __init__(self, participants):
 71        self.kids_arr = [element for element in participants if isinstance(element, Kid)]
 72        self.kids_arr.sort(key=lambda x: x.get_initiative(), reverse=True)
 73        self.hosts_arr = [element for element in participants if isinstance(element, Host)]
 74        
 75    def get_victim(self):
 76        def decide_which_candy_to_give(container):
 77            result = container[0]
 78            for element in container:
 79                if element.get_mass() > result.get_mass():
 80                    result = element
 81            return result
 82
 83        host_list = [[]for _ in range(len(self.hosts_arr))]
 84        passed_hosts = [[]for _ in range(len(self.kids_arr))]
 85        flag = True
 86        while flag: 
 87            for kid in self.kids_arr:
 88                cur_distance = sys.maxsize
 89                best_host_idx = 0
 90                
 91                for i in range(len(self.hosts_arr)):
 92                    if i in passed_hosts[self.kids_arr.index(kid)]:
 93                        continue
 94                    if get_distance(kid.get_position(), self.hosts_arr[i].get_position()) < cur_distance:
 95                        cur_distance = get_distance(kid.get_position(), self.hosts_arr[i].get_position())
 96                        best_host_idx = i
 97                    elif get_distance(kid.get_position(), self.hosts_arr[i].get_position()) == cur_distance:
 98                        if self.hosts_arr[i].get_position()[0] < self.hosts_arr[best_host_idx].get_position()[0]:
 99                            best_host_idx = i
100                            cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())
101                        elif self.hosts_arr[i].get_position()[0] == self.hosts_arr[best_host_idx].get_position()[0]:
102                            if self.hosts_arr[i].get_position()[1] < self.hosts_arr[best_host_idx].get_position()[1]:
103                                best_host_idx = i
104                                cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())
105                host_list[best_host_idx].append(kid)
106                passed_hosts[self.kids_arr.index(kid)].append(best_host_idx)
107                
108            for i, host in enumerate(host_list):
109                for kid in host:
110                    kid.add_candy(self.hosts_arr[i].remove_candy(decide_which_candy_to_give))
111                    kid.set_position(self.hosts_arr[i].get_position())
112                    
113            dead_kids = set(kid for kid in self.kids_arr if kid.is_critical())
114            if dead_kids:
115                return dead_kids
116            
117            counter = 0
118            for element in passed_hosts:
119                counter += len(element)
120            
121            if counter // len(self.hosts_arr) == len(self.kids_arr):
122                return None                    

.EE.E.E.....
======================================================================
ERROR: test_empty (test.FluxCapacitorTest)
Test with empty collection.
----------------------------------------------------------------------
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 80, in test_empty
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 121, in get_victim
if counter // len(self.hosts_arr) == len(self.kids_arr):
ZeroDivisionError: integer division or modulo by zero

======================================================================
ERROR: test_empty_hosts (test.FluxCapacitorTest)
Test with empty hosts.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
File "/tmp/test.py", line 88, in test_empty_hosts
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 105, in get_victim
host_list[best_host_idx].append(kid)
IndexError: list index out of range

======================================================================
ERROR: test_no_candies (test.FluxCapacitorTest)
Test with no candies.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
File "/tmp/test.py", line 106, in test_no_candies
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 110, in get_victim
kid.add_candy(self.hosts_arr[i].remove_candy(decide_which_candy_to_give))
File "/tmp/solution.py", line 64, in remove_candy
result = func(self.basket)
File "/tmp/solution.py", line 77, in decide_which_candy_to_give
result = container[0]
IndexError: list index out of range

======================================================================
ERROR: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 69, in test_basic_usage
candy = host.remove_candy(compare_fun)
File "/tmp/solution.py", line 64, in remove_candy
result = func(self.basket)
File "/tmp/test.py", line 63, in <lambda>
compare_fun = lambda candies: min(candies, key=lambda candy: candy.get_mass())
ValueError: min() arg is an empty sequence

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

FAILED (errors=4)

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

f1import mathf1import math
nn2import sys
3 
24
3class Candy:5class Candy:
4    def __init__(self, mass, uranium):6    def __init__(self, mass, uranium):
5        self.mass = mass7        self.mass = mass
6        self.uranium = uranium8        self.uranium = uranium
7    9    
8    def get_uranium_quantity(self):10    def get_uranium_quantity(self):
9        return self.mass * self.uranium11        return self.mass * self.uranium
10    12    
11    def get_mass(self):13    def get_mass(self):
12        return self.mass14        return self.mass
13    15    
1416
15class Person:17class Person:
16    def __init__(self, position):18    def __init__(self, position):
n17        self.x = position[0]n19        self.x, self.y = position
18        self.y = position[1]
19    20    
20    def get_position(self):21    def get_position(self):
n21        return (self.x, self.y)n22        return self.x, self.y
22    23    
23    def set_position(self, new_position):24    def set_position(self, new_position):
24        self.x = new_position[0]25        self.x = new_position[0]
25        self.y = new_position[1]26        self.y = new_position[1]
2627
2728
28class Kid(Person):29class Kid(Person):
29    30    
30    MAX_URANIUM = 2031    MAX_URANIUM = 20
31    32    
32    def __init__(self, position, initiative):33    def __init__(self, position, initiative):
33        super().__init__(position)34        super().__init__(position)
34        self.initiative = initiative35        self.initiative = initiative
35        self.mass_counter = 036        self.mass_counter = 0
n36        self.candy_counter = 0n
37        37        
38    def get_initiative(self):38    def get_initiative(self):
39        return self.initiative39        return self.initiative
40    40    
41    def add_candy(self, candy):41    def add_candy(self, candy):
42        self.mass_counter += candy.get_uranium_quantity()42        self.mass_counter += candy.get_uranium_quantity()
n43        self.candy_counter += 1n
44                43                
45    def is_critical(self):44    def is_critical(self):
46        return self.mass_counter > self.MAX_URANIUM45        return self.mass_counter > self.MAX_URANIUM
47    46    
4847
n49def func(container):n
50    result = container[0]
51    for element in container:
52        if element.get_mass() > result.get_mass():
53            result = element
54    return result
55 
56def get_distance(pos1, pos2):48def get_distance(pos1, pos2):
57    return math.sqrt((pos2[0] - pos1[0])**2 + (pos2[1] - pos1[1])**2)49    return math.sqrt((pos2[0] - pos1[0])**2 + (pos2[1] - pos1[1])**2)
5850
nn51 
59class Host(Person):52class Host(Person):
n60    n
61    basket = []
62    
63    def __init__(self, position, candies):53    def __init__(self, position, candies):
64        super().__init__(position)54        super().__init__(position)
nn55        self.basket = []
65        56        
66        for element in candies:57        for element in candies:
n67            temp = Candy(element[0], element[1])n
68            self.basket.append(temp)58            self.basket.append(Candy(*element))
6959
70    def remove_candy(self, func):60    def remove_candy(self, func):
71        if self.basket.count == 0:61        if self.basket.count == 0:
72            return None62            return None
73        else:63        else:
74            result = func(self.basket)64            result = func(self.basket)
75            self.basket.remove(result)65            self.basket.remove(result)
76            return result66            return result
77        67        
7868
79class FluxCapacitor:69class FluxCapacitor:
80    def __init__(self, participants):70    def __init__(self, participants):
81        self.kids_arr = [element for element in participants if isinstance(element, Kid)]71        self.kids_arr = [element for element in participants if isinstance(element, Kid)]
82        self.kids_arr.sort(key=lambda x: x.get_initiative(), reverse=True)72        self.kids_arr.sort(key=lambda x: x.get_initiative(), reverse=True)
83        self.hosts_arr = [element for element in participants if isinstance(element, Host)]73        self.hosts_arr = [element for element in participants if isinstance(element, Host)]
84        74        
85    def get_victim(self):75    def get_victim(self):
nn76        def decide_which_candy_to_give(container):
77            result = container[0]
78            for element in container:
79                if element.get_mass() > result.get_mass():
80                    result = element
81            return result
82 
86        host_list = [[]for _ in range(len(self.hosts_arr))]83        host_list = [[]for _ in range(len(self.hosts_arr))]
87        passed_hosts = [[]for _ in range(len(self.kids_arr))]84        passed_hosts = [[]for _ in range(len(self.kids_arr))]
88        flag = True85        flag = True
n89        while flag n86        while flag: 
90            for kid in self.kids_arr:87            for kid in self.kids_arr:
n91                cur_distance = 6666669n88                cur_distance = sys.maxsize
92                best_host_idx = 089                best_host_idx = 0
93                90                
94                for i in range(len(self.hosts_arr)):91                for i in range(len(self.hosts_arr)):
95                    if i in passed_hosts[self.kids_arr.index(kid)]:92                    if i in passed_hosts[self.kids_arr.index(kid)]:
96                        continue93                        continue
97                    if get_distance(kid.get_position(), self.hosts_arr[i].get_position()) < cur_distance:94                    if get_distance(kid.get_position(), self.hosts_arr[i].get_position()) < cur_distance:
98                        cur_distance = get_distance(kid.get_position(), self.hosts_arr[i].get_position())95                        cur_distance = get_distance(kid.get_position(), self.hosts_arr[i].get_position())
99                        best_host_idx = i96                        best_host_idx = i
100                    elif get_distance(kid.get_position(), self.hosts_arr[i].get_position()) == cur_distance:97                    elif get_distance(kid.get_position(), self.hosts_arr[i].get_position()) == cur_distance:
101                        if self.hosts_arr[i].get_position()[0] < self.hosts_arr[best_host_idx].get_position()[0]:98                        if self.hosts_arr[i].get_position()[0] < self.hosts_arr[best_host_idx].get_position()[0]:
102                            best_host_idx = i99                            best_host_idx = i
103                            cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())100                            cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())
104                        elif self.hosts_arr[i].get_position()[0] == self.hosts_arr[best_host_idx].get_position()[0]:101                        elif self.hosts_arr[i].get_position()[0] == self.hosts_arr[best_host_idx].get_position()[0]:
105                            if self.hosts_arr[i].get_position()[1] < self.hosts_arr[best_host_idx].get_position()[1]:102                            if self.hosts_arr[i].get_position()[1] < self.hosts_arr[best_host_idx].get_position()[1]:
106                                best_host_idx = i103                                best_host_idx = i
107                                cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())104                                cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())
108                host_list[best_host_idx].append(kid)105                host_list[best_host_idx].append(kid)
109                passed_hosts[self.kids_arr.index(kid)].append(best_host_idx)106                passed_hosts[self.kids_arr.index(kid)].append(best_host_idx)
110                107                
n111            for i,host in enumerate(host_list):n108            for i, host in enumerate(host_list):
112                for kid in host:109                for kid in host:
t113                    kid.add_candy(self.hosts_arr[i].remove_candy(func))t110                    kid.add_candy(self.hosts_arr[i].remove_candy(decide_which_candy_to_give))
114                    kid.set_position(self.hosts_arr[i].get_position())111                    kid.set_position(self.hosts_arr[i].get_position())
115                    112                    
116            dead_kids = set(kid for kid in self.kids_arr if kid.is_critical())113            dead_kids = set(kid for kid in self.kids_arr if kid.is_critical())
117            if dead_kids:114            if dead_kids:
118                return dead_kids115                return dead_kids
119            116            
120            counter = 0117            counter = 0
121            for element in passed_hosts:118            for element in passed_hosts:
122                counter += len(element)119                counter += len(element)
123            120            
124            if counter // len(self.hosts_arr) == len(self.kids_arr):121            if counter // len(self.hosts_arr) == len(self.kids_arr):
125                return None                    122                return None                    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
22
3class Candy:3class Candy:
4    def __init__(self, mass, uranium):4    def __init__(self, mass, uranium):
5        self.mass = mass5        self.mass = mass
6        self.uranium = uranium6        self.uranium = uranium
7    7    
8    def get_uranium_quantity(self):8    def get_uranium_quantity(self):
9        return self.mass * self.uranium9        return self.mass * self.uranium
10    10    
11    def get_mass(self):11    def get_mass(self):
12        return self.mass12        return self.mass
13    13    
1414
15class Person:15class Person:
16    def __init__(self, position):16    def __init__(self, position):
17        self.x = position[0]17        self.x = position[0]
18        self.y = position[1]18        self.y = position[1]
19    19    
20    def get_position(self):20    def get_position(self):
21        return (self.x, self.y)21        return (self.x, self.y)
22    22    
23    def set_position(self, new_position):23    def set_position(self, new_position):
24        self.x = new_position[0]24        self.x = new_position[0]
25        self.y = new_position[1]25        self.y = new_position[1]
2626
2727
28class Kid(Person):28class Kid(Person):
29    29    
t30    MAX_URANIUM=20t30    MAX_URANIUM = 20
31    31    
32    def __init__(self, position, initiative):32    def __init__(self, position, initiative):
33        super().__init__(position)33        super().__init__(position)
34        self.initiative = initiative34        self.initiative = initiative
35        self.mass_counter = 035        self.mass_counter = 0
36        self.candy_counter = 036        self.candy_counter = 0
37        37        
38    def get_initiative(self):38    def get_initiative(self):
39        return self.initiative39        return self.initiative
40    40    
41    def add_candy(self, candy):41    def add_candy(self, candy):
42        self.mass_counter += candy.get_uranium_quantity()42        self.mass_counter += candy.get_uranium_quantity()
43        self.candy_counter += 143        self.candy_counter += 1
44                44                
45    def is_critical(self):45    def is_critical(self):
46        return self.mass_counter > self.MAX_URANIUM46        return self.mass_counter > self.MAX_URANIUM
47    47    
4848
49def func(container):49def func(container):
50    result = container[0]50    result = container[0]
51    for element in container:51    for element in container:
52        if element.get_mass() > result.get_mass():52        if element.get_mass() > result.get_mass():
53            result = element53            result = element
54    return result54    return result
5555
56def get_distance(pos1, pos2):56def get_distance(pos1, pos2):
57    return math.sqrt((pos2[0] - pos1[0])**2 + (pos2[1] - pos1[1])**2)57    return math.sqrt((pos2[0] - pos1[0])**2 + (pos2[1] - pos1[1])**2)
5858
59class Host(Person):59class Host(Person):
60    60    
61    basket = []61    basket = []
62    62    
63    def __init__(self, position, candies):63    def __init__(self, position, candies):
64        super().__init__(position)64        super().__init__(position)
65        65        
66        for element in candies:66        for element in candies:
67            temp = Candy(element[0], element[1])67            temp = Candy(element[0], element[1])
68            self.basket.append(temp)68            self.basket.append(temp)
6969
70    def remove_candy(self, func):70    def remove_candy(self, func):
71        if self.basket.count == 0:71        if self.basket.count == 0:
72            return None72            return None
73        else:73        else:
74            result = func(self.basket)74            result = func(self.basket)
75            self.basket.remove(result)75            self.basket.remove(result)
76            return result76            return result
77        77        
7878
79class FluxCapacitor:79class FluxCapacitor:
80    def __init__(self, participants):80    def __init__(self, participants):
81        self.kids_arr = [element for element in participants if isinstance(element, Kid)]81        self.kids_arr = [element for element in participants if isinstance(element, Kid)]
82        self.kids_arr.sort(key=lambda x: x.get_initiative(), reverse=True)82        self.kids_arr.sort(key=lambda x: x.get_initiative(), reverse=True)
83        self.hosts_arr = [element for element in participants if isinstance(element, Host)]83        self.hosts_arr = [element for element in participants if isinstance(element, Host)]
84        84        
85    def get_victim(self):85    def get_victim(self):
86        host_list = [[]for _ in range(len(self.hosts_arr))]86        host_list = [[]for _ in range(len(self.hosts_arr))]
87        passed_hosts = [[]for _ in range(len(self.kids_arr))]87        passed_hosts = [[]for _ in range(len(self.kids_arr))]
88        flag = True88        flag = True
89        while flag : 89        while flag : 
90            for kid in self.kids_arr:90            for kid in self.kids_arr:
91                cur_distance = 666666991                cur_distance = 6666669
92                best_host_idx = 092                best_host_idx = 0
93                93                
94                for i in range(len(self.hosts_arr)):94                for i in range(len(self.hosts_arr)):
95                    if i in passed_hosts[self.kids_arr.index(kid)]:95                    if i in passed_hosts[self.kids_arr.index(kid)]:
96                        continue96                        continue
97                    if get_distance(kid.get_position(), self.hosts_arr[i].get_position()) < cur_distance:97                    if get_distance(kid.get_position(), self.hosts_arr[i].get_position()) < cur_distance:
98                        cur_distance = get_distance(kid.get_position(), self.hosts_arr[i].get_position())98                        cur_distance = get_distance(kid.get_position(), self.hosts_arr[i].get_position())
99                        best_host_idx = i99                        best_host_idx = i
100                    elif get_distance(kid.get_position(), self.hosts_arr[i].get_position()) == cur_distance:100                    elif get_distance(kid.get_position(), self.hosts_arr[i].get_position()) == cur_distance:
101                        if self.hosts_arr[i].get_position()[0] < self.hosts_arr[best_host_idx].get_position()[0]:101                        if self.hosts_arr[i].get_position()[0] < self.hosts_arr[best_host_idx].get_position()[0]:
102                            best_host_idx = i102                            best_host_idx = i
103                            cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())103                            cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())
104                        elif self.hosts_arr[i].get_position()[0] == self.hosts_arr[best_host_idx].get_position()[0]:104                        elif self.hosts_arr[i].get_position()[0] == self.hosts_arr[best_host_idx].get_position()[0]:
105                            if self.hosts_arr[i].get_position()[1] < self.hosts_arr[best_host_idx].get_position()[1]:105                            if self.hosts_arr[i].get_position()[1] < self.hosts_arr[best_host_idx].get_position()[1]:
106                                best_host_idx = i106                                best_host_idx = i
107                                cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())107                                cur_distance = get_distance(kid.get_position(), self.hosts_arr[best_host_idx].get_position())
108                host_list[best_host_idx].append(kid)108                host_list[best_host_idx].append(kid)
109                passed_hosts[self.kids_arr.index(kid)].append(best_host_idx)109                passed_hosts[self.kids_arr.index(kid)].append(best_host_idx)
110                110                
111            for i,host in enumerate(host_list):111            for i,host in enumerate(host_list):
112                for kid in host:112                for kid in host:
113                    kid.add_candy(self.hosts_arr[i].remove_candy(func))113                    kid.add_candy(self.hosts_arr[i].remove_candy(func))
114                    kid.set_position(self.hosts_arr[i].get_position())114                    kid.set_position(self.hosts_arr[i].get_position())
115                    115                    
116            dead_kids = set(kid for kid in self.kids_arr if kid.is_critical())116            dead_kids = set(kid for kid in self.kids_arr if kid.is_critical())
117            if dead_kids:117            if dead_kids:
118                return dead_kids118                return dead_kids
119            119            
120            counter = 0120            counter = 0
121            for element in passed_hosts:121            for element in passed_hosts:
122                counter += len(element)122                counter += len(element)
123            123            
124            if counter // len(self.hosts_arr) == len(self.kids_arr):124            if counter // len(self.hosts_arr) == len(self.kids_arr):
125                return None                    125                return None                    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op