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

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

6 точки общо

7 успешни теста
5 неуспешни теста
Код
Скрий всички коментари

  1import math
  2
  3class Candy:
  4
  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
 18    def __init__(self, position):
 19        self.position = position
 20
 21    def get_position(self):
 22        return self.position
 23    
 24    def set_position(self, new_position):
 25        self.position = new_position
 26
 27
 28class Kid(Person):
 29
 30    CRITICAL_URANIUM_QUANTITY_PER_KID = 20
 31
 32    def __init__(self, position, initiative):
 33        super().__init__(position)
 34        self.initiative = initiative
 35        self.uranium_quantity_in_basket = 0
 36
 37    def get_initiative(self):
 38        return self.initiative
 39    
 40    def add_candy(self, candy):
 41        self.uranium_quantity_in_basket += candy.get_uranium_quantity()
 42
 43    def is_critical(self):
 44        return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID
 45    
 46
 47class Host(Person):
 48
 49    def __init__(self, position, candies):
 50        super().__init__(position)
 51        real_candies = []
 52        for candy in candies:
 53            real_candies.append(Candy(candy[0], candy[1]))
 54        self.candies = sorted(real_candies, key=lambda candy: candy.mass, reverse=True)
 55        self.guests = []
 56
 57    def remove_candy(self, candy_to_be_removed):
 58        if self.candies:
 59            exact_candy_to_be_removed = candy_to_be_removed(self.candies)
 60            return exact_candy_to_be_removed
 61        
 62        return None
 63    
 64    def add_guest(self, guest):
 65        self.guests.append(guest)
 66        
 67    def treat(self):
 68        self.guests.sort(key=lambda kid: kid.initiative)
 69        kids_with_critical_condition = set()
 70
 71        for (kid, candy) in zip(self.guests, self.candies):
 72            kid.add_candy(candy)
 73            self.candies.remove(candy)
 74            kid.set_position(self.get_position())
 75
 76            if kid.is_critical():
 77                kids_with_critical_condition.add(kid)
 78
 79        self.guests.clear()
 80        return kids_with_critical_condition
 81
 82class FluxCapacitor:
 83
 84    def __init__(self, participants):
 85        self.hosts = []
 86        self.kids = []
 87
 88        for participant in participants:
 89            if type(participant) == Host:
 90                self.hosts.append(participant)
 91            else:
 92                self.kids.append(participant)
 93
 94    def arrange_guests(self):
 95        for kid in self.kids:
 96            lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position())
 97            closest_host = self.hosts[0]
 98
 99            for host in self.hosts:
100                current_distance = math.dist(kid.get_position(), host.get_position())
101
102                if current_distance < lowest_distance:
103                    lowest_distance = current_distance
104                    closest_host = host
105                elif current_distance == lowest_distance:
106                    if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]:
107                        lowest_distance = current_distance
108                        closest_host = host
109
110            closest_host.add_guest(kid)
111
112    def get_victim(self):
113        self.arrange_guests()
114        final_set_critical_condition = set()
115        
116        for host in self.hosts:
117            final_set_critical_condition.update(host.treat())
118
119        return final_set_critical_condition

.FEFF.F.....
======================================================================
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 113, in get_victim
self.arrange_guests()
File "/tmp/solution.py", line 96, in arrange_guests
lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position())
IndexError: list index out of range

======================================================================
FAIL: 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)
AssertionError: set() != None

======================================================================
FAIL: 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)
AssertionError: set() != None

======================================================================
FAIL: 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)
AssertionError: set() != None

======================================================================
FAIL: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 68, in test_basic_usage
self.assertEqual(candy.get_mass(), 785)
AssertionError: 456 != 785

----------------------------------------------------------------------
Ran 12 tests in 0.001s

FAILED (failures=4, errors=1)

Дискусия
История

f1import mathf1import math
22
3class Candy:3class Candy:
44
5    def __init__(self, mass, uranium):5    def __init__(self, mass, uranium):
6        self.mass = mass6        self.mass = mass
7        self.uranium = uranium7        self.uranium = uranium
88
9    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
10        return self.mass * self.uranium10        return self.mass * self.uranium
11    11    
12    def get_mass(self):12    def get_mass(self):
13        return self.mass13        return self.mass
14    14    
1515
16class Person:16class Person:
1717
18    def __init__(self, position):18    def __init__(self, position):
19        self.position = position19        self.position = position
2020
21    def get_position(self):21    def get_position(self):
22        return self.position22        return self.position
23    23    
24    def set_position(self, new_position):24    def set_position(self, new_position):
25        self.position = new_position25        self.position = new_position
2626
2727
28class Kid(Person):28class Kid(Person):
2929
30    CRITICAL_URANIUM_QUANTITY_PER_KID = 2030    CRITICAL_URANIUM_QUANTITY_PER_KID = 20
3131
32    def __init__(self, position, initiative):32    def __init__(self, position, initiative):
33        super().__init__(position)33        super().__init__(position)
34        self.initiative = initiative34        self.initiative = initiative
35        self.uranium_quantity_in_basket = 035        self.uranium_quantity_in_basket = 0
3636
37    def get_initiative(self):37    def get_initiative(self):
38        return self.initiative38        return self.initiative
39    39    
40    def add_candy(self, candy):40    def add_candy(self, candy):
41        self.uranium_quantity_in_basket += candy.get_uranium_quantity()41        self.uranium_quantity_in_basket += candy.get_uranium_quantity()
4242
43    def is_critical(self):43    def is_critical(self):
44        return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID44        return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID
45    45    
4646
47class Host(Person):47class Host(Person):
4848
49    def __init__(self, position, candies):49    def __init__(self, position, candies):
50        super().__init__(position)50        super().__init__(position)
51        real_candies = []51        real_candies = []
52        for candy in candies:52        for candy in candies:
53            real_candies.append(Candy(candy[0], candy[1]))53            real_candies.append(Candy(candy[0], candy[1]))
n54        self.candies = sorted(real_candies, key=lambda candy: candy.mass)n54        self.candies = sorted(real_candies, key=lambda candy: candy.mass, reverse=True)
55        self.guests = []55        self.guests = []
5656
57    def remove_candy(self, candy_to_be_removed):57    def remove_candy(self, candy_to_be_removed):
58        if self.candies:58        if self.candies:
59            exact_candy_to_be_removed = candy_to_be_removed(self.candies)59            exact_candy_to_be_removed = candy_to_be_removed(self.candies)
60            return exact_candy_to_be_removed60            return exact_candy_to_be_removed
61        61        
62        return None62        return None
63    63    
64    def add_guest(self, guest):64    def add_guest(self, guest):
65        self.guests.append(guest)65        self.guests.append(guest)
66        66        
67    def treat(self):67    def treat(self):
68        self.guests.sort(key=lambda kid: kid.initiative)68        self.guests.sort(key=lambda kid: kid.initiative)
n69        kids_with_critical_condition = {}n69        kids_with_critical_condition = set()
7070
71        for (kid, candy) in zip(self.guests, self.candies):71        for (kid, candy) in zip(self.guests, self.candies):
72            kid.add_candy(candy)72            kid.add_candy(candy)
73            self.candies.remove(candy)73            self.candies.remove(candy)
74            kid.set_position(self.get_position())74            kid.set_position(self.get_position())
7575
76            if kid.is_critical():76            if kid.is_critical():
n77                kids_with_critical_condition.append(kid)n77                kids_with_critical_condition.add(kid)
7878
79        self.guests.clear()79        self.guests.clear()
80        return kids_with_critical_condition80        return kids_with_critical_condition
8181
82class FluxCapacitor:82class FluxCapacitor:
8383
84    def __init__(self, participants):84    def __init__(self, participants):
85        self.hosts = []85        self.hosts = []
86        self.kids = []86        self.kids = []
8787
88        for participant in participants:88        for participant in participants:
89            if type(participant) == Host:89            if type(participant) == Host:
90                self.hosts.append(participant)90                self.hosts.append(participant)
91            else:91            else:
92                self.kids.append(participant)92                self.kids.append(participant)
9393
94    def arrange_guests(self):94    def arrange_guests(self):
95        for kid in self.kids:95        for kid in self.kids:
96            lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position())96            lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position())
97            closest_host = self.hosts[0]97            closest_host = self.hosts[0]
9898
99            for host in self.hosts:99            for host in self.hosts:
100                current_distance = math.dist(kid.get_position(), host.get_position())100                current_distance = math.dist(kid.get_position(), host.get_position())
101101
102                if current_distance < lowest_distance:102                if current_distance < lowest_distance:
103                    lowest_distance = current_distance103                    lowest_distance = current_distance
104                    closest_host = host104                    closest_host = host
105                elif current_distance == lowest_distance:105                elif current_distance == lowest_distance:
106                    if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]:106                    if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]:
107                        lowest_distance = current_distance107                        lowest_distance = current_distance
108                        closest_host = host108                        closest_host = host
109109
110            closest_host.add_guest(kid)110            closest_host.add_guest(kid)
111111
112    def get_victim(self):112    def get_victim(self):
113        self.arrange_guests()113        self.arrange_guests()
t114        final_set_critical_condition = {}t114        final_set_critical_condition = set()
115        115        
116        for host in self.hosts:116        for host in self.hosts:
117            final_set_critical_condition.update(host.treat())117            final_set_critical_condition.update(host.treat())
118118
119        return final_set_critical_condition119        return final_set_critical_condition
120120
121121
122122
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
22
3class Candy:3class Candy:
44
5    def __init__(self, mass, uranium):5    def __init__(self, mass, uranium):
6        self.mass = mass6        self.mass = mass
7        self.uranium = uranium7        self.uranium = uranium
88
9    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
10        return self.mass * self.uranium10        return self.mass * self.uranium
11    11    
12    def get_mass(self):12    def get_mass(self):
13        return self.mass13        return self.mass
14    14    
1515
16class Person:16class Person:
1717
18    def __init__(self, position):18    def __init__(self, position):
19        self.position = position19        self.position = position
2020
21    def get_position(self):21    def get_position(self):
22        return self.position22        return self.position
23    23    
24    def set_position(self, new_position):24    def set_position(self, new_position):
25        self.position = new_position25        self.position = new_position
2626
2727
28class Kid(Person):28class Kid(Person):
2929
30    CRITICAL_URANIUM_QUANTITY_PER_KID = 2030    CRITICAL_URANIUM_QUANTITY_PER_KID = 20
3131
32    def __init__(self, position, initiative):32    def __init__(self, position, initiative):
33        super().__init__(position)33        super().__init__(position)
34        self.initiative = initiative34        self.initiative = initiative
35        self.uranium_quantity_in_basket = 035        self.uranium_quantity_in_basket = 0
3636
37    def get_initiative(self):37    def get_initiative(self):
38        return self.initiative38        return self.initiative
39    39    
40    def add_candy(self, candy):40    def add_candy(self, candy):
41        self.uranium_quantity_in_basket += candy.get_uranium_quantity()41        self.uranium_quantity_in_basket += candy.get_uranium_quantity()
4242
43    def is_critical(self):43    def is_critical(self):
44        return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID44        return self.uranium_quantity_in_basket > Kid.CRITICAL_URANIUM_QUANTITY_PER_KID
45    45    
4646
47class Host(Person):47class Host(Person):
4848
49    def __init__(self, position, candies):49    def __init__(self, position, candies):
50        super().__init__(position)50        super().__init__(position)
51        real_candies = []51        real_candies = []
52        for candy in candies:52        for candy in candies:
53            real_candies.append(Candy(candy[0], candy[1]))53            real_candies.append(Candy(candy[0], candy[1]))
54        self.candies = sorted(real_candies, key=lambda candy: candy.mass)54        self.candies = sorted(real_candies, key=lambda candy: candy.mass)
55        self.guests = []55        self.guests = []
5656
57    def remove_candy(self, candy_to_be_removed):57    def remove_candy(self, candy_to_be_removed):
58        if self.candies:58        if self.candies:
59            exact_candy_to_be_removed = candy_to_be_removed(self.candies)59            exact_candy_to_be_removed = candy_to_be_removed(self.candies)
60            return exact_candy_to_be_removed60            return exact_candy_to_be_removed
61        61        
62        return None62        return None
63    63    
64    def add_guest(self, guest):64    def add_guest(self, guest):
65        self.guests.append(guest)65        self.guests.append(guest)
66        66        
67    def treat(self):67    def treat(self):
68        self.guests.sort(key=lambda kid: kid.initiative)68        self.guests.sort(key=lambda kid: kid.initiative)
69        kids_with_critical_condition = {}69        kids_with_critical_condition = {}
7070
n71        for kid, candy in self.guests, self.candies:n71        for (kid, candy) in zip(self.guests, self.candies):
72            kid.add_candy(candy)72            kid.add_candy(candy)
tt73            self.candies.remove(candy)
73            kid.set_position(self.get_position())74            kid.set_position(self.get_position())
7475
75            if kid.is_critical():76            if kid.is_critical():
76                kids_with_critical_condition.append(kid)77                kids_with_critical_condition.append(kid)
7778
78        self.guests.clear()79        self.guests.clear()
79        return kids_with_critical_condition80        return kids_with_critical_condition
8081
81class FluxCapacitor:82class FluxCapacitor:
8283
83    def __init__(self, participants):84    def __init__(self, participants):
84        self.hosts = []85        self.hosts = []
85        self.kids = []86        self.kids = []
8687
87        for participant in participants:88        for participant in participants:
88            if type(participant) == Host:89            if type(participant) == Host:
89                self.hosts.append(participant)90                self.hosts.append(participant)
90            else:91            else:
91                self.kids.append(participant)92                self.kids.append(participant)
9293
93    def arrange_guests(self):94    def arrange_guests(self):
94        for kid in self.kids:95        for kid in self.kids:
95            lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position())96            lowest_distance = math.dist(kid.get_position(), self.hosts[0].get_position())
96            closest_host = self.hosts[0]97            closest_host = self.hosts[0]
9798
98            for host in self.hosts:99            for host in self.hosts:
99                current_distance = math.dist(kid.get_position(), host.get_position())100                current_distance = math.dist(kid.get_position(), host.get_position())
100101
101                if current_distance < lowest_distance:102                if current_distance < lowest_distance:
102                    lowest_distance = current_distance103                    lowest_distance = current_distance
103                    closest_host = host104                    closest_host = host
104                elif current_distance == lowest_distance:105                elif current_distance == lowest_distance:
105                    if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]:106                    if host.get_position()[0] < closest_host.get_position()[0] or host.get_position()[1] < closest_host.get_position()[0]:
106                        lowest_distance = current_distance107                        lowest_distance = current_distance
107                        closest_host = host108                        closest_host = host
108109
109            closest_host.add_guest(kid)110            closest_host.add_guest(kid)
110111
111    def get_victim(self):112    def get_victim(self):
112        self.arrange_guests()113        self.arrange_guests()
113        final_set_critical_condition = {}114        final_set_critical_condition = {}
114        115        
115        for host in self.hosts:116        for host in self.hosts:
116            final_set_critical_condition.update(host.treat())117            final_set_critical_condition.update(host.treat())
117118
118        return final_set_critical_condition119        return final_set_critical_condition
119120
120121
121122
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op