Домашни > Хелоуин в Припят > Решения > Решението на Магдалена Иванова

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

7 точки общо

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

  1import math
  2
  3
  4class Candy:
  5
  6    def __init__(self, mass, uranium):
  7        self.mass = mass
  8        self.uranium = uranium
  9
 10    def get_uranium_quantity(self):
 11        return self.mass * self.uranium
 12    
 13    def get_mass(self):
 14        return self.mass
 15    
 16    def __gt__(self, other):
 17        if(self.get_mass() > other.get_mass()):
 18            return True
 19        return False
 20
 21
 22class Person:
 23
 24    def __init__(self, position):
 25        self.position = position
 26
 27    def get_position(self):
 28        return self.position
 29    
 30    def set_position(self, new_position):
 31        self.position = new_position
 32
 33
 34class Kid(Person):
 35
 36    kid_uranium = 0
 37
 38    def __init__(self, position, initiative):
 39         super().__init__(position)
 40         self.initiative = initiative
 41
 42    def get_initiative(self):
 43        return self.initiative
 44    
 45    def add_candy(self, mass, uranium):
 46        self.candy = Candy(mass, uranium)
 47        self.kid_uranium += self.candy.get_uranium_quantity()
 48
 49    def is_critical(self):
 50        if (self.kid_uranium > 20):
 51            return True
 52        return False
 53    
 54    def __lt__(self, other):
 55        if(self.get_initiative() < other.get_initiative()):
 56            return True
 57        return False
 58    
 59    def get_closest(self, hosts):
 60        min_dist = float('inf')
 61        distances = list()
 62        for host in hosts:
 63            curr_dist = math.dist(self.get_position(), host.get_position())
 64            if(curr_dist <  min_dist):
 65                min_dist = curr_dist
 66                closest_host = host
 67
 68            if curr_dist in distances:
 69                    idx = distances.index(curr_dist)
 70                    closest_host = min(host, hosts[idx])
 71            else:
 72                distances.append(curr_dist)
 73        return closest_host
 74
 75
 76class Host(Person):
 77
 78    candies = []
 79
 80    def __init__(self, position, candies):
 81         super().__init__(position)
 82         for i in candies:
 83             host_candy = Candy(i[0], i[1])
 84             self.candies.append(host_candy)
 85
 86    def remove_candy(self, func):
 87        if(len(self.candies) == 0):
 88            return None
 89        return func(self.candies)
 90    
 91    def __lt__(self, other):
 92        if(self.get_position()[0] < other.get_position()[0]):
 93            return True
 94        elif(self.get_position()[0] == other.get_position()[0] and self.get_position()[1] < other.get_position()[1]):
 95            return True
 96        else: 
 97            return False
 98
 99
100
101class FluxCapacitor():
102
103    victims = []
104    hosts = list()
105
106    def __init__(self, participants):
107        for i in participants:
108            if( isinstance(i, Host) ):
109                self.hosts.append(i)
110
111        for kid in participants:
112            if( isinstance(kid, Kid) ):
113               
114                closest_host = kid.get_closest(self.hosts)
115    
116                kid.set_position(closest_host.get_position())
117                candy = closest_host.remove_candy(max)
118                kid.add_candy(candy.get_mass(), candy.get_uranium_quantity())
119            
120                if(kid.is_critical()):
121                    self.victims.append(kid)
122
123                if(self.get_victim() != None):
124                    return
125    
126    def get_victim(self):
127        if(len(self.victims) == 0):
128            return None
129        return self.victims
130    

..E..FF..E..
======================================================================
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 87, in test_empty_hosts
flux_capacitor = FluxCapacitor({kid1, kid2})
File "/tmp/solution.py", line 114, in __init__
closest_host = kid.get_closest(self.hosts)
File "/tmp/solution.py", line 73, in get_closest
return closest_host
UnboundLocalError: local variable 'closest_host' referenced before assignment

======================================================================
ERROR: test_candies (test.KidTest)
Test basic usage of candies in the Kid class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 47, in test_candies
kid.add_candy(candy)
TypeError: Kid.add_candy() missing 1 required positional argument: 'uranium'

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

======================================================================
FAIL: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 66, in test_basic_usage
self.assertEqual(candy.get_mass(), 456)
AssertionError: 1 != 456

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

FAILED (failures=2, errors=2)

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