1import math
2
3class Candy:
4
5 def __init__(self, mass, uranium):
6 self.mass = mass
7 self.uranium = uranium
8
9 def get_uranium_quantity(self):
10 return self.mass * self.uranium
11
12 def get_mass(self):
13 return self.mass
14
15
16class Person:
17
18 def __init__(self, position):
19 self.position = position
20
21 def get_position(self):
22 return self.position
23
24 def set_position(self, new_position):
25 self.position = new_position
26
27
28class Kid(Person):
29
30 CRITICAL_URANIUM_QUANTITY_PER_KID = 20
31
32 def __init__(self, position, initiative):
33 super().__init__(position)
34 self.initiative = initiative
35 self.uranium_quantity_in_basket = 0
36
37 def get_initiative(self):
38 return self.initiative
39
40 def add_candy(self, candy):
41 self.uranium_quantity_in_basket += candy.get_uranium_quantity()
42
43 def is_critical(self):
44 return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID
45
46
47class Host(Person):
48
49 def __init__(self, position, candies):
50 super().__init__(position)
51 real_candies = []
52 for candy in candies:
53 real_candies.append(Candy(candy[0], candy[1]))
54 self.candies = sorted(real_candies, key=lambda candy: candy.mass, reverse=True)
55 self.guests = []
56
57 def remove_candy(self, candy_to_be_removed):
58 if self.candies:
59 exact_candy_to_be_removed = candy_to_be_removed(self.candies)
60 return exact_candy_to_be_removed
61
62 return None
63
64 def add_guest(self, guest):
65 self.guests.append(guest)
66
67 def treat(self):
68 self.guests.sort(key=lambda kid: kid.initiative)
69 kids_with_critical_condition = set()
70
71 for (kid, candy) in zip(self.guests, self.candies):
72 kid.add_candy(candy)
73 self.candies.remove(candy)
74 kid.set_position(self.get_position())
75
76 if kid.is_critical():
77 kids_with_critical_condition.add(kid)
78
79 self.guests.clear()
80 return kids_with_critical_condition
81
82class FluxCapacitor:
83
84 def __init__(self, participants):
85 self.hosts = []
86 self.kids = []
87
88 for participant in participants:
89 if type(participant) == Host:
90 self.hosts.append(participant)
91 else:
92 self.kids.append(participant)
93
94 def arrange_guests(self):
95 for kid in self.kids:
96 lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position())
97 closest_host = self.hosts[0]
98
99 for host in self.hosts:
100 current_distance = math.dist(kid.get_position(), host.get_position())
101
102 if current_distance < lowest_distance:
103 lowest_distance = current_distance
104 closest_host = host
105 elif current_distance == lowest_distance:
106 if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]:
107 lowest_distance = current_distance
108 closest_host = host
109
110 closest_host.add_guest(kid)
111
112 def get_victim(self):
113 self.arrange_guests()
114 final_set_critical_condition = set()
115
116 for host in self.hosts:
117 final_set_critical_condition.update(host.treat())
118
119 return final_set_critical_condition
.FEFF.F.....
======================================================================
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 113, in get_victim
self.arrange_guests()
File "/tmp/solution.py", line 96, in arrange_guests
lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position())
IndexError: list index out of range
======================================================================
FAIL: 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)
AssertionError: set() != None
======================================================================
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)
AssertionError: set() != None
======================================================================
FAIL: 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)
AssertionError: set() != None
======================================================================
FAIL: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 68, in test_basic_usage
self.assertEqual(candy.get_mass(), 785)
AssertionError: 456 != 785
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (failures=4, errors=1)
f | 1 | import math | f | 1 | import math |
2 | 2 | ||||
3 | class Candy: | 3 | class Candy: | ||
4 | 4 | ||||
5 | def __init__(self, mass, uranium): | 5 | def __init__(self, mass, uranium): | ||
6 | self.mass = mass | 6 | self.mass = mass | ||
7 | self.uranium = uranium | 7 | self.uranium = uranium | ||
8 | 8 | ||||
9 | def get_uranium_quantity(self): | 9 | def get_uranium_quantity(self): | ||
10 | return self.mass * self.uranium | 10 | return self.mass * self.uranium | ||
11 | 11 | ||||
12 | def get_mass(self): | 12 | def get_mass(self): | ||
13 | return self.mass | 13 | return self.mass | ||
14 | 14 | ||||
15 | 15 | ||||
16 | class Person: | 16 | class Person: | ||
17 | 17 | ||||
18 | def __init__(self, position): | 18 | def __init__(self, position): | ||
19 | self.position = position | 19 | self.position = position | ||
20 | 20 | ||||
21 | def get_position(self): | 21 | def get_position(self): | ||
22 | return self.position | 22 | return self.position | ||
23 | 23 | ||||
24 | def set_position(self, new_position): | 24 | def set_position(self, new_position): | ||
25 | self.position = new_position | 25 | self.position = new_position | ||
26 | 26 | ||||
27 | 27 | ||||
28 | class Kid(Person): | 28 | class Kid(Person): | ||
29 | 29 | ||||
30 | CRITICAL_URANIUM_QUANTITY_PER_KID = 20 | 30 | CRITICAL_URANIUM_QUANTITY_PER_KID = 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 = initiative | 34 | self.initiative = initiative | ||
35 | self.uranium_quantity_in_basket = 0 | 35 | self.uranium_quantity_in_basket = 0 | ||
36 | 36 | ||||
37 | def get_initiative(self): | 37 | def get_initiative(self): | ||
38 | return self.initiative | 38 | return self.initiative | ||
39 | 39 | ||||
40 | def add_candy(self, candy): | 40 | def add_candy(self, candy): | ||
41 | self.uranium_quantity_in_basket += candy.get_uranium_quantity() | 41 | self.uranium_quantity_in_basket += candy.get_uranium_quantity() | ||
42 | 42 | ||||
43 | def is_critical(self): | 43 | def is_critical(self): | ||
44 | return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID | 44 | return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID | ||
45 | 45 | ||||
46 | 46 | ||||
47 | class Host(Person): | 47 | class Host(Person): | ||
48 | 48 | ||||
49 | def __init__(self, position, candies): | 49 | def __init__(self, position, candies): | ||
50 | super().__init__(position) | 50 | super().__init__(position) | ||
51 | real_candies = [] | 51 | real_candies = [] | ||
52 | for candy in candies: | 52 | for candy in candies: | ||
53 | real_candies.append(Candy(candy[0], candy[1])) | 53 | real_candies.append(Candy(candy[0], candy[1])) | ||
n | 54 | self.candies = sorted(real_candies, key=lambda candy: candy.mass) | n | 54 | self.candies = sorted(real_candies, key=lambda candy: candy.mass, reverse=True) |
55 | self.guests = [] | 55 | self.guests = [] | ||
56 | 56 | ||||
57 | def remove_candy(self, candy_to_be_removed): | 57 | def remove_candy(self, candy_to_be_removed): | ||
58 | if self.candies: | 58 | if self.candies: | ||
59 | exact_candy_to_be_removed = candy_to_be_removed(self.candies) | 59 | exact_candy_to_be_removed = candy_to_be_removed(self.candies) | ||
60 | return exact_candy_to_be_removed | 60 | return exact_candy_to_be_removed | ||
61 | 61 | ||||
62 | return None | 62 | return None | ||
63 | 63 | ||||
64 | def add_guest(self, guest): | 64 | def add_guest(self, guest): | ||
65 | self.guests.append(guest) | 65 | self.guests.append(guest) | ||
66 | 66 | ||||
67 | def treat(self): | 67 | def treat(self): | ||
68 | self.guests.sort(key=lambda kid: kid.initiative) | 68 | self.guests.sort(key=lambda kid: kid.initiative) | ||
n | 69 | kids_with_critical_condition = {} | n | 69 | kids_with_critical_condition = set() |
70 | 70 | ||||
71 | for (kid, candy) in zip(self.guests, self.candies): | 71 | for (kid, candy) in zip(self.guests, self.candies): | ||
72 | kid.add_candy(candy) | 72 | kid.add_candy(candy) | ||
73 | self.candies.remove(candy) | 73 | self.candies.remove(candy) | ||
74 | kid.set_position(self.get_position()) | 74 | kid.set_position(self.get_position()) | ||
75 | 75 | ||||
76 | if kid.is_critical(): | 76 | if kid.is_critical(): | ||
n | 77 | kids_with_critical_condition.append(kid) | n | 77 | kids_with_critical_condition.add(kid) |
78 | 78 | ||||
79 | self.guests.clear() | 79 | self.guests.clear() | ||
80 | return kids_with_critical_condition | 80 | return kids_with_critical_condition | ||
81 | 81 | ||||
82 | class FluxCapacitor: | 82 | class FluxCapacitor: | ||
83 | 83 | ||||
84 | def __init__(self, participants): | 84 | def __init__(self, participants): | ||
85 | self.hosts = [] | 85 | self.hosts = [] | ||
86 | self.kids = [] | 86 | self.kids = [] | ||
87 | 87 | ||||
88 | for participant in participants: | 88 | for participant in participants: | ||
89 | if type(participant) == Host: | 89 | if type(participant) == Host: | ||
90 | self.hosts.append(participant) | 90 | self.hosts.append(participant) | ||
91 | else: | 91 | else: | ||
92 | self.kids.append(participant) | 92 | self.kids.append(participant) | ||
93 | 93 | ||||
94 | def arrange_guests(self): | 94 | def arrange_guests(self): | ||
95 | for kid in self.kids: | 95 | for kid in self.kids: | ||
96 | lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position()) | 96 | lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position()) | ||
97 | closest_host = self.hosts[0] | 97 | closest_host = self.hosts[0] | ||
98 | 98 | ||||
99 | for host in self.hosts: | 99 | for host in self.hosts: | ||
100 | current_distance = math.dist(kid.get_position(), host.get_position()) | 100 | current_distance = math.dist(kid.get_position(), host.get_position()) | ||
101 | 101 | ||||
102 | if current_distance < lowest_distance: | 102 | if current_distance < lowest_distance: | ||
103 | lowest_distance = current_distance | 103 | lowest_distance = current_distance | ||
104 | closest_host = host | 104 | closest_host = host | ||
105 | elif current_distance == lowest_distance: | 105 | elif current_distance == lowest_distance: | ||
106 | if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]: | 106 | if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]: | ||
107 | lowest_distance = current_distance | 107 | lowest_distance = current_distance | ||
108 | closest_host = host | 108 | closest_host = host | ||
109 | 109 | ||||
110 | closest_host.add_guest(kid) | 110 | closest_host.add_guest(kid) | ||
111 | 111 | ||||
112 | def get_victim(self): | 112 | def get_victim(self): | ||
113 | self.arrange_guests() | 113 | self.arrange_guests() | ||
t | 114 | final_set_critical_condition = {} | t | 114 | final_set_critical_condition = set() |
115 | 115 | ||||
116 | for host in self.hosts: | 116 | for host in self.hosts: | ||
117 | final_set_critical_condition.update(host.treat()) | 117 | final_set_critical_condition.update(host.treat()) | ||
118 | 118 | ||||
119 | return final_set_critical_condition | 119 | return final_set_critical_condition | ||
120 | 120 | ||||
121 | 121 | ||||
122 | 122 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
2 | 2 | ||||
3 | class Candy: | 3 | class Candy: | ||
4 | 4 | ||||
5 | def __init__(self, mass, uranium): | 5 | def __init__(self, mass, uranium): | ||
6 | self.mass = mass | 6 | self.mass = mass | ||
7 | self.uranium = uranium | 7 | self.uranium = uranium | ||
8 | 8 | ||||
9 | def get_uranium_quantity(self): | 9 | def get_uranium_quantity(self): | ||
10 | return self.mass * self.uranium | 10 | return self.mass * self.uranium | ||
11 | 11 | ||||
12 | def get_mass(self): | 12 | def get_mass(self): | ||
13 | return self.mass | 13 | return self.mass | ||
14 | 14 | ||||
15 | 15 | ||||
16 | class Person: | 16 | class Person: | ||
17 | 17 | ||||
18 | def __init__(self, position): | 18 | def __init__(self, position): | ||
19 | self.position = position | 19 | self.position = position | ||
20 | 20 | ||||
21 | def get_position(self): | 21 | def get_position(self): | ||
22 | return self.position | 22 | return self.position | ||
23 | 23 | ||||
24 | def set_position(self, new_position): | 24 | def set_position(self, new_position): | ||
25 | self.position = new_position | 25 | self.position = new_position | ||
26 | 26 | ||||
27 | 27 | ||||
28 | class Kid(Person): | 28 | class Kid(Person): | ||
29 | 29 | ||||
30 | CRITICAL_URANIUM_QUANTITY_PER_KID = 20 | 30 | CRITICAL_URANIUM_QUANTITY_PER_KID = 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 = initiative | 34 | self.initiative = initiative | ||
35 | self.uranium_quantity_in_basket = 0 | 35 | self.uranium_quantity_in_basket = 0 | ||
36 | 36 | ||||
37 | def get_initiative(self): | 37 | def get_initiative(self): | ||
38 | return self.initiative | 38 | return self.initiative | ||
39 | 39 | ||||
40 | def add_candy(self, candy): | 40 | def add_candy(self, candy): | ||
41 | self.uranium_quantity_in_basket += candy.get_uranium_quantity() | 41 | self.uranium_quantity_in_basket += candy.get_uranium_quantity() | ||
42 | 42 | ||||
43 | def is_critical(self): | 43 | def is_critical(self): | ||
44 | return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID | 44 | return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID | ||
45 | 45 | ||||
46 | 46 | ||||
47 | class Host(Person): | 47 | class Host(Person): | ||
48 | 48 | ||||
49 | def __init__(self, position, candies): | 49 | def __init__(self, position, candies): | ||
50 | super().__init__(position) | 50 | super().__init__(position) | ||
51 | real_candies = [] | 51 | real_candies = [] | ||
52 | for candy in candies: | 52 | for candy in candies: | ||
53 | real_candies.append(Candy(candy[0], candy[1])) | 53 | real_candies.append(Candy(candy[0], candy[1])) | ||
54 | self.candies = sorted(real_candies, key=lambda candy: candy.mass) | 54 | self.candies = sorted(real_candies, key=lambda candy: candy.mass) | ||
55 | self.guests = [] | 55 | self.guests = [] | ||
56 | 56 | ||||
57 | def remove_candy(self, candy_to_be_removed): | 57 | def remove_candy(self, candy_to_be_removed): | ||
58 | if self.candies: | 58 | if self.candies: | ||
59 | exact_candy_to_be_removed = candy_to_be_removed(self.candies) | 59 | exact_candy_to_be_removed = candy_to_be_removed(self.candies) | ||
60 | return exact_candy_to_be_removed | 60 | return exact_candy_to_be_removed | ||
61 | 61 | ||||
62 | return None | 62 | return None | ||
63 | 63 | ||||
64 | def add_guest(self, guest): | 64 | def add_guest(self, guest): | ||
65 | self.guests.append(guest) | 65 | self.guests.append(guest) | ||
66 | 66 | ||||
67 | def treat(self): | 67 | def treat(self): | ||
68 | self.guests.sort(key=lambda kid: kid.initiative) | 68 | self.guests.sort(key=lambda kid: kid.initiative) | ||
69 | kids_with_critical_condition = {} | 69 | kids_with_critical_condition = {} | ||
70 | 70 | ||||
n | 71 | for kid, candy in self.guests, self.candies: | n | 71 | for (kid, candy) in zip(self.guests, self.candies): |
72 | kid.add_candy(candy) | 72 | kid.add_candy(candy) | ||
t | t | 73 | self.candies.remove(candy) | ||
73 | kid.set_position(self.get_position()) | 74 | kid.set_position(self.get_position()) | ||
74 | 75 | ||||
75 | if kid.is_critical(): | 76 | if kid.is_critical(): | ||
76 | kids_with_critical_condition.append(kid) | 77 | kids_with_critical_condition.append(kid) | ||
77 | 78 | ||||
78 | self.guests.clear() | 79 | self.guests.clear() | ||
79 | return kids_with_critical_condition | 80 | return kids_with_critical_condition | ||
80 | 81 | ||||
81 | class FluxCapacitor: | 82 | class FluxCapacitor: | ||
82 | 83 | ||||
83 | def __init__(self, participants): | 84 | def __init__(self, participants): | ||
84 | self.hosts = [] | 85 | self.hosts = [] | ||
85 | self.kids = [] | 86 | self.kids = [] | ||
86 | 87 | ||||
87 | for participant in participants: | 88 | for participant in participants: | ||
88 | if type(participant) == Host: | 89 | if type(participant) == Host: | ||
89 | self.hosts.append(participant) | 90 | self.hosts.append(participant) | ||
90 | else: | 91 | else: | ||
91 | self.kids.append(participant) | 92 | self.kids.append(participant) | ||
92 | 93 | ||||
93 | def arrange_guests(self): | 94 | def arrange_guests(self): | ||
94 | for kid in self.kids: | 95 | for kid in self.kids: | ||
95 | lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position()) | 96 | lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position()) | ||
96 | closest_host = self.hosts[0] | 97 | closest_host = self.hosts[0] | ||
97 | 98 | ||||
98 | for host in self.hosts: | 99 | for host in self.hosts: | ||
99 | current_distance = math.dist(kid.get_position(), host.get_position()) | 100 | current_distance = math.dist(kid.get_position(), host.get_position()) | ||
100 | 101 | ||||
101 | if current_distance < lowest_distance: | 102 | if current_distance < lowest_distance: | ||
102 | lowest_distance = current_distance | 103 | lowest_distance = current_distance | ||
103 | closest_host = host | 104 | closest_host = host | ||
104 | elif current_distance == lowest_distance: | 105 | elif current_distance == lowest_distance: | ||
105 | if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]: | 106 | if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]: | ||
106 | lowest_distance = current_distance | 107 | lowest_distance = current_distance | ||
107 | closest_host = host | 108 | closest_host = host | ||
108 | 109 | ||||
109 | closest_host.add_guest(kid) | 110 | closest_host.add_guest(kid) | ||
110 | 111 | ||||
111 | def get_victim(self): | 112 | def get_victim(self): | ||
112 | self.arrange_guests() | 113 | self.arrange_guests() | ||
113 | final_set_critical_condition = {} | 114 | final_set_critical_condition = {} | ||
114 | 115 | ||||
115 | for host in self.hosts: | 116 | for host in self.hosts: | ||
116 | final_set_critical_condition.update(host.treat()) | 117 | final_set_critical_condition.update(host.treat()) | ||
117 | 118 | ||||
118 | return final_set_critical_condition | 119 | return final_set_critical_condition | ||
119 | 120 | ||||
120 | 121 | ||||
121 | 122 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
07.11.2023 08:59
07.11.2023 09:00
07.11.2023 09:01
07.11.2023 09:01