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.uranium * self.get_mass()
10
11 def get_mass(self):
12 return self.mass
13
14
15class Person:
16 def __init__(self, position):
17 self.set_position(position)
18 self._basket = []
19
20 def get_position(self):
21 return self.position
22
23 def set_position(self, position):
24 self.position = position
25
26
27class Kid(Person):
28 def __init__(self, position, initiative):
29 super().__init__(position)
30 self.initiative = initiative
31 self.visited_host = []
32
33 def get_initiative(self):
34 return self.initiative
35
36 def get_candies(self):
37 return self._basket
38
39 def add_candy(self, candy):
40 self._basket.append(candy)
41
42 def is_critical(self):
43 sum = 0
44 for candies in self._basket:
45 sum += candies.get_uranium_quantity()
46 return sum > 20
47
48
49class Host(Person):
50 def __init__(self, position, candies):
51 super().__init__(self,position)
52 for make_candy, uran in candies:
53 self._basket.append(Candy(make_candy, uran))
54
55 def remove_candy(self, funct):
56 return self._basket.pop(funct(self._basket))
57
58
59class FluxCapacitor:
60 def __init__(self, participants):
61 self.__kids = []
62 self.__hosts = []
63 for participant in participant:
64 if isinstance(participant, Kid):
65 self.__kids.append(participant)
66 else:
67 self.__host.append(participant)
68# Pretty sure that this is not a correct function.
69 def get_victim(self):
70 poisoned = []
71 kid_all = set()
72 visited = set()
73 while len(self.__kids) > 0:
74 for kid in self.__kids.copy():
75 if kid.visited_host.__len__ == self.__hosts.__len__:
76 kid_all.add(kid)
77 self.__kids.pop(kid)
78 break
79 max_distance = 1000000
80 for hst in self.__hosts:
81 kid_cord = kid.get_position()
82 hst_cord = hst.get_position()
83 new_dist = math.sqrt(((hst_cord[0] - kid_cord[0]) ** 2)+
84 ((hst_cord[1] - kid_cord[1]) ** 2))
85 if new_dist < max_distance:
86 last_host = hst
87 max_distance = new_dist
88
89 visited.add(last_host)
90 kid.setposition(hst_cord)
91 candy = last_host.remove_candy()
92 kid.addcandy(candy)
93
94 for kid in self.__kids:
95 if isinstance(kid, Kid) and not kid.is_critical():
96 poisoned.append(kid)
97 return poisoned
.EEEEEE.....
======================================================================
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 79, in test_empty
flux_capacitor = FluxCapacitor(set())
File "/tmp/solution.py", line 63, in __init__
for participant in participant:
UnboundLocalError: local variable 'participant' referenced before assignment
======================================================================
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 87, in test_empty_hosts
flux_capacitor = FluxCapacitor({kid1, kid2})
File "/tmp/solution.py", line 63, in __init__
for participant in participant:
UnboundLocalError: local variable 'participant' referenced before assignment
======================================================================
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 93, in test_empty_kids
host1 = Host((1, 3), [(1, 0.1), (3, 0.3)])
File "/tmp/solution.py", line 51, in __init__
super().__init__(self,position)
TypeError: Person.__init__() takes 2 positional arguments but 3 were given
======================================================================
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 103, in test_no_candies
host1 = Host((2, 1), [])
File "/tmp/solution.py", line 51, in __init__
super().__init__(self,position)
TypeError: Person.__init__() takes 2 positional arguments but 3 were given
======================================================================
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 113, in test_real_case
host1 = Host((101, 101), [(21, 1.0)])
File "/tmp/solution.py", line 51, in __init__
super().__init__(self,position)
TypeError: Person.__init__() takes 2 positional arguments but 3 were given
======================================================================
ERROR: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 64, in test_basic_usage
host = Host((0, 0), [(456, 1.0), (785, 0.5)])
File "/tmp/solution.py", line 51, in __init__
super().__init__(self,position)
TypeError: Person.__init__() takes 2 positional arguments but 3 were given
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (errors=6)
Виктор Бечев
08.11.2023 13:12Останалите грешки са все неща, които Жорката спомена на лекцията вчера, разгледай слайдовете и ще припознаеш някои от тях.
|
t | 1 | import math | t | 1 | import math |
2 | 2 | ||||
3 | class Candy: | 3 | class Candy: | ||
4 | def __init__(self, mass, uranium): | 4 | def __init__(self, mass, uranium): | ||
5 | self.mass = mass | 5 | self.mass = mass | ||
6 | self.uranium = uranium | 6 | self.uranium = uranium | ||
7 | 7 | ||||
8 | def get_uranium_quantity(self): | 8 | def get_uranium_quantity(self): | ||
9 | return self.uranium * self.get_mass() | 9 | return self.uranium * self.get_mass() | ||
10 | 10 | ||||
11 | def get_mass(self): | 11 | def get_mass(self): | ||
12 | return self.mass | 12 | return self.mass | ||
13 | 13 | ||||
14 | 14 | ||||
15 | class Person: | 15 | class Person: | ||
16 | def __init__(self, position): | 16 | def __init__(self, position): | ||
17 | self.set_position(position) | 17 | self.set_position(position) | ||
18 | self._basket = [] | 18 | self._basket = [] | ||
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, position): | 23 | def set_position(self, position): | ||
24 | self.position = position | 24 | self.position = position | ||
25 | 25 | ||||
26 | 26 | ||||
27 | class Kid(Person): | 27 | class Kid(Person): | ||
28 | def __init__(self, position, initiative): | 28 | def __init__(self, position, initiative): | ||
29 | super().__init__(position) | 29 | super().__init__(position) | ||
30 | self.initiative = initiative | 30 | self.initiative = initiative | ||
31 | self.visited_host = [] | 31 | self.visited_host = [] | ||
32 | 32 | ||||
33 | def get_initiative(self): | 33 | def get_initiative(self): | ||
34 | return self.initiative | 34 | return self.initiative | ||
35 | 35 | ||||
36 | def get_candies(self): | 36 | def get_candies(self): | ||
37 | return self._basket | 37 | return self._basket | ||
38 | 38 | ||||
39 | def add_candy(self, candy): | 39 | def add_candy(self, candy): | ||
40 | self._basket.append(candy) | 40 | self._basket.append(candy) | ||
41 | 41 | ||||
42 | def is_critical(self): | 42 | def is_critical(self): | ||
43 | sum = 0 | 43 | sum = 0 | ||
44 | for candies in self._basket: | 44 | for candies in self._basket: | ||
45 | sum += candies.get_uranium_quantity() | 45 | sum += candies.get_uranium_quantity() | ||
46 | return sum > 20 | 46 | return sum > 20 | ||
47 | 47 | ||||
48 | 48 | ||||
49 | class Host(Person): | 49 | class Host(Person): | ||
50 | def __init__(self, position, candies): | 50 | def __init__(self, position, candies): | ||
51 | super().__init__(self,position) | 51 | super().__init__(self,position) | ||
52 | for make_candy, uran in candies: | 52 | for make_candy, uran in candies: | ||
53 | self._basket.append(Candy(make_candy, uran)) | 53 | self._basket.append(Candy(make_candy, uran)) | ||
54 | 54 | ||||
55 | def remove_candy(self, funct): | 55 | def remove_candy(self, funct): | ||
56 | return self._basket.pop(funct(self._basket)) | 56 | return self._basket.pop(funct(self._basket)) | ||
57 | 57 | ||||
58 | 58 | ||||
59 | class FluxCapacitor: | 59 | class FluxCapacitor: | ||
60 | def __init__(self, participants): | 60 | def __init__(self, participants): | ||
61 | self.__kids = [] | 61 | self.__kids = [] | ||
62 | self.__hosts = [] | 62 | self.__hosts = [] | ||
63 | for participant in participant: | 63 | for participant in participant: | ||
64 | if isinstance(participant, Kid): | 64 | if isinstance(participant, Kid): | ||
65 | self.__kids.append(participant) | 65 | self.__kids.append(participant) | ||
66 | else: | 66 | else: | ||
67 | self.__host.append(participant) | 67 | self.__host.append(participant) | ||
68 | # Pretty sure that this is not a correct function. | 68 | # Pretty sure that this is not a correct function. | ||
69 | def get_victim(self): | 69 | def get_victim(self): | ||
70 | poisoned = [] | 70 | poisoned = [] | ||
71 | kid_all = set() | 71 | kid_all = set() | ||
72 | visited = set() | 72 | visited = set() | ||
73 | while len(self.__kids) > 0: | 73 | while len(self.__kids) > 0: | ||
74 | for kid in self.__kids.copy(): | 74 | for kid in self.__kids.copy(): | ||
75 | if kid.visited_host.__len__ == self.__hosts.__len__: | 75 | if kid.visited_host.__len__ == self.__hosts.__len__: | ||
76 | kid_all.add(kid) | 76 | kid_all.add(kid) | ||
77 | self.__kids.pop(kid) | 77 | self.__kids.pop(kid) | ||
78 | break | 78 | break | ||
79 | max_distance = 1000000 | 79 | max_distance = 1000000 | ||
80 | for hst in self.__hosts: | 80 | for hst in self.__hosts: | ||
81 | kid_cord = kid.get_position() | 81 | kid_cord = kid.get_position() | ||
82 | hst_cord = hst.get_position() | 82 | hst_cord = hst.get_position() | ||
83 | new_dist = math.sqrt(((hst_cord[0] - kid_cord[0]) ** 2)+ | 83 | new_dist = math.sqrt(((hst_cord[0] - kid_cord[0]) ** 2)+ | ||
84 | ((hst_cord[1] - kid_cord[1]) ** 2)) | 84 | ((hst_cord[1] - kid_cord[1]) ** 2)) | ||
85 | if new_dist < max_distance: | 85 | if new_dist < max_distance: | ||
86 | last_host = hst | 86 | last_host = hst | ||
87 | max_distance = new_dist | 87 | max_distance = new_dist | ||
88 | 88 | ||||
89 | visited.add(last_host) | 89 | visited.add(last_host) | ||
90 | kid.setposition(hst_cord) | 90 | kid.setposition(hst_cord) | ||
91 | candy = last_host.remove_candy() | 91 | candy = last_host.remove_candy() | ||
92 | kid.addcandy(candy) | 92 | kid.addcandy(candy) | ||
93 | 93 | ||||
94 | for kid in self.__kids: | 94 | for kid in self.__kids: | ||
95 | if isinstance(kid, Kid) and not kid.is_critical(): | 95 | if isinstance(kid, Kid) and not kid.is_critical(): | ||
96 | poisoned.append(kid) | 96 | poisoned.append(kid) | ||
97 | return poisoned | 97 | return poisoned | ||
98 | 98 | ||||
99 | 99 | ||||
100 | 100 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
08.11.2023 13:11