1import math
2
3
4class Candy:
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 def __init__(self, position):
18 self.position = list(position)
19
20 def get_position(self):
21 return self.position
22
23 def set_position(self, new_position):
24 self.position[0] = new_position[0]
25 self.position[1] = new_position[1]
26
27
28class Kid(Person):
29 def __init__(self, position, initiative):
30 super().__init__(position)
31 self.initiative = initiative
32 self.bucket = []
33 self.meeted_hosts = []
34
35 def get_initiative(self):
36 return self.initiative
37
38 def add_candy(self, candy):
39 self.bucket.append(candy)
40
41 def is_critical(self) :
42 sum = 0
43 for candy in self.bucket :
44 sum += candy.get_uranium_quantity()
45 return sum > 20
46 #return sum(candy.get_uranium_quantity() for candy in self.bucket) > 20
47
48
49def choose_candy(bucket):
50 return max(bucket, key=lambda candy: candy.get_mass())
51
52
53class Host(Person):
54 def __init__(self, position, candies) :
55 super().__init__(position)
56 self.candies = candies
57 self.bucket = []
58 for candy in candies:
59 current_candy = Candy(*candy)
60 self.bucket.append(current_candy)
61
62 def remove_candy(self, choose_candy_func) :
63 if not self.bucket:
64 return None
65 chosen_candy = choose_candy_func(self.bucket)
66 self.bucket.remove(chosen_candy)
67 return chosen_candy
68
69
70def find_distance(kid_position, host_positon) :
71 return math.sqrt((kid_position[0] - host_positon[0])**2 + (kid_position[1] - host_positon[1])**2)
72
73
74def find_closest(kid, hosts) :
75 if not hosts :
76 return None
77 closest_dist = float('inf') #това го видях от chatgpt, понеже в python няма int_max
78 closest_host = None
79 for host in hosts :
80 if host not in kid.meeted_hosts and host != closest_host :
81 curr_dist = find_distance(kid.position, host.position)
82 if curr_dist < closest_dist :
83 closest_dist = curr_dist
84 closest_host = host
85 elif curr_dist == closest_dist :
86 curr_x, curr_y = host.get_position()
87 closest_x, closest_y = closest_host.get_position()
88 if curr_x < closest_x or (curr_x == closest_x and curr_y < closest_y) :
89 closest_dist = curr_dist
90 closest_host = host
91 return closest_host
92
93
94class FluxCapacitor:
95 def __init__(self, participants) :
96 self.kids = []
97 self.hosts = []
98 self.victim_set = set()
99 for human in participants :
100 if isinstance(human, Kid) :
101 self.kids.append(human)
102 elif isinstance(human, Host) :
103 self.hosts.append(human)
104
105 def get_victim(self):
106 is_finished = False
107 if not self.kids :
108 return None
109 while not is_finished :
110 for kid in sorted(self.kids, key=lambda x: x.get_initiative(), reverse=True) :
111 closest_host = find_closest(kid, self.hosts)
112 if closest_host is None :
113 self.kids.remove(kid)
114 continue
115 curr_candy = closest_host.remove_candy(choose_candy)
116 if curr_candy == None :
117 kid.meeted_hosts.append(closest_host)
118 continue
119 kid.add_candy(curr_candy)
120 kid.meeted_hosts.append(closest_host)
121 kid.set_position(closest_host.get_position())
122 if kid.is_critical() :
123 self.victim_set.add(kid)
124 self.kids.remove(kid)
125 if len(self.victim_set) != 0 :
126 return self.victim_set
127 if not self.kids :
128 is_finished = True
129 break
130 return None
........F..F
======================================================================
FAIL: test_basic_usage (test.KidTest)
Test basic usage of Kid class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 39, in test_basic_usage
self.assertEqual(kid.get_position(), (0, 0))
AssertionError: [0, 0] != (0, 0)
======================================================================
FAIL: test_basic_usage (test.PersonTest)
Test basic usage of Person class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 24, in test_basic_usage
self.assertEqual(person.get_position(), (0, 0))
AssertionError: [0, 0] != (0, 0)
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (failures=2)
Георги Кунчев
07.11.2023 14:53Модифицирахме сайта си, така че дори да имаш безкраен цикъл, това ще афектира само част от тестовете, в които стигаш до този цикъл. Вече не трябва да се притесняваш от нула точки.
Но, да ти дам обратна връзка - вече нямаш проблем с безкрайни цикли.
|
Георги Кунчев
06.11.2023 21:22Не, все още имаш безкраен цикъл.
|
Георги Кунчев
06.11.2023 08:53Имаш безкраен цикъл е тестовете ни ще зависнат. Моля изтествай с реален случай, за да избегнеш нула точки.
|
f | 1 | import math | f | 1 | import math |
2 | 2 | ||||
3 | 3 | ||||
4 | class Candy: | 4 | class Candy: | ||
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 | def __init__(self, position): | 17 | def __init__(self, position): | ||
18 | self.position = list(position) | 18 | self.position = list(position) | ||
19 | 19 | ||||
20 | def get_position(self): | 20 | def get_position(self): | ||
21 | return self.position | 21 | return self.position | ||
22 | 22 | ||||
23 | def set_position(self, new_position): | 23 | def set_position(self, new_position): | ||
24 | self.position[0] = new_position[0] | 24 | self.position[0] = new_position[0] | ||
25 | self.position[1] = new_position[1] | 25 | self.position[1] = new_position[1] | ||
26 | 26 | ||||
27 | 27 | ||||
28 | class Kid(Person): | 28 | class Kid(Person): | ||
29 | def __init__(self, position, initiative): | 29 | def __init__(self, position, initiative): | ||
30 | super().__init__(position) | 30 | super().__init__(position) | ||
31 | self.initiative = initiative | 31 | self.initiative = initiative | ||
32 | self.bucket = [] | 32 | self.bucket = [] | ||
33 | self.meeted_hosts = [] | 33 | self.meeted_hosts = [] | ||
34 | 34 | ||||
35 | def get_initiative(self): | 35 | def get_initiative(self): | ||
36 | return self.initiative | 36 | return self.initiative | ||
37 | 37 | ||||
38 | def add_candy(self, candy): | 38 | def add_candy(self, candy): | ||
39 | self.bucket.append(candy) | 39 | self.bucket.append(candy) | ||
40 | 40 | ||||
n | 41 | def is_critical(self): | n | 41 | def is_critical(self) : |
42 | sum = 0 | ||||
43 | for candy in self.bucket : | ||||
44 | sum += candy.get_uranium_quantity() | ||||
45 | return sum > 20 | ||||
42 | return sum(candy.get_uranium_quantity() for candy in self.bucket) > 20 | 46 | #return sum(candy.get_uranium_quantity() for candy in self.bucket) > 20 | ||
43 | 47 | ||||
44 | 48 | ||||
45 | def choose_candy(bucket): | 49 | def choose_candy(bucket): | ||
46 | return max(bucket, key=lambda candy: candy.get_mass()) | 50 | return max(bucket, key=lambda candy: candy.get_mass()) | ||
47 | 51 | ||||
48 | 52 | ||||
49 | class Host(Person): | 53 | class Host(Person): | ||
50 | def __init__(self, position, candies) : | 54 | def __init__(self, position, candies) : | ||
51 | super().__init__(position) | 55 | super().__init__(position) | ||
52 | self.candies = candies | 56 | self.candies = candies | ||
53 | self.bucket = [] | 57 | self.bucket = [] | ||
54 | for candy in candies: | 58 | for candy in candies: | ||
55 | current_candy = Candy(*candy) | 59 | current_candy = Candy(*candy) | ||
56 | self.bucket.append(current_candy) | 60 | self.bucket.append(current_candy) | ||
57 | 61 | ||||
58 | def remove_candy(self, choose_candy_func) : | 62 | def remove_candy(self, choose_candy_func) : | ||
59 | if not self.bucket: | 63 | if not self.bucket: | ||
60 | return None | 64 | return None | ||
61 | chosen_candy = choose_candy_func(self.bucket) | 65 | chosen_candy = choose_candy_func(self.bucket) | ||
62 | self.bucket.remove(chosen_candy) | 66 | self.bucket.remove(chosen_candy) | ||
63 | return chosen_candy | 67 | return chosen_candy | ||
64 | 68 | ||||
65 | 69 | ||||
66 | def find_distance(kid_position, host_positon) : | 70 | def find_distance(kid_position, host_positon) : | ||
67 | return math.sqrt((kid_position[0] - host_positon[0])**2 + (kid_position[1] - host_positon[1])**2) | 71 | return math.sqrt((kid_position[0] - host_positon[0])**2 + (kid_position[1] - host_positon[1])**2) | ||
68 | 72 | ||||
69 | 73 | ||||
70 | def find_closest(kid, hosts) : | 74 | def find_closest(kid, hosts) : | ||
71 | if not hosts : | 75 | if not hosts : | ||
72 | return None | 76 | return None | ||
73 | closest_dist = float('inf') #това го видях от chatgpt, понеже в python няма int_max | 77 | closest_dist = float('inf') #това го видях от chatgpt, понеже в python няма int_max | ||
74 | closest_host = None | 78 | closest_host = None | ||
75 | for host in hosts : | 79 | for host in hosts : | ||
76 | if host not in kid.meeted_hosts and host != closest_host : | 80 | if host not in kid.meeted_hosts and host != closest_host : | ||
77 | curr_dist = find_distance(kid.position, host.position) | 81 | curr_dist = find_distance(kid.position, host.position) | ||
78 | if curr_dist < closest_dist : | 82 | if curr_dist < closest_dist : | ||
79 | closest_dist = curr_dist | 83 | closest_dist = curr_dist | ||
80 | closest_host = host | 84 | closest_host = host | ||
81 | elif curr_dist == closest_dist : | 85 | elif curr_dist == closest_dist : | ||
82 | curr_x, curr_y = host.get_position() | 86 | curr_x, curr_y = host.get_position() | ||
83 | closest_x, closest_y = closest_host.get_position() | 87 | closest_x, closest_y = closest_host.get_position() | ||
84 | if curr_x < closest_x or (curr_x == closest_x and curr_y < closest_y) : | 88 | if curr_x < closest_x or (curr_x == closest_x and curr_y < closest_y) : | ||
85 | closest_dist = curr_dist | 89 | closest_dist = curr_dist | ||
86 | closest_host = host | 90 | closest_host = host | ||
87 | return closest_host | 91 | return closest_host | ||
88 | 92 | ||||
89 | 93 | ||||
90 | class FluxCapacitor: | 94 | class FluxCapacitor: | ||
91 | def __init__(self, participants) : | 95 | def __init__(self, participants) : | ||
92 | self.kids = [] | 96 | self.kids = [] | ||
93 | self.hosts = [] | 97 | self.hosts = [] | ||
94 | self.victim_set = set() | 98 | self.victim_set = set() | ||
95 | for human in participants : | 99 | for human in participants : | ||
96 | if isinstance(human, Kid) : | 100 | if isinstance(human, Kid) : | ||
97 | self.kids.append(human) | 101 | self.kids.append(human) | ||
98 | elif isinstance(human, Host) : | 102 | elif isinstance(human, Host) : | ||
99 | self.hosts.append(human) | 103 | self.hosts.append(human) | ||
100 | 104 | ||||
101 | def get_victim(self): | 105 | def get_victim(self): | ||
102 | is_finished = False | 106 | is_finished = False | ||
103 | if not self.kids : | 107 | if not self.kids : | ||
104 | return None | 108 | return None | ||
105 | while not is_finished : | 109 | while not is_finished : | ||
106 | for kid in sorted(self.kids, key=lambda x: x.get_initiative(), reverse=True) : | 110 | for kid in sorted(self.kids, key=lambda x: x.get_initiative(), reverse=True) : | ||
107 | closest_host = find_closest(kid, self.hosts) | 111 | closest_host = find_closest(kid, self.hosts) | ||
108 | if closest_host is None : | 112 | if closest_host is None : | ||
109 | self.kids.remove(kid) | 113 | self.kids.remove(kid) | ||
110 | continue | 114 | continue | ||
111 | curr_candy = closest_host.remove_candy(choose_candy) | 115 | curr_candy = closest_host.remove_candy(choose_candy) | ||
112 | if curr_candy == None : | 116 | if curr_candy == None : | ||
t | t | 117 | kid.meeted_hosts.append(closest_host) | ||
113 | continue | 118 | continue | ||
114 | kid.add_candy(curr_candy) | 119 | kid.add_candy(curr_candy) | ||
115 | kid.meeted_hosts.append(closest_host) | 120 | kid.meeted_hosts.append(closest_host) | ||
116 | kid.set_position(closest_host.get_position()) | 121 | kid.set_position(closest_host.get_position()) | ||
117 | if kid.is_critical() : | 122 | if kid.is_critical() : | ||
118 | self.victim_set.add(kid) | 123 | self.victim_set.add(kid) | ||
119 | self.kids.remove(kid) | 124 | self.kids.remove(kid) | ||
120 | if len(self.victim_set) != 0 : | 125 | if len(self.victim_set) != 0 : | ||
121 | return self.victim_set | 126 | return self.victim_set | ||
122 | if not self.kids : | 127 | if not self.kids : | ||
123 | is_finished = True | 128 | is_finished = True | ||
124 | break | 129 | break | ||
125 | return None | 130 | return None |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
2 | 2 | ||||
3 | 3 | ||||
4 | class Candy: | 4 | class Candy: | ||
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 | def __init__(self, position): | 17 | def __init__(self, position): | ||
18 | self.position = list(position) | 18 | self.position = list(position) | ||
19 | 19 | ||||
20 | def get_position(self): | 20 | def get_position(self): | ||
21 | return self.position | 21 | return self.position | ||
22 | 22 | ||||
23 | def set_position(self, new_position): | 23 | def set_position(self, new_position): | ||
24 | self.position[0] = new_position[0] | 24 | self.position[0] = new_position[0] | ||
25 | self.position[1] = new_position[1] | 25 | self.position[1] = new_position[1] | ||
26 | 26 | ||||
27 | 27 | ||||
28 | class Kid(Person): | 28 | class Kid(Person): | ||
29 | def __init__(self, position, initiative): | 29 | def __init__(self, position, initiative): | ||
30 | super().__init__(position) | 30 | super().__init__(position) | ||
31 | self.initiative = initiative | 31 | self.initiative = initiative | ||
32 | self.bucket = [] | 32 | self.bucket = [] | ||
33 | self.meeted_hosts = [] | 33 | self.meeted_hosts = [] | ||
34 | 34 | ||||
35 | def get_initiative(self): | 35 | def get_initiative(self): | ||
36 | return self.initiative | 36 | return self.initiative | ||
37 | 37 | ||||
38 | def add_candy(self, candy): | 38 | def add_candy(self, candy): | ||
39 | self.bucket.append(candy) | 39 | self.bucket.append(candy) | ||
40 | 40 | ||||
41 | def is_critical(self): | 41 | def is_critical(self): | ||
42 | return sum(candy.get_uranium_quantity() for candy in self.bucket) > 20 | 42 | return sum(candy.get_uranium_quantity() for candy in self.bucket) > 20 | ||
43 | 43 | ||||
44 | 44 | ||||
45 | def choose_candy(bucket): | 45 | def choose_candy(bucket): | ||
46 | return max(bucket, key=lambda candy: candy.get_mass()) | 46 | return max(bucket, key=lambda candy: candy.get_mass()) | ||
47 | 47 | ||||
48 | 48 | ||||
49 | class Host(Person): | 49 | class Host(Person): | ||
n | 50 | def __init__(self, position, candies): | n | 50 | def __init__(self, position, candies) : |
51 | super().__init__(position) | 51 | super().__init__(position) | ||
52 | self.candies = candies | 52 | self.candies = candies | ||
53 | self.bucket = [] | 53 | self.bucket = [] | ||
54 | for candy in candies: | 54 | for candy in candies: | ||
55 | current_candy = Candy(*candy) | 55 | current_candy = Candy(*candy) | ||
56 | self.bucket.append(current_candy) | 56 | self.bucket.append(current_candy) | ||
57 | 57 | ||||
n | 58 | def remove_candy(self, choose_candy_func): | n | 58 | def remove_candy(self, choose_candy_func) : |
59 | if not self.bucket: | 59 | if not self.bucket: | ||
60 | return None | 60 | return None | ||
61 | chosen_candy = choose_candy_func(self.bucket) | 61 | chosen_candy = choose_candy_func(self.bucket) | ||
62 | self.bucket.remove(chosen_candy) | 62 | self.bucket.remove(chosen_candy) | ||
63 | return chosen_candy | 63 | return chosen_candy | ||
64 | 64 | ||||
65 | 65 | ||||
n | 66 | def find_distance(kid_position, host_positon): | n | 66 | def find_distance(kid_position, host_positon) : |
67 | return math.sqrt((kid_position[0] - host_positon[0])**2 + (kid_position[1] - host_positon[1])**2) | 67 | return math.sqrt((kid_position[0] - host_positon[0])**2 + (kid_position[1] - host_positon[1])**2) | ||
68 | 68 | ||||
69 | 69 | ||||
n | 70 | def find_closest(kid, hosts): | n | 70 | def find_closest(kid, hosts) : |
71 | if not hosts : | 71 | if not hosts : | ||
72 | return None | 72 | return None | ||
73 | closest_dist = float('inf') #това го видях от chatgpt, понеже в python няма int_max | 73 | closest_dist = float('inf') #това го видях от chatgpt, понеже в python няма int_max | ||
74 | closest_host = None | 74 | closest_host = None | ||
n | 75 | for host in hosts: | n | 75 | for host in hosts : |
76 | if host not in kid.meeted_hosts and host != closest_host : | 76 | if host not in kid.meeted_hosts and host != closest_host : | ||
77 | curr_dist = find_distance(kid.position, host.position) | 77 | curr_dist = find_distance(kid.position, host.position) | ||
n | 78 | if curr_dist < closest_dist: | n | 78 | if curr_dist < closest_dist : |
79 | closest_dist = curr_dist | 79 | closest_dist = curr_dist | ||
80 | closest_host = host | 80 | closest_host = host | ||
n | 81 | elif curr_dist == closest_dist: | n | 81 | elif curr_dist == closest_dist : |
82 | curr_x, curr_y = host.get_position() | 82 | curr_x, curr_y = host.get_position() | ||
83 | closest_x, closest_y = closest_host.get_position() | 83 | closest_x, closest_y = closest_host.get_position() | ||
n | 84 | if curr_x < closest_x or (curr_x == closest_x and curr_y < closest_y): | n | 84 | if curr_x < closest_x or (curr_x == closest_x and curr_y < closest_y) : |
85 | closest_dist = curr_dist | 85 | closest_dist = curr_dist | ||
86 | closest_host = host | 86 | closest_host = host | ||
87 | return closest_host | 87 | return closest_host | ||
88 | 88 | ||||
89 | 89 | ||||
90 | class FluxCapacitor: | 90 | class FluxCapacitor: | ||
n | 91 | def __init__(self, participants): | n | 91 | def __init__(self, participants) : |
92 | self.kids = [] | 92 | self.kids = [] | ||
93 | self.hosts = [] | 93 | self.hosts = [] | ||
94 | self.victim_set = set() | 94 | self.victim_set = set() | ||
n | 95 | for human in participants: | n | 95 | for human in participants : |
96 | if isinstance(human, Kid) : | 96 | if isinstance(human, Kid) : | ||
97 | self.kids.append(human) | 97 | self.kids.append(human) | ||
98 | elif isinstance(human, Host) : | 98 | elif isinstance(human, Host) : | ||
99 | self.hosts.append(human) | 99 | self.hosts.append(human) | ||
100 | 100 | ||||
101 | def get_victim(self): | 101 | def get_victim(self): | ||
102 | is_finished = False | 102 | is_finished = False | ||
n | 103 | if not self.kids: | n | 103 | if not self.kids : |
104 | return None | 104 | return None | ||
n | 105 | while(not is_finished): | n | 105 | while not is_finished : |
106 | for kid in sorted(self.kids, key=lambda x: x.get_initiative(), reverse=True): | 106 | for kid in sorted(self.kids, key=lambda x: x.get_initiative(), reverse=True) : | ||
107 | closest_host = find_closest(kid, self.hosts) | 107 | closest_host = find_closest(kid, self.hosts) | ||
n | 108 | if(closest_host is None): | n | 108 | if closest_host is None : |
109 | self.kids.remove(kid) | 109 | self.kids.remove(kid) | ||
110 | continue | 110 | continue | ||
111 | curr_candy = closest_host.remove_candy(choose_candy) | 111 | curr_candy = closest_host.remove_candy(choose_candy) | ||
n | 112 | if curr_candy == None: | n | 112 | if curr_candy == None : |
113 | continue | 113 | continue | ||
114 | kid.add_candy(curr_candy) | 114 | kid.add_candy(curr_candy) | ||
115 | kid.meeted_hosts.append(closest_host) | 115 | kid.meeted_hosts.append(closest_host) | ||
116 | kid.set_position(closest_host.get_position()) | 116 | kid.set_position(closest_host.get_position()) | ||
117 | if kid.is_critical() : | 117 | if kid.is_critical() : | ||
118 | self.victim_set.add(kid) | 118 | self.victim_set.add(kid) | ||
119 | self.kids.remove(kid) | 119 | self.kids.remove(kid) | ||
n | 120 | if len(self.victim_set) != 0: | n | 120 | if len(self.victim_set) != 0 : |
121 | return self.victim_set | 121 | return self.victim_set | ||
t | 122 | if not self.kids: | t | 122 | if not self.kids : |
123 | is_finished = True | 123 | is_finished = True | ||
124 | break | 124 | break | ||
125 | return None | 125 | return None |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
n | n | 2 | |||
2 | 3 | ||||
3 | class Candy: | 4 | class Candy: | ||
4 | def __init__(self, mass, uranium): | 5 | def __init__(self, mass, uranium): | ||
5 | self.mass = mass | 6 | self.mass = mass | ||
n | 6 | if uranium < 0 or uranium > 1 : | n | ||
7 | raise ValueError("Value must be between 0 and 1") | ||||
8 | self.uranium = uranium | 7 | self.uranium = uranium | ||
9 | 8 | ||||
10 | def get_uranium_quantity(self): | 9 | def get_uranium_quantity(self): | ||
11 | return self.mass * self.uranium | 10 | return self.mass * self.uranium | ||
12 | 11 | ||||
13 | def get_mass(self): | 12 | def get_mass(self): | ||
14 | return self.mass | 13 | return self.mass | ||
15 | 14 | ||||
16 | 15 | ||||
17 | class Person: | 16 | class Person: | ||
18 | def __init__(self, position): | 17 | def __init__(self, position): | ||
19 | self.position = list(position) | 18 | self.position = list(position) | ||
20 | 19 | ||||
21 | def get_position(self): | 20 | def get_position(self): | ||
22 | return self.position | 21 | return self.position | ||
23 | 22 | ||||
24 | def set_position(self, new_position): | 23 | def set_position(self, new_position): | ||
25 | self.position[0] = new_position[0] | 24 | self.position[0] = new_position[0] | ||
26 | self.position[1] = new_position[1] | 25 | self.position[1] = new_position[1] | ||
27 | 26 | ||||
28 | 27 | ||||
29 | class Kid(Person): | 28 | class Kid(Person): | ||
30 | def __init__(self, position, initiative): | 29 | def __init__(self, position, initiative): | ||
31 | super().__init__(position) | 30 | super().__init__(position) | ||
n | 32 | self.initative = initiative | n | 31 | self.initiative = initiative |
33 | self.bucket = [] | 32 | self.bucket = [] | ||
34 | self.meeted_hosts = [] | 33 | self.meeted_hosts = [] | ||
35 | 34 | ||||
n | 36 | def get_iniative(self): | n | 35 | def get_initiative(self): |
37 | return self.initative | 36 | return self.initiative | ||
38 | 37 | ||||
39 | def add_candy(self, candy): | 38 | def add_candy(self, candy): | ||
40 | self.bucket.append(candy) | 39 | self.bucket.append(candy) | ||
41 | 40 | ||||
42 | def is_critical(self): | 41 | def is_critical(self): | ||
n | 43 | sum = 0 | n | 42 | return sum(candy.get_uranium_quantity() for candy in self.bucket) > 20 |
44 | for candy in self.bucket: | 43 | |||
45 | sum += candy.get_uranium_quantity() | ||||
46 | return sum > 20 | ||||
47 | 44 | ||||
48 | def choose_candy(bucket): | 45 | def choose_candy(bucket): | ||
49 | return max(bucket, key=lambda candy: candy.get_mass()) | 46 | return max(bucket, key=lambda candy: candy.get_mass()) | ||
n | n | 47 | |||
50 | 48 | ||||
51 | class Host(Person): | 49 | class Host(Person): | ||
52 | def __init__(self, position, candies): | 50 | def __init__(self, position, candies): | ||
53 | super().__init__(position) | 51 | super().__init__(position) | ||
54 | self.candies = candies | 52 | self.candies = candies | ||
55 | self.bucket = [] | 53 | self.bucket = [] | ||
56 | for candy in candies: | 54 | for candy in candies: | ||
n | 57 | current_candy = Candy(candy[0], candy[1]) | n | 55 | current_candy = Candy(*candy) |
58 | self.bucket.append(current_candy) | 56 | self.bucket.append(current_candy) | ||
59 | 57 | ||||
60 | def remove_candy(self, choose_candy_func): | 58 | def remove_candy(self, choose_candy_func): | ||
61 | if not self.bucket: | 59 | if not self.bucket: | ||
62 | return None | 60 | return None | ||
63 | chosen_candy = choose_candy_func(self.bucket) | 61 | chosen_candy = choose_candy_func(self.bucket) | ||
64 | self.bucket.remove(chosen_candy) | 62 | self.bucket.remove(chosen_candy) | ||
65 | return chosen_candy | 63 | return chosen_candy | ||
66 | 64 | ||||
n | n | 65 | |||
67 | def find_distance(kid_position, host_positon): | 66 | def find_distance(kid_position, host_positon): | ||
68 | return math.sqrt((kid_position[0] - host_positon[0])**2 + (kid_position[1] - host_positon[1])**2) | 67 | return math.sqrt((kid_position[0] - host_positon[0])**2 + (kid_position[1] - host_positon[1])**2) | ||
n | n | 68 | |||
69 | 69 | ||||
70 | def find_closest(kid, hosts): | 70 | def find_closest(kid, hosts): | ||
71 | if not hosts : | 71 | if not hosts : | ||
72 | return None | 72 | return None | ||
73 | closest_dist = float('inf') #това го видях от chatgpt, понеже в python няма int_max | 73 | closest_dist = float('inf') #това го видях от chatgpt, понеже в python няма int_max | ||
74 | closest_host = None | 74 | closest_host = None | ||
75 | for host in hosts: | 75 | for host in hosts: | ||
76 | if host not in kid.meeted_hosts and host != closest_host : | 76 | if host not in kid.meeted_hosts and host != closest_host : | ||
77 | curr_dist = find_distance(kid.position, host.position) | 77 | curr_dist = find_distance(kid.position, host.position) | ||
78 | if curr_dist < closest_dist: | 78 | if curr_dist < closest_dist: | ||
79 | closest_dist = curr_dist | 79 | closest_dist = curr_dist | ||
80 | closest_host = host | 80 | closest_host = host | ||
81 | elif curr_dist == closest_dist: | 81 | elif curr_dist == closest_dist: | ||
82 | curr_x, curr_y = host.get_position() | 82 | curr_x, curr_y = host.get_position() | ||
83 | closest_x, closest_y = closest_host.get_position() | 83 | closest_x, closest_y = closest_host.get_position() | ||
84 | if curr_x < closest_x or (curr_x == closest_x and curr_y < closest_y): | 84 | if curr_x < closest_x or (curr_x == closest_x and curr_y < closest_y): | ||
85 | closest_dist = curr_dist | 85 | closest_dist = curr_dist | ||
86 | closest_host = host | 86 | closest_host = host | ||
87 | return closest_host | 87 | return closest_host | ||
88 | 88 | ||||
89 | 89 | ||||
90 | class FluxCapacitor: | 90 | class FluxCapacitor: | ||
91 | def __init__(self, participants): | 91 | def __init__(self, participants): | ||
92 | self.kids = [] | 92 | self.kids = [] | ||
93 | self.hosts = [] | 93 | self.hosts = [] | ||
94 | self.victim_set = set() | 94 | self.victim_set = set() | ||
95 | for human in participants: | 95 | for human in participants: | ||
n | 96 | if(type(human) == Kid): | n | 96 | if isinstance(human, Kid) : |
97 | self.kids.append(human) | 97 | self.kids.append(human) | ||
n | 98 | elif(type(human) == Host): | n | 98 | elif isinstance(human, Host) : |
99 | self.hosts.append(human) | 99 | self.hosts.append(human) | ||
100 | 100 | ||||
101 | def get_victim(self): | 101 | def get_victim(self): | ||
102 | is_finished = False | 102 | is_finished = False | ||
n | n | 103 | if not self.kids: | ||
104 | return None | ||||
103 | while(is_finished == False): | 105 | while(not is_finished): | ||
104 | for kid in sorted(self.kids, key=lambda x: x.get_iniative(), reverse=True): | 106 | for kid in sorted(self.kids, key=lambda x: x.get_initiative(), reverse=True): | ||
105 | closest_host = find_closest(kid, self.hosts) | 107 | closest_host = find_closest(kid, self.hosts) | ||
n | 106 | if(closest_host == None): | n | 108 | if(closest_host is None): |
107 | self.kids.remove(kid) | 109 | self.kids.remove(kid) | ||
108 | continue | 110 | continue | ||
109 | curr_candy = closest_host.remove_candy(choose_candy) | 111 | curr_candy = closest_host.remove_candy(choose_candy) | ||
110 | if curr_candy == None: | 112 | if curr_candy == None: | ||
111 | continue | 113 | continue | ||
112 | kid.add_candy(curr_candy) | 114 | kid.add_candy(curr_candy) | ||
113 | kid.meeted_hosts.append(closest_host) | 115 | kid.meeted_hosts.append(closest_host) | ||
114 | kid.set_position(closest_host.get_position()) | 116 | kid.set_position(closest_host.get_position()) | ||
115 | if kid.is_critical() : | 117 | if kid.is_critical() : | ||
116 | self.victim_set.add(kid) | 118 | self.victim_set.add(kid) | ||
117 | self.kids.remove(kid) | 119 | self.kids.remove(kid) | ||
118 | if len(self.victim_set) != 0: | 120 | if len(self.victim_set) != 0: | ||
119 | return self.victim_set | 121 | return self.victim_set | ||
120 | if not self.kids: | 122 | if not self.kids: | ||
121 | is_finished = True | 123 | is_finished = True | ||
t | t | 124 | break | ||
122 | return None | 125 | return None |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|