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

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

8 точки общо

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

 1class Candy:
 2    def __init__(self, mass, uranium):
 3        self.mass = mass
 4        self.uranium = uranium
 5
 6    def get_uranium_quantity(self):
 7        return self.uranium * self.mass
 8
 9    def get_mass(self):
10        return self.mass
11
12
13class Person:
14    def __init__(self, position):
15        self.position = position
16
17    def get_position(self):
18        return self.position
19
20    def set_position(self, position):
21        self.position = position
22
23
24class Kid(Person):
25    def __init__(self, position, initiative):
26        super().__init__(position)
27        self.initiative = initiative
28        self.candies = []
29        self.visited_hosts = set()
30
31    def get_initiative(self):
32        return self.initiative
33
34    def add_candy(self, candy):
35        self.candies.append(candy)
36
37    def is_critical(self):
38        uranium_total = sum(candy.get_uranium_quantity() for candy in self.candies)
39        return uranium_total > 20
40
41    def calculate_distance(self, other_person):
42        x1, y1 = self.position
43        x2, y2 = other_person.position
44        return ((x1 - x2) ** 2 + (y1 - y2) ** 2)
45
46    def get_next_host(self, hosts):
47        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
48        if unvisited_hosts:
49            return min(unvisited_hosts, key=lambda host: (self.calculate_distance(host), host.position))
50        return None 
51
52
53class Host(Person):
54    def __init__(self, position, candies):
55        super().__init__(position)
56        self.candies = [Candy(mass, uranium) for mass, uranium in candies]
57
58    def remove_candy(self):
59        if not self.candies:
60            return None
61
62        max_candy = max(self.candies, key=lambda candy: candy.get_mass())
63        self.candies.remove(max_candy)
64        return max_candy
65
66
67class FluxCapacitor:
68    def __init__(self, participants):
69        self.participants = participants
70        self.kids = [participant for participant in participants if isinstance(participant, Kid)]
71        self.hosts = [participant for participant in participants if isinstance(participant, Host)]
72
73    def get_victim(self):
74        while self.kids:
75            for kid in self.kids:
76                host = kid.get_next_host(self.hosts)
77                if host and (candy := host.remove_candy()):
78                    kid.add_candy(candy)
79                    kid.visited_hosts.add(host)
80                    if kid.is_critical():
81                        return {kid}
82            if all(map(lambda kid: len(kid.visited_hosts) == len(self.hosts), self.kids)):
83                return None

....FFE.....
======================================================================
ERROR: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 65, in test_basic_usage
candy = host.remove_candy(compare_fun)
TypeError: Host.remove_candy() takes 1 positional argument but 2 were given

======================================================================
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)
File "/tmp/solution.py", line 76, in get_victim
host = kid.get_next_host(self.hosts)
File "/tmp/solution.py", line 49, in get_next_host
return min(unvisited_hosts, key=lambda host: (self.calculate_distance(host), host.position))
File "/tmp/solution.py", line 49, in <lambda>
return min(unvisited_hosts, key=lambda host: (self.calculate_distance(host), host.position))
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 69, in handler
_raise_exception(timeout_exception, exception_message)
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 45, in _raise_exception
raise exception()
timeout_decorator.timeout_decorator.TimeoutError: 'Timed Out'

======================================================================
FAIL: 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})
AssertionError: Items in the second set but not the first:
<solution.Kid object at 0x7f3782029420>

----------------------------------------------------------------------
Ran 12 tests in 0.201s

FAILED (failures=2, errors=1)

Дискусия
История
Това решение има само една версия.