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

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

8 точки общо

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

  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.mass * self.uranium
  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, new_position):
 21        self.position = new_position
 22
 23    def get_distance(self, other_person):
 24        x1, y1 = self.position
 25        x2, y2 = other_person.position
 26        distance = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
 27        return distance
 28
 29
 30class Kid(Person):
 31    def __init__(self, position, initiative):
 32        super().__init__(position)
 33        self.initiative = initiative
 34        self.candy_basket = []
 35        self.visited_hosts = set()
 36    
 37    def get_initiative(self):
 38        return self.initiative
 39    
 40    def add_candy(self, candy):
 41        self.candy_basket.append(candy)
 42    
 43    def is_critical(self):
 44        uranium_quantity = sum(candy.get_uranium_quantity() for candy in self.candy_basket)
 45        return uranium_quantity > 20
 46    
 47    def get_closest_host(self, hosts):
 48        closest_host = None
 49        min_distance = float('inf')
 50
 51        for host in hosts:
 52            if host not in self.visited_hosts:
 53                distance = self.get_distance(host)
 54
 55                if distance < min_distance or (distance == min_distance and (host.position < closest_host.position)):
 56                    closest_host = host
 57                    min_distance = distance
 58        
 59        if closest_host is not None:
 60            self.visited_hosts.add(closest_host)
 61
 62        return closest_host
 63    
 64
 65class Host(Person):
 66    def __init__(self, position, candies):
 67        super().__init__(position)
 68        self.candy_basket = [Candy(mass, uranium) for mass, uranium in candies]
 69        self.kids = []
 70    
 71    def remove_candy(self, func):
 72        if not self.candy_basket:
 73            return None
 74        
 75        candy_to_give = func(self.candy_basket)
 76        self.candy_basket.remove(candy_to_give)
 77        return candy_to_give
 78    
 79    def give_candies(self, kids):
 80        kids = sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True)
 81        for kid in kids:
 82            candy = self.remove_candy(lambda candies: max(candies, key=lambda candy: candy.get_mass()))
 83            if not candy:
 84                break
 85            kid.add_candy(candy)
 86
 87
 88class FluxCapacitor:
 89    def __init__(self, participants):
 90        self.participants = participants
 91
 92    def get_victim(self):
 93        victims = set()
 94
 95        hosts=[]
 96        kids=[]
 97
 98        for person in self.participants:
 99            if isinstance(person, Host):
100                hosts.append(person)
101            else:
102                kids.append(person)
103
104        should_continue = True
105        while should_continue:
106            for kid in kids:
107                if kid not in victims:
108                    closest_host = kid.get_closest_host(hosts)
109                    if closest_host is None:
110                        should_continue = False
111                        break
112                    kid.set_position(closest_host.get_position())
113                    closest_host.kids.append(kid)
114
115            for host in hosts:
116                host.give_candies(host.kids)
117
118            for host in hosts:
119                for kid in host.kids:
120                    if kid.is_critical():
121                            victims.add(kid)
122                host.kids = []
123
124            if victims:
125                break
126
127        return victims or None

.F.F........
======================================================================
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)
File "/tmp/solution.py", line 124, in get_victim
if victims:
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_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)
File "/tmp/solution.py", line 116, in get_victim
host.give_candies(host.kids)
File "/tmp/solution.py", line 79, in give_candies
def give_candies(self, kids):
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'

----------------------------------------------------------------------
Ran 12 tests in 0.401s

FAILED (failures=2)

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