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(гледа кой бонбон да махне, според масата) трябва да я оставя или не.
П.П качих го втори път, защото не променях позицията на децата, когато отидат при някой хост
|
f | 1 | class Candy: | f | 1 | class Candy: |
2 | def __init__(self, mass, uranium): | 2 | def __init__(self, mass, uranium): | ||
3 | self.mass = mass | 3 | self.mass = mass | ||
4 | self.uranium = uranium | 4 | self.uranium = uranium | ||
5 | 5 | ||||
6 | def get_uranium_quantity(self): | 6 | def get_uranium_quantity(self): | ||
7 | return self.mass * self.uranium | 7 | return self.mass * self.uranium | ||
8 | 8 | ||||
9 | def get_mass(self): | 9 | def get_mass(self): | ||
10 | return self.mass | 10 | return self.mass | ||
11 | 11 | ||||
12 | 12 | ||||
13 | class Person: | 13 | class Person: | ||
14 | def __init__(self, position): | 14 | def __init__(self, position): | ||
15 | self.position = position | 15 | self.position = position | ||
16 | 16 | ||||
17 | def get_position(self): | 17 | def get_position(self): | ||
18 | return self.position | 18 | return self.position | ||
19 | 19 | ||||
20 | def set_postition(self, new_position): | 20 | def set_postition(self, new_position): | ||
21 | self.position = new_position | 21 | 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 | ||||
26 | 26 | ||||
27 | class Kid(Person): | 27 | class Kid(Person): | ||
28 | MAX_URANIUM = 20 | 28 | 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 = 0 | 32 | self.sum_of_candies = 0 | ||
33 | super().__init__(position) | 33 | super().__init__(position) | ||
34 | self.initiative = initiative | 34 | self.initiative = initiative | ||
35 | 35 | ||||
36 | def get_initiative(self): | 36 | def get_initiative(self): | ||
37 | return self.initiative | 37 | 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 = candy | 41 | 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 None | 46 | 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]) | ||
n | 49 | index_of_host = 0 | n | 49 | min_host = self.list_of_hosts[0] |
50 | 50 | ||||
n | 51 | for curr_host_ind in range(1, len(self.list_of_hosts)): | n | 51 | 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_dist | 54 | min_dist = next_dist | ||
n | 55 | index_of_host = curr_host_ind | n | 55 | min_host = host |
56 | elif min_dist == next_dist: | 56 | elif min_dist == next_dist: | ||
n | 57 | if self.list_of_hosts[curr_host_ind].position[0] < self.list_of_hosts[index_of_host].position[0]: | n | 57 | if host.position[0] < min_host.position[0]: |
58 | min_dist = next_dist | 58 | min_dist = next_dist | ||
n | 59 | index_of_host = curr_host_ind | n | 59 | 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_dist | 62 | min_dist = next_dist | ||
n | 63 | index_of_host = curr_host_ind | n | 63 | min_host = host |
64 | 64 | ||||
n | 65 | self.add_candy(self.list_of_hosts[index_of_host].remove_candy(Host.function_for_removal)) | n | 65 | self.add_candy(min_host.remove_candy(Host.function_for_removal)) |
66 | self.position = self.list_of_hosts[index_of_host].position | 66 | 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 = 0 | 70 | 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_URANIUM | 73 | return self.sum_of_candies > self.MAX_URANIUM | ||
74 | 74 | ||||
75 | 75 | ||||
76 | class Host(Person): | 76 | class Host(Person): | ||
77 | 77 | ||||
78 | @staticmethod | 78 | @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()) | ||
81 | 81 | ||||
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 None | 91 | 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_remove | 95 | return candy_to_remove | ||
96 | 96 | ||||
97 | 97 | ||||
98 | class FluxCapacitor: | 98 | class 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 = False | 118 | 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 = True | 122 | is_there_someone_to_visit = True | ||
123 | break | 123 | break | ||
124 | 124 | ||||
125 | if is_there_someone_to_visit == False: | 125 | if is_there_someone_to_visit == False: | ||
126 | return None | 126 | 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(): | ||
n | 133 | 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_dead | 136 | return self.set_of_dead | ||
138 | 137 | ||||
t | 139 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | class Candy: | f | 1 | class Candy: |
2 | def __init__(self, mass, uranium): | 2 | def __init__(self, mass, uranium): | ||
3 | self.mass = mass | 3 | self.mass = mass | ||
4 | self.uranium = uranium | 4 | self.uranium = uranium | ||
5 | 5 | ||||
6 | def get_uranium_quantity(self): | 6 | def get_uranium_quantity(self): | ||
7 | return self.mass * self.uranium | 7 | return self.mass * self.uranium | ||
8 | 8 | ||||
9 | def get_mass(self): | 9 | def get_mass(self): | ||
10 | return self.mass | 10 | return self.mass | ||
11 | 11 | ||||
12 | 12 | ||||
13 | class Person: | 13 | class Person: | ||
14 | def __init__(self, position): | 14 | def __init__(self, position): | ||
15 | self.position = position | 15 | self.position = position | ||
16 | 16 | ||||
17 | def get_position(self): | 17 | def get_position(self): | ||
18 | return self.position | 18 | return self.position | ||
19 | 19 | ||||
20 | def set_postition(self, new_position): | 20 | def set_postition(self, new_position): | ||
21 | self.position = new_position | 21 | self.position = new_position | ||
22 | 22 | ||||
23 | def get_distance(self, other_person): | 23 | def get_distance(self, other_person): | ||
t | 24 | return ((self.position[0] - other_person.position[0]) * 2 + (self.position[1] - other_person.position[1]) * 2) ** (1/2) | t | 24 | return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2) |
25 | 25 | ||||
26 | 26 | ||||
27 | class Kid(Person): | 27 | class Kid(Person): | ||
28 | MAX_URANIUM = 20 | 28 | 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 = 0 | 32 | self.sum_of_candies = 0 | ||
33 | super().__init__(position) | 33 | super().__init__(position) | ||
34 | self.initiative = initiative | 34 | self.initiative = initiative | ||
35 | 35 | ||||
36 | def get_initiative(self): | 36 | def get_initiative(self): | ||
37 | return self.initiative | 37 | 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 = candy | 41 | 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 None | 46 | 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 = 0 | 49 | 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_dist | 54 | min_dist = next_dist | ||
55 | index_of_host = curr_host_ind | 55 | 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_dist | 58 | min_dist = next_dist | ||
59 | index_of_host = curr_host_ind | 59 | 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_dist | 62 | min_dist = next_dist | ||
63 | index_of_host = curr_host_ind | 63 | 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].position | 66 | 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 = 0 | 70 | 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_URANIUM | 73 | return self.sum_of_candies > self.MAX_URANIUM | ||
74 | 74 | ||||
75 | 75 | ||||
76 | class Host(Person): | 76 | class Host(Person): | ||
77 | 77 | ||||
78 | @staticmethod | 78 | @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()) | ||
81 | 81 | ||||
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 None | 91 | 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_remove | 95 | return candy_to_remove | ||
96 | 96 | ||||
97 | 97 | ||||
98 | class FluxCapacitor: | 98 | class 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 = False | 118 | 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 = True | 122 | is_there_someone_to_visit = True | ||
123 | break | 123 | break | ||
124 | 124 | ||||
125 | if is_there_someone_to_visit == False: | 125 | if is_there_someone_to_visit == False: | ||
126 | return None | 126 | 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_dead | 137 | return self.set_of_dead | ||
138 | 138 | ||||
139 | 139 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | class Candy: | f | 1 | class Candy: |
2 | def __init__(self, mass, uranium): | 2 | def __init__(self, mass, uranium): | ||
3 | self.mass = mass | 3 | self.mass = mass | ||
4 | self.uranium = uranium | 4 | self.uranium = uranium | ||
5 | 5 | ||||
6 | def get_uranium_quantity(self): | 6 | def get_uranium_quantity(self): | ||
7 | return self.mass * self.uranium | 7 | return self.mass * self.uranium | ||
8 | 8 | ||||
9 | def get_mass(self): | 9 | def get_mass(self): | ||
10 | return self.mass | 10 | return self.mass | ||
11 | 11 | ||||
12 | 12 | ||||
13 | class Person: | 13 | class Person: | ||
14 | def __init__(self, position): | 14 | def __init__(self, position): | ||
15 | self.position = position | 15 | self.position = position | ||
16 | 16 | ||||
17 | def get_position(self): | 17 | def get_position(self): | ||
18 | return self.position | 18 | return self.position | ||
19 | 19 | ||||
20 | def set_postition(self, new_position): | 20 | def set_postition(self, new_position): | ||
21 | self.position = new_position | 21 | self.position = new_position | ||
22 | 22 | ||||
23 | def get_distance(self, other_person): | 23 | def get_distance(self, other_person): | ||
n | 24 | return ((self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2) ** (1/2) | n | 24 | return ((self.position[0] - other_person.position[0]) * 2 + (self.position[1] - other_person.position[1]) * 2) ** (1/2) |
25 | 25 | ||||
26 | 26 | ||||
27 | class Kid(Person): | 27 | class Kid(Person): | ||
n | 28 | n | 28 | 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 = 0 | 32 | self.sum_of_candies = 0 | ||
33 | super().__init__(position) | 33 | super().__init__(position) | ||
34 | self.initiative = initiative | 34 | self.initiative = initiative | ||
35 | 35 | ||||
36 | def get_initiative(self): | 36 | def get_initiative(self): | ||
37 | return self.initiative | 37 | 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 = candy | 41 | 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): | ||
n | 45 | if len(self.list_of_hosts) == 0: | n | 45 | if not self.list_of_hosts: |
46 | return None | 46 | 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 = 0 | 49 | 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_dist | 54 | min_dist = next_dist | ||
55 | index_of_host = curr_host_ind | 55 | 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_dist | 58 | min_dist = next_dist | ||
59 | index_of_host = curr_host_ind | 59 | 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_dist | 62 | min_dist = next_dist | ||
63 | index_of_host = curr_host_ind | 63 | index_of_host = curr_host_ind | ||
64 | 64 | ||||
n | 65 | self.add_candy(self.list_of_hosts[index_of_host].remove_candy(function_for_removal)) | n | 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].position | 66 | 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): | ||
n | n | 70 | 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() | ||
n | 72 | return self.sum_of_candies > 20 | n | 73 | return self.sum_of_candies > self.MAX_URANIUM |
73 | 74 | ||||
74 | 75 | ||||
75 | class Host(Person): | 76 | class Host(Person): | ||
n | n | 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 | |||||
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: | ||
n | 81 | self.candies.append(Candy(candy[0], candy[1])) | n | 87 | 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 None | 91 | 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_remove | 95 | return candy_to_remove | ||
90 | 96 | ||||
91 | 97 | ||||
92 | class FluxCapacitor: | 98 | class 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 = False | 118 | 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 = True | 122 | is_there_someone_to_visit = True | ||
117 | break | 123 | break | ||
118 | 124 | ||||
119 | if is_there_someone_to_visit == False: | 125 | if is_there_someone_to_visit == False: | ||
120 | return None | 126 | 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(): | ||
n | n | 133 | 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_dead | 137 | return self.set_of_dead | ||
131 | 138 | ||||
132 | 139 | ||||
t | 133 | t | |||
134 | # nqkva primerna funkciq | ||||
135 | def 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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | class Candy: | f | 1 | class Candy: |
2 | def __init__(self, mass, uranium): | 2 | def __init__(self, mass, uranium): | ||
3 | self.mass = mass | 3 | self.mass = mass | ||
4 | self.uranium = uranium | 4 | self.uranium = uranium | ||
5 | 5 | ||||
6 | def get_uranium_quantity(self): | 6 | def get_uranium_quantity(self): | ||
7 | return self.mass * self.uranium | 7 | return self.mass * self.uranium | ||
8 | 8 | ||||
9 | def get_mass(self): | 9 | def get_mass(self): | ||
10 | return self.mass | 10 | return self.mass | ||
11 | 11 | ||||
12 | 12 | ||||
13 | class Person: | 13 | class Person: | ||
14 | def __init__(self, position): | 14 | def __init__(self, position): | ||
15 | self.position = position | 15 | self.position = position | ||
16 | 16 | ||||
17 | def get_position(self): | 17 | def get_position(self): | ||
18 | return self.position | 18 | return self.position | ||
19 | 19 | ||||
20 | def set_postition(self, new_position): | 20 | def set_postition(self, new_position): | ||
21 | self.position = new_position | 21 | 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 | ||||
26 | 26 | ||||
27 | class Kid(Person): | 27 | class 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 = 0 | 32 | self.sum_of_candies = 0 | ||
33 | super().__init__(position) | 33 | super().__init__(position) | ||
34 | self.initiative = initiative | 34 | self.initiative = initiative | ||
35 | 35 | ||||
36 | def get_initiative(self): | 36 | def get_initiative(self): | ||
37 | return self.initiative | 37 | 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 = candy | 41 | 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 None | 46 | 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 = 0 | 49 | 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_dist | 54 | min_dist = next_dist | ||
55 | index_of_host = curr_host_ind | 55 | 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_dist | 58 | min_dist = next_dist | ||
59 | index_of_host = curr_host_ind | 59 | 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_dist | 62 | min_dist = next_dist | ||
63 | index_of_host = curr_host_ind | 63 | 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)) | ||
n | n | 66 | 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 > 20 | 72 | return self.sum_of_candies > 20 | ||
72 | 73 | ||||
73 | 74 | ||||
74 | class Host(Person): | 75 | class 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 None | 85 | 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_remove | 89 | return candy_to_remove | ||
89 | 90 | ||||
90 | 91 | ||||
91 | class FluxCapacitor: | 92 | class 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 = False | 112 | 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 = True | 116 | is_there_someone_to_visit = True | ||
116 | break | 117 | break | ||
117 | 118 | ||||
118 | if is_there_someone_to_visit == False: | 119 | if is_there_someone_to_visit == False: | ||
119 | return None | 120 | 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_dead | 130 | return self.set_of_dead | ||
130 | 131 | ||||
131 | 132 | ||||
n | n | 133 | |||
132 | # nqkva primerna funkciq | 134 | # nqkva primerna funkciq | ||
133 | def function_for_removal(my_list_of_candies): | 135 | def 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 = candy | 139 | max_mass_candy = candy | ||
138 | 140 | ||||
139 | return max_mass_candy | 141 | return max_mass_candy | ||
140 | 142 | ||||
t | 141 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|