1import math
2
3class Candy:
4 def __init__(self, mass, uranium):
5 self.mass = mass
6 self.uranium = uranium
7
8 def get_uranium_quantity(self):
9 return self.mass * self.uranium
10
11 def get_mass(self):
12 return self.mass
13
14
15class Person:
16 def __init__(self, position):
17 self.position = position
18
19 def get_position(self):
20 return self.position
21
22 def set_position(self, new_position):
23 self.position = new_position
24
25
26class Kid(Person):
27
28 max_uranium = int(20)
29
30 def __init__(self, position, initiative):
31 super().__init__(position)
32 self.initiative = initiative
33 self.basket = []
34
35 def get_initiative(self):
36 return self.initiative
37
38 def add_candy(self, candy):
39 self.basket.append(candy)
40
41 def is_critical(self):
42 sum_uranium = sum(candy.get_uranium_quantity() for candy in self.basket)
43 return sum_uranium > self.max_uranium
44
45
46class Host(Person):
47 def __init__(self, position, candies):
48 super().__init__(position)
49 self.candies = candies
50
51
52 def remove_candy(self, func):
53 if len(self.candies) == 0:
54 return None
55 def choose_candy(list_of_candies):
56 return func(list_of_candies)
57 return choose_candy
58
59
60
61class FluxCapacitor:
62 def __init__(self, participants):
63 self.participants = participants
64
65
66 def get_victim(self):
67
68 @staticmethod
69 def _get_hosts():
70 return [x for x in self.participants if isinstance(x, Host)]
71
72 @staticmethod
73 def _get_kids():
74 return [x for x in self.participants if isinstance(x, Kid)]
75
76 num_of_dead = 0
77 set_of_dead = set()
78 all_kids = _get_kids()
79 all_hosts = _get_hosts()
80 list_of_host_visited = []
81 list_of_kids_visited_all = []
82 stop_loop = True
83 while stop_loop :
84 kid_to_host = []
85 for kid in all_kids:
86 if kid not in list_of_kids_visited_all:
87 for host in all_hosts:
88 if (kid, host) not in list_of_host_visited:
89 dist_to_host = math.dist(kid.get_position(), host.get_position())
90 kid_to_host.append((kid, host, dist_to_host, host.get_position()))
91 kid_to_host_sorted = sorted(kid_to_host, key=lambda p: p[2])
92
93 #check for duplicates
94 kid_to_host_sorted_duplicates = sorted(kid_to_host_sorted, key=lambda p: p[3])
95 kid_to_host.append((kid, kid_to_host_sorted_duplicates[0][1], kid.get_initiative()))
96
97 list_of_host_visited.append((kid, kid_to_host_sorted_duplicates[0][1]))
98
99 #check if kid has visited all hosts
100 if (len(list_of_host_visited) == len(all_hosts)):
101 list_of_kids_visited_all.append(kid)
102
103
104 #check initiative
105
106 list_of_final_kid_to_host = sorted(kid_to_host, key=lambda p:p[2])
107 for item in list_of_final_kid_to_host:
108 candy = item[1].remove_candy(max)
109 item[0].add_candy(candy)
110 item[0].set_position(item[1].get_position())
111
112
113 for t in list_of_final_kid_to_host:
114 if t[0].is_critical():
115 num_of_dead += 1
116 set_of_dead.add(t[0])
117
118
119 if num_of_dead > 0:
120 return set_of_dead
121
122 if(len(list_of_kids_visited_all) == len(all_kids)):
123 return None
124
..E.EEE.....
======================================================================
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 95, in get_victim
kid_to_host.append((kid, kid_to_host_sorted_duplicates[0][1], kid.get_initiative()))
IndexError: list index out of range
======================================================================
ERROR: test_no_candies (test.FluxCapacitorTest)
Test with no candies.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
File "/tmp/test.py", line 106, in test_no_candies
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 94, in get_victim
kid_to_host_sorted_duplicates = sorted(kid_to_host_sorted, key=lambda p: p[3])
File "/tmp/solution.py", line 94, in <lambda>
kid_to_host_sorted_duplicates = sorted(kid_to_host_sorted, key=lambda p: p[3])
IndexError: tuple index out of range
======================================================================
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})
File "/tmp/solution.py", line 94, in get_victim
kid_to_host_sorted_duplicates = sorted(kid_to_host_sorted, key=lambda p: p[3])
File "/tmp/solution.py", line 94, in <lambda>
kid_to_host_sorted_duplicates = sorted(kid_to_host_sorted, key=lambda p: p[3])
IndexError: tuple index out of range
======================================================================
ERROR: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 66, in test_basic_usage
self.assertEqual(candy.get_mass(), 456)
AttributeError: 'function' object has no attribute 'get_mass'
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (errors=4)
Виктор Бечев
08.11.2023 13:01Останалите грешки са все неща, които Жорката спомена на лекцията вчера, разгледай слайдовете и ще припознаеш някои от тях.
|
08.11.2023 13:00