Домашни > Хелоуин в Припят > Решения > Решението на Пресиан Андрейчев

Резултати
5 точки от тестове
0 точки от учител

5 точки общо

6 успешни теста
6 неуспешни теста
Код (Функцията get_victim не е вярна :( ...)
Скрий всички коментари

 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

Останалите грешки са все неща, които Жорката спомена на лекцията вчера, разгледай слайдовете и ще припознаеш някои от тях.
История

t1import matht1import math
22
3class Candy:3class Candy:
4    def __init__(self, mass, uranium):4    def __init__(self, mass, uranium):
5        self.mass = mass5        self.mass = mass
6        self.uranium = uranium6        self.uranium = uranium
77
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()
1010
11    def get_mass(self):11    def get_mass(self):
12        return self.mass12        return self.mass
13         13         
1414
15class Person:15class 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 = []
1919
20    def get_position(self):20    def get_position(self):
21        return self.position21        return self.position
22    22    
23    def set_position(self, position):23    def set_position(self, position):
24        self.position = position24        self.position = position
2525
2626
27class Kid(Person):27class 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 = initiative30        self.initiative = initiative
31        self.visited_host = []31        self.visited_host = []
3232
33    def get_initiative(self):33    def get_initiative(self):
34        return self.initiative34        return self.initiative
35    35    
36    def get_candies(self):36    def get_candies(self):
37        return self._basket37        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 > 2046        return sum > 20
4747
4848
49class Host(Person):49class 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))
5454
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))
5757
5858
59class FluxCapacitor:59class 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                    break78                    break
79                max_distance = 100000079                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 = hst86                        last_host = hst
87                        max_distance = new_dist87                        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 poisoned97        return poisoned
9898
9999
100100
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op