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

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

8 точки общо

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

  1import math
  2
  3class Candy:
  4    def __init__(self, mass: float, uranium: float):
  5        self.mass = mass
  6        self.uranium = uranium
  7 
  8    def get_mass(self):
  9        return self.mass
 10   
 11    def get_uranium_quantity(self):
 12        return self.mass * self.uranium
 13    
 14class Person:
 15    def __init__(self, position: tuple):
 16        self.position = position
 17
 18    def get_position(self):
 19        return self.position
 20 
 21    def set_position(self, new_position: tuple):
 22        self.position = new_position
 23        
 24class Kid(Person):
 25    candies = []
 26    indexes_of_hosts = []
 27    
 28    def __init__(self, position: tuple, initiative: int):
 29        super().__init__(position)
 30        self.initiative = initiative
 31        
 32    def get_initiative(self):
 33        return self.initiative
 34    
 35    def add_candy(self, candy : Candy):
 36        self.candies.append(candy)
 37        
 38    def is_critical(self):
 39        sum = 0
 40        
 41        for candy in self.candies:
 42            sum = sum + candy.get_uranium_quantity()
 43            
 44        if sum > 20:
 45            return True
 46     
 47        return False
 48    
 49class Host(Person):
 50    basket = [] 
 51    kids_to_give_treat = []
 52
 53    def __init__(self, position: tuple, candies: list):
 54        super().__init__(position)
 55        self.basket = candies
 56        
 57    def remove_candy(self, func):
 58        if self.basket.__len__() == 0:
 59            return None
 60        
 61        candy_to_remove =  func(self.basket)
 62        self.basket.remove(candy_to_remove)
 63        return candy_to_remove
 64    
 65    def add_kid(self, kid):
 66        self.kids_to_give_treat.append(kid)
 67        kid.set_position(self.position)
 68    
 69    def give_treats_to_kids(self, func):
 70        if(self.kids_to_give_treat.__len__() ==  0):
 71            return False        
 72
 73        while self.kids_to_give_treat.__len__() > 0:
 74
 75            kid_to_host = self.kids_to_give_treat[0]
 76            if(self.kids_to_give_treat.__len__() > 1):
 77            
 78                for kid in self.kids_to_give_treat:
 79                    if(kid.get_initiative() > kid_to_host.get_initiative()):
 80                        kid_to_host = kid
 81                    
 82            kid_to_host.add_candy(self.remove_candy(func))
 83            self.kids_to_give_treat.remove(kid_to_host)
 84            
 85        return True;
 86                
 87            
 88
 89class FluxCapacitor:
 90    hosts = []
 91    kids = []
 92    dead_kids = []
 93    
 94    def calculate_dist(self, x1, y1, x2, y2):
 95        return ((x2 - x1) ** 2 + (y2 - y1) ** 2)
 96
 97    def __init__(self, participants: set):
 98        testTypeKid = Kid((0,0), 100)
 99        testTypeHost = Host((3, 4), [(1, 1.0), (2, 0.5)])
100        self.participants = participants
101        
102        for participant in self.participants:
103            if type(participant) == type(testTypeKid):
104               self.kids.append(participant)
105            elif type(participant) == type(testTypeHost):
106                self.hosts.append(participant)
107                
108    def get_victim(self):
109        if self.dead_kids.__len__() == 0:
110            return None
111        return set(self.dead_kids)
112                
113    def send_kids_to_hosts(self):
114        if(self.hosts.__len__() == 0 or self.kids.__len__() == 0)  :
115            return False
116
117        for kid in self.kids:
118            best_dist = self.calculate_dist(kid.get_position()[0], kid.get_position()[1], self.hosts[0].get_position()[0], self.hosts[0].get_position()[1])
119            best_host = self.hosts[0]
120            for host in self.hosts:
121                if self.calculate_dist(kid.get_position()[0], kid.get_position()[1], host.get_position()[0], host.get_position()[1]) < best_dist and kid.indexes_of_hosts.count(self.hosts.index(host)) == 0:
122                    best_dist = self.calculate_dist(kid.get_position()[0], kid.get_position()[1], host.get_position()[0], host.get_position()[1])
123                    best_host = host
124                    
125            best_host.add_kid(kid)
126            kid.indexes_of_hosts.append(self.hosts.index(best_host))
127        
128    def hosts_give_candies(self, func):
129        for host in self.hosts:
130            host.give_treats_to_kids(func)
131            
132    def check_if_kids_went_everywhere(self):
133        for kid in self.kids:
134            for i in range(0, self.hosts.__len__):
135                if kid.indexes_of_hosts.count(i) == 0:
136                    return False
137                
138        return True
139    
140    def check_if_all_candy_is_done(self):
141        for host in self.hosts:
142            if host.basket.__len__() != 0:
143                return False
144        return True
145            
146    def write_the_dead(self):
147        for kid in self.kids:
148            if kid.is_critical():
149                self.dead_kids.append(kid)
150                self.kids.remove(kid)
151                
152    def run(self, func):
153        while True:
154            self.send_kids_to_hosts()
155            self.hosts_give_candies(func)
156            self.write_the_dead()
157            
158            if self.get_victim() != None:
159                return self.get_victim()
160            if self.check_if_kids_went_everywhere() or self.check_if_all_candy_is_done():
161                return None
162            
163
164
165
166
167        
168                
169        
170
171        

.....FE.....
======================================================================
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
candy_to_remove = func(self.basket)
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'

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

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

FAILED (failures=1, errors=1)

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