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

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

6 точки общо

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

  1import math
  2critical_uranium_quantity = 20
  3
  4class Candy:
  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
 16
 17class Person:
 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    collection_of_candy = []
 31
 32    def __init__(self, position, initiative):
 33        Person.__init__(self, position)
 34        self.initiative = initiative
 35
 36    def get_initiative(self):
 37        return self.initiative
 38
 39    def add_candy(self, candy):
 40        self.collection_of_candy.append((candy.get_mass(), candy.get_uranium_quantity()))
 41
 42    def is_critical(self):
 43        sum_of_u = 0
 44
 45        for piece_of_candy in self.collection_of_candy:
 46            sum_of_u += piece_of_candy[1]
 47        
 48        return sum_of_u > critical_uranium_quantity
 49
 50
 51class Host(Person):
 52
 53    def __init__(self, position, candies):
 54        Person.__init__(self, position)
 55        self.candies = candies
 56
 57    def remove_candy(self, func):
 58        if self.candies == []:
 59            return None
 60        else:
 61            chosen_candy = func(self.candies)
 62            self.candies.remove(chosen_candy)
 63            return chosen_candy
 64
 65class FluxCapacitor:
 66
 67    def __init__(self, participants):
 68        self.participants = participants
 69
 70    def get_victim(self):
 71
 72        def closer(child, host1, host2):
 73
 74            def get_distance(x1, y1, x2, y2):
 75                return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
 76            
 77            distance_to_host1 = get_distance(child.get_position()[0], child.get_position()[1], host1.get_position()[0], host1.get_position()[1])
 78            distance_to_host2 = get_distance(child.get_position()[0], child.get_position()[1], host2.get_position()[0], host2.get_position()[1])
 79
 80            if distance_to_host1 < distance_to_host2:
 81                return host1
 82            elif distance_to_host1 > distance_to_host2:
 83                return host2
 84            else:
 85                if host1.get_position()[0] < host2.get_position()[0]:
 86                    return host1
 87                elif host1.get_position()[0] > host2.get_position()[0]:
 88                    return host2
 89                else:
 90                    if host1.get_position()[1] < host2.get_position()[1]:
 91                        return host1
 92                    else:
 93                        return host2
 94
 95        victims = {}
 96        kids = {}
 97        hosts = {}
 98
 99        for participant in self.participants:
100            if type(participant) is Kid:
101                kids.append(participant)
102            else:
103                hosts.append(participant)
104
105        for _ in hosts:
106            for child in kids:
107                child.nearest_host = None
108                child.visited_hosts = {}
109                for host in hosts:
110                    if host in child.visited_hosts:
111                        continue
112
113                    if child.nearest_host is None:
114                        child.nearest_host = host
115                    else:
116                        child.nearest_host = closer(child, child.nearest_host, host)
117
118        # при колизия проверяваме за инициативност на детето
119        # host remove_candy
120        # kid add_candy
121
122
123
124
125        for participant in self.participants:
126            if type(participant) is Kid and participant.is_critical():
127                victims.append(participant)
128        
129        if victims:           
130            #stop game
131            return victims
132        else:
133            return None

..EEEEE.....
======================================================================
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 101, in get_victim
kids.append(participant)
AttributeError: 'dict' object has no attribute 'append'

======================================================================
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 103, in get_victim
hosts.append(participant)
AttributeError: 'dict' object has no attribute 'append'

======================================================================
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 103, in get_victim
hosts.append(participant)
AttributeError: 'dict' object has no attribute 'append'

======================================================================
ERROR: 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})
File "/tmp/solution.py", line 101, in get_victim
kids.append(participant)
AttributeError: 'dict' object has no attribute 'append'

======================================================================
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 61, in remove_candy
chosen_candy = func(self.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())
AttributeError: 'tuple' object has no attribute 'get_mass'

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

FAILED (errors=5)

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