1from math import sqrt
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 = position
19
20 def get_position(self):
21 return self._position
22
23 def set_position(self, position):
24 self._position = position
25
26 def distance_to_person(self, person):
27 this_x, this_y = self.get_position()
28 other_x, other_y = person.get_position()
29 return sqrt((this_x - other_x)**2 + (this_y - other_y)**2)
30
31
32class Kid(Person):
33 def __init__(self, position, initiative):
34 super().__init__(position)
35 self._initiative = initiative
36 self._basket = []
37
38 def get_initiative(self):
39 return self._initiative
40
41 def add_candy(self, candy: Candy):
42 self._basket.append(candy)
43
44 def is_critical(self):
45 return sum([c.get_uranium_quantity() for c in self._basket]) > 20
46
47
48class Host(Person):
49 def __init__(self, position, candies):
50 super().__init__(position)
51 self._candies = [Candy(m, u) for m, u in candies]
52
53 def remove_candy(self, selector):
54 if len(self._candies) == 0:
55 return None
56 candy = selector(self._candies)
57 self._candies.remove(candy)
58 return candy
59
60
61class FluxCapacitor:
62 def __init__(self, participants):
63 self._participants = participants
64
65 def get_victims(self):
66 all_hosts = list(
67 filter(lambda p: type(p) is Host, self._participants))
68 all_kids = list(
69 filter(lambda p: type(p) is Kid, self._participants))
70
71 def selector(candies):
72 return max(candies, key=lambda c: c.get_mass())
73 close_to_kid = {}
74 for kid in all_kids:
75 close_to_kid[kid] = []
76 all_hosts.sort(key=lambda h: h.distance_to_person(kid))
77 [close_to_kid[kid].append(h) for h in all_hosts]
78 for i in range(len(all_hosts)):
79 guest_dict = {}
80 for host in all_hosts:
81 guest_dict[host] = []
82 for kid in all_kids:
83 host = close_to_kid[kid][i]
84 guest_dict[host].append(kid)
85 for host in all_hosts:
86 guest_dict[host].sort(
87 key=lambda k: k.get_initiative())
88 for guest in guest_dict[host]:
89 candy = host.remove_candy(selector)
90 guest.add_candy(candy)
91 victims = list(filter(lambda k: k.is_critical(), all_kids))
92 if len(victims) > 0:
93 return set(victims)
94 return None
.EEEEE......
======================================================================
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)
AttributeError: 'FluxCapacitor' object has no attribute 'get_victim'
======================================================================
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)
AttributeError: 'FluxCapacitor' object has no attribute 'get_victim'
======================================================================
ERROR: 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)
AttributeError: 'FluxCapacitor' object has no attribute 'get_victim'
======================================================================
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)
AttributeError: 'FluxCapacitor' object has no attribute 'get_victim'
======================================================================
ERROR: test_real_case (test.FluxCapacitorTest)
Test with real case.
----------------------------------------------------------------------
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 115, in test_real_case
self.assertEqual(FluxCapacitor({kid1, kid2, host1, host2}).get_victim(), {kid1, kid2})
AttributeError: 'FluxCapacitor' object has no attribute 'get_victim'
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (errors=5)
Виктор Бечев
08.11.2023 13:02Добро решение, малшанс, че си кръстил функцията грешно.
П.П. Тук таме имаш дребни грешки, които Жорката спомена на лекцията вчера, разгледай слайдовете и ще припознаеш някои от тях.
|