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

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

2 точки общо

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

  1import math
  2
  3class Candy:
  4    
  5    def __init__(self, mass, uranium):
  6        self.mass = mass
  7        self.uranium = uranium
  8
  9    @property
 10    def get_uranium_quantity(self):
 11        return self.mass * self.uranium
 12
 13    @property
 14    def get_mass(self):
 15        return self.mass
 16
 17    def __lt__ (self, other):
 18        return self.mass < other.mass
 19
 20    def __gt__ (self, other):
 21        return self.mass > other.mass
 22
 23
 24class Person:
 25    
 26    def __init__(self, position):
 27        self.position = position
 28
 29    @property
 30    def get_position(self):
 31        return self.position
 32
 33    def distance(self, other):
 34        return math.sqrt(pow((other.position[0] - self.position[0]), 2) +
 35                         pow((other.position[1] - self.position[1]), 2))
 36
 37    def __eq__(self, other):
 38        return self.position == other.position
 39
 40    def __hash__(self):
 41        return hash(self.position)
 42
 43    def set_position(self, new_pos):
 44        self.position = new_pos
 45
 46
 47class Kid(Person):
 48    
 49    def __init__(self, position, initiative):
 50        super().__init__(position)
 51        self.initiative = initiative
 52        self.basket = []
 53        self.visited_hosts = []
 54
 55    @property
 56    def get_initiative(self):
 57        return self.initiative
 58
 59    
 60    def add_candy(self, candy):
 61        self.basket.append(candy)
 62
 63    @property
 64    def get_visited_hosts(self):
 65        return self.visited_hosts
 66
 67    def set_visited_hosts(self, new_visited_hosts):
 68        self.visited_hosts = new_visited_hosts
 69
 70    def is_critical(self):
 71        uranium_grams = 0
 72        for item in self.basket:
 73            uranium_grams += self.basket[item].get_uranium_quantity()
 74        if uranium_grams > 20:
 75            return True
 76        else:
 77            return False
 78
 79    def __lt__ (self, other):
 80        return self.initiative < other.initiative
 81
 82    def __gt__ (self, other):
 83        return self.initiative > other.initiative
 84
 85
 86
 87class Host(Person):
 88    
 89    def __init__(self, position, candies):
 90        super().__init__(position)
 91        self.candies = candies
 92
 93    
 94    def remove_candy(self, f):
 95        list_of_candies = []
 96        for item, candy in enumerate(self.candies):
 97            list_of_candies.append(Candy(candy[0], candy[1]))
 98        if list_of_candies:
 99            self.candies.sort(key=lambda x: x[0])
100            self.candies.pop()
101            return f(list_of_candies)
102
103
104class FluxCapacitor:
105    def __init__(self, participants):
106        self.participants = participants
107
108    
109    def get_victim(self):
110        host_list = []
111        kid_list = []
112        victims = []
113        for person in self.participants:
114            if type(person) == Host:
115                host_list.append(person)
116            else:
117                kid_list.append(person)
118        kid_list.sort(reverse=True)
119        current_host = host_list[0]
120        while len(kid_list[-1].visited_hosts) != len(host_list) and not victims:
121            for kid in kid_list:
122                distance = math.inf
123                for host in host_list:
124                    if distance > kid.distance(host) and host not in kid.visited_hosts:
125                        distance = kid.distance(host)
126                        current_host = host
127                    elif distance == kid.distance(host) and host not in kid.visited_hosts:
128                        if current_host.position[0] > host.position[0]:
129                            distance = kid.distance(host)
130                            current_host = host
131                        elif current_host.position[0] == host.position[0] and current_host.positon[1] > host.position[1]:
132                            distance = kid.distance(host)
133                            current_host = host
134
135                kid.visited_hosts.append(current_host)
136                if not current_host.candies:
137                    continue
138                kid.add_candy(current_host.remove_candy(max))
139                if kid.is_critical:
140                    victims.append(kid)
141
142        if victims:
143            return victims
144        
145        
146    

EEEE.FE.EE.E
======================================================================
ERROR: test_basic_usage (test.CandyTest)
Test basic usage of Candy class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 14, in test_basic_usage
self.assertEqual(candy.get_mass(), 5)
TypeError: 'int' object is not callable

======================================================================
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 80, in test_empty
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 119, in get_victim
current_host = host_list[0]
IndexError: list index out of range

======================================================================
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 119, in get_victim
current_host = host_list[0]
IndexError: list index out of range

======================================================================
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 96, in test_empty_kids
self.assertEqual(flux_capacitor.get_victim(), None)
File "/tmp/solution.py", line 120, in get_victim
while len(kid_list[-1].visited_hosts) != len(host_list) and not victims:
IndexError: list index out of range

======================================================================
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)
File "/tmp/solution.py", line 101, in remove_candy
return f(list_of_candies)
File "/tmp/test.py", line 63, in <lambda>
compare_fun = lambda candies: min(candies, key=lambda candy: candy.get_mass())
File "/tmp/test.py", line 63, in <lambda>
compare_fun = lambda candies: min(candies, key=lambda candy: candy.get_mass())
TypeError: 'int' object is not callable

======================================================================
ERROR: test_basic_usage (test.KidTest)
Test basic usage of Kid class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 39, in test_basic_usage
self.assertEqual(kid.get_position(), (0, 0))
TypeError: 'tuple' object is not callable

======================================================================
ERROR: test_candies (test.KidTest)
Test basic usage of candies in the Kid class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 48, in test_candies
self.assertFalse(kid.is_critical())
File "/tmp/solution.py", line 73, in is_critical
uranium_grams += self.basket[item].get_uranium_quantity()
TypeError: list indices must be integers or slices, not Candy

======================================================================
ERROR: test_basic_usage (test.PersonTest)
Test basic usage of Person class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 24, in test_basic_usage
self.assertEqual(person.get_position(), (0, 0))
TypeError: 'tuple' object is not callable

======================================================================
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: [<solution.Kid object at 0x7f110679b5b0>,[36 chars]070>] != {<solution.Kid object at 0x7f110679b5b0>,[36 chars]070>}

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

FAILED (failures=1, errors=8)

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