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

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

9 точки общо

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

  1import math
  2import sys
  3
  4URAN_LIMIT = 20
  5
  6class Candy:
  7    def __init__(self, mass, uranium):
  8        self.mass = mass
  9        self.uranium = uranium
 10
 11    def get_uranium_quantity(self):
 12        uranium_quantity = self.mass * self.uranium
 13        return uranium_quantity
 14
 15    def get_mass(self):
 16        return self.mass
 17
 18
 19class Person:
 20    def __init__(self, position=tuple):
 21        self.position = position
 22
 23    def get_position(self):
 24        return self.position
 25
 26    def set_position(self, new_position):
 27        self.position = new_position
 28
 29
 30class Kid(Person):
 31    def __init__(self, position, initiative):
 32        super().__init__(position)
 33        self.candies = list()
 34        self.initiative = initiative
 35        self.visited_hosts = set()
 36
 37    def get_initiative(self):
 38        return self.initiative
 39
 40    def add_candy(self, candy=Candy):
 41        if(candy):
 42            self.candies.append(candy)
 43
 44    def is_critical(self):
 45        total_uranium_in_basket = 0
 46        for candy in self.candies:
 47            total_uranium_in_basket += candy.get_uranium_quantity()
 48
 49        if total_uranium_in_basket > URAN_LIMIT:
 50            return 1
 51
 52        else:
 53            return 0
 54
 55    def visit_host(self, host):
 56        self.visited_hosts.add(host)
 57
 58
 59class Host(Person):
 60    def __init__(self, position, candies):
 61        super().__init__(position)
 62        self.candies = [Candy(mass, uranium) for mass, uranium in candies]
 63
 64    def remove_candy(self, func):
 65        if len(self.candies) == 0:
 66            return None
 67        chosen_candy = func(self.candies)
 68        self.candies.remove(chosen_candy)
 69        return chosen_candy
 70
 71def pick_candy(list_of_candies):
 72    return max(list_of_candies, key=lambda x: x.get_mass())
 73
 74
 75def calculate_distance(x1, y1, x2, y2):
 76        return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
 77
 78
 79class FluxCapacitor:
 80    def __init__(self, participants):
 81        self.kids = set()
 82        self.hosts = set()
 83        for participant in participants:
 84            if isinstance(participant, Kid):
 85                self.kids.add(participant)
 86            if isinstance(participant, Host):
 87                self.hosts.add(participant)
 88
 89
 90    def find_nearest_host(self,kid):
 91        min_distance = sys.float_info.max
 92        current_host = None
 93        for host in self.hosts:
 94            if(host not in kid.visited_hosts):
 95                current_distance = calculate_distance(kid.position[0], kid.position[1], host.position[0], host.position[1])
 96            if current_distance < min_distance or (current_distance==min_distance and (host.get_position() < current_host.get_position())):
 97                min_distance = current_distance
 98                current_host = host
 99        return current_host
100
101    def all_have_been_visited(self):
102        for kid in self.kids:
103            if kid.visited_hosts != self.hosts:
104                return False
105        return True
106
107    def get_victim(self):
108        dead_kids = set()
109        sorted_kids = sorted((kid for kid in self.kids), key=lambda x: x.get_initiative(), reverse=True)
110
111        while not self.all_have_been_visited():
112            for kid in sorted_kids:
113                nearest_host = self.find_nearest_host(kid)
114                if nearest_host:
115                    candy = nearest_host.remove_candy(pick_candy)
116                    self.kids.remove(kid)
117                    kid.add_candy(candy)
118                    kid.set_position(nearest_host.get_position)
119                    kid.visit_host(nearest_host)
120                    self.kids.add(kid)
121                if kid.is_critical():
122                        dead_kids.add(kid)
123                        self.kids.remove(kid)
124
125            if dead_kids:
126                return dead_kids
127        return None

....E.......
======================================================================
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 106, in test_no_candies
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 113, in get_victim
nearest_host = self.find_nearest_host(kid)
File "/tmp/solution.py", line 96, in find_nearest_host
if current_distance < min_distance or (current_distance==min_distance and (host.get_position() < current_host.get_position())):
UnboundLocalError: local variable 'current_distance' referenced before assignment

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

FAILED (errors=1)

Дискусия
Виктор Бечев
08.11.2023 12:59

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