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

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

10 точки общо

12 успешни теста
0 неуспешни теста
Код

  1class Candy:
  2    
  3    def __init__(self, mass, uranium):
  4        self._mass = mass
  5        self._uranium = uranium
  6
  7    def get_uranium_quantity(self):
  8        return self._mass * self._uranium
  9
 10    def get_mass(self):
 11        return self._mass
 12
 13
 14class Person:
 15    
 16    def __init__(self, position):
 17        self._position = position
 18
 19    def get_position(self):
 20        return self._position
 21    
 22    def set_position(self, new_position):
 23        self._position = new_position
 24
 25
 26class Kid(Person):
 27    
 28    URANIUM_SUM_LIMIT = 20
 29    def __init__(self, position, initiative):
 30        super().__init__(position)
 31        self._initiative = initiative
 32        self._candies = []
 33
 34    def get_initiative(self):
 35        return self._initiative
 36    
 37    def add_candy(self, candy):
 38        self._candies.append(candy)
 39
 40    def is_critical(self):
 41        return sum(map(Candy.get_uranium_quantity, self._candies)) > self.URANIUM_SUM_LIMIT
 42    
 43
 44class Host(Person):
 45    
 46    def __init__(self, position, candies):
 47        super().__init__(position)
 48        self._candies = [Candy(*candy) for candy in candies]
 49
 50    def remove_candy(self, function_predicate):
 51        if not self._candies:
 52            return None
 53        else:
 54            candy_to_remove = function_predicate(self._candies)
 55            self._candies.remove(candy_to_remove)
 56            return candy_to_remove
 57    
 58    @staticmethod
 59    def get_max_candy(candies):
 60        return max(candies, key=lambda candy: candy._mass)
 61
 62
 63class FluxCapacitor:
 64    
 65    def __init__(self, participants):
 66        self._hosts = []
 67        self._kids = []
 68        for person in participants:
 69            if type(person) is Host:
 70                self._hosts.append(person)    
 71            else:
 72                self._kids.append(person)
 73
 74    def get_victim(self):
 75        kids_data = list(map(list, map(lambda kid: [kid, self._hosts.copy()], self._kids)))
 76        counter_all_hosts_already_visited = 0
 77        while counter_all_hosts_already_visited < len(self._hosts):
 78            counter_all_hosts_already_visited += 1
 79            host_data = list(map(lambda host: [], self._hosts))
 80            for kid_data in kids_data:
 81                closest_host = None
 82                closest_host_dist = -1
 83                curr_closest_host_index = 0
 84                closet_host_index = 0
 85                for host in kid_data[1]:
 86                    if host is None:
 87                        curr_closest_host_index += 1
 88                        continue
 89                    curr_dist_to_host = ((list(host._position)[0] - list(kid_data[0]._position)[0]) ** 2
 90                                          + (list(host._position)[1] - list(kid_data[0]._position)[1]) ** 2) ** 0.5
 91                    if closest_host_dist == -1 or closest_host_dist > curr_dist_to_host:
 92                        closest_host = host
 93                        closest_host_dist = curr_dist_to_host
 94                        closet_host_index = curr_closest_host_index
 95                    elif ((closest_host_dist == curr_dist_to_host) and
 96                    (host._position[0] < closest_host._position[0] or
 97                         (host._position[0] == closest_host._position[0] and
 98                          (host._position[1] < closest_host._position[1])))):
 99                        closest_host = host
100                        closest_host_dist = curr_dist_to_host
101                        closet_host_index = curr_closest_host_index
102                    curr_closest_host_index+=1
103
104                kid_data[0]._position = closest_host._position
105                host_data[closet_host_index].append(kid_data[0]) 
106                kid_data[1][closet_host_index] = None
107                
108            counter_curr_host = 0
109            for kids_going_to in host_data:
110                kids_going_to.sort(reverse=True, key=lambda kid: kid._initiative)
111                for kid in kids_going_to:  
112                    candy_to_add = self._hosts[counter_curr_host].remove_candy(Host.get_max_candy)
113                    if candy_to_add is not None:
114                        kid.add_candy(candy_to_add)
115                counter_curr_host+=1
116            kids_above_safe_uranium_level = set()
117            for kid in self._kids:
118                if kid.is_critical():
119                    kids_above_safe_uranium_level.add(kid)
120            if kids_above_safe_uranium_level:
121                return kids_above_safe_uranium_level
122        return None

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

OK

Дискусия
История

f1class Candy:f1class Candy:
nn2    
2    def __init__(self, mass, uranium):3    def __init__(self, mass, uranium):
3        self._mass = mass4        self._mass = mass
4        self._uranium = uranium5        self._uranium = uranium
56
6    def get_uranium_quantity(self):7    def get_uranium_quantity(self):
7        return self._mass * self._uranium8        return self._mass * self._uranium
89
9    def get_mass(self):10    def get_mass(self):
10        return self._mass11        return self._mass
1112
1213
13class Person:14class Person:
nn15    
14    def __init__(self, position):16    def __init__(self, position):
15        self._position = position17        self._position = position
1618
17    def get_position(self):19    def get_position(self):
18        return self._position20        return self._position
19    21    
20    def set_position(self, new_position):22    def set_position(self, new_position):
21        self._position = new_position23        self._position = new_position
2224
2325
24class Kid(Person):26class Kid(Person):
nn27    
25    URANIUM_SUM_LIMIT = 2028    URANIUM_SUM_LIMIT = 20
26    def __init__(self, position, initiative):29    def __init__(self, position, initiative):
27        super().__init__(position)30        super().__init__(position)
28        self._initiative = initiative31        self._initiative = initiative
29        self._candies = []32        self._candies = []
3033
31    def get_initiative(self):34    def get_initiative(self):
32        return self._initiative35        return self._initiative
33    36    
34    def add_candy(self, candy):37    def add_candy(self, candy):
35        self._candies.append(candy)38        self._candies.append(candy)
3639
37    def is_critical(self):40    def is_critical(self):
38        return sum(map(Candy.get_uranium_quantity, self._candies)) > self.URANIUM_SUM_LIMIT41        return sum(map(Candy.get_uranium_quantity, self._candies)) > self.URANIUM_SUM_LIMIT
39    42    
4043
41class Host(Person):44class Host(Person):
nn45    
42    def __init__(self, position, candies):46    def __init__(self, position, candies):
43        super().__init__(position)47        super().__init__(position)
n44        self._candies = []n48        self._candies = [Candy(*candy) for candy in candies]
45        for candy_tuple in candies:
46            self._candies.append(Candy(candy_tuple[0], candy_tuple[1]))
4749
48    def remove_candy(self, function_predicate):50    def remove_candy(self, function_predicate):
n49        if len(self._candies) == 0:n51        if not self._candies:
50            return None52            return None
51        else:53        else:
52            candy_to_remove = function_predicate(self._candies)54            candy_to_remove = function_predicate(self._candies)
53            self._candies.remove(candy_to_remove)55            self._candies.remove(candy_to_remove)
54            return candy_to_remove56            return candy_to_remove
55    57    
56    @staticmethod58    @staticmethod
57    def get_max_candy(candies):59    def get_max_candy(candies):
58        return max(candies, key=lambda candy: candy._mass)60        return max(candies, key=lambda candy: candy._mass)
5961
6062
61class FluxCapacitor:63class FluxCapacitor:
nn64    
62    def __init__(self, participants):65    def __init__(self, participants):
n63        self._participants = participantsn66        self._hosts = []
67        self._kids = []
68        for person in participants:
69            if type(person) is Host:
70                self._hosts.append(person)    
71            else:
72                self._kids.append(person)
6473
65    def get_victim(self):74    def get_victim(self):
n66        hosts = []n
67        kids = []
68        for person in self._participants:
69            if person.__class__.__name__ == 'Host':
70                hosts.append(person)    
71            else:
72                kids.append(person)
73        kids_data = list(map(list, map(lambda kid: [kid, hosts.copy()], kids)))75        kids_data = list(map(list, map(lambda kid: [kid, self._hosts.copy()], self._kids)))
74        
75        counter_all_hosts_already_visited = 076        counter_all_hosts_already_visited = 0
n76        while counter_all_hosts_already_visited < len(hosts):n77        while counter_all_hosts_already_visited < len(self._hosts):
77            counter_all_hosts_already_visited += 178            counter_all_hosts_already_visited += 1
n78            host_data = list(map(lambda host: [], hosts))n79            host_data = list(map(lambda host: [], self._hosts))
79            for kid_data in kids_data:80            for kid_data in kids_data:
80                closest_host = None81                closest_host = None
81                closest_host_dist = -182                closest_host_dist = -1
82                curr_closest_host_index = 083                curr_closest_host_index = 0
83                closet_host_index = 084                closet_host_index = 0
84                for host in kid_data[1]:85                for host in kid_data[1]:
85                    if host is None:86                    if host is None:
n86                        curr_closest_host_index+=1n87                        curr_closest_host_index += 1
87                        continue88                        continue
n88 n
89                    curr_dist_to_host = ((list(host._position)[0] - list(kid_data[0]._position)[0]) ** 289                    curr_dist_to_host = ((list(host._position)[0] - list(kid_data[0]._position)[0]) ** 2
90                                          + (list(host._position)[1] - list(kid_data[0]._position)[1]) ** 2) ** 0.590                                          + (list(host._position)[1] - list(kid_data[0]._position)[1]) ** 2) ** 0.5
n91                n
92                    if closest_host_dist == -1 or closest_host_dist > curr_dist_to_host:91                    if closest_host_dist == -1 or closest_host_dist > curr_dist_to_host:
93                        closest_host = host92                        closest_host = host
94                        closest_host_dist = curr_dist_to_host93                        closest_host_dist = curr_dist_to_host
95                        closet_host_index = curr_closest_host_index94                        closet_host_index = curr_closest_host_index
n96                    elif closest_host_dist == curr_dist_to_host \n95                    elif ((closest_host_dist == curr_dist_to_host) and
97                    and (host._position[0] < closest_host._position[0] 96                    (host._position[0] < closest_host._position[0] or
98                         or (host._position[0] == closest_host._position[0] 97                         (host._position[0] == closest_host._position[0] and
99                             and host._position[1] < closest_host._position[1])):98                          (host._position[1] < closest_host._position[1])))):
100                        closest_host = host99                        closest_host = host
101                        closest_host_dist = curr_dist_to_host100                        closest_host_dist = curr_dist_to_host
102                        closet_host_index = curr_closest_host_index101                        closet_host_index = curr_closest_host_index
103                    curr_closest_host_index+=1102                    curr_closest_host_index+=1
104103
105                kid_data[0]._position = closest_host._position104                kid_data[0]._position = closest_host._position
106                host_data[closet_host_index].append(kid_data[0]) 105                host_data[closet_host_index].append(kid_data[0]) 
107                kid_data[1][closet_host_index] = None106                kid_data[1][closet_host_index] = None
108                107                
109            counter_curr_host = 0108            counter_curr_host = 0
110            for kids_going_to in host_data:109            for kids_going_to in host_data:
111                kids_going_to.sort(reverse=True, key=lambda kid: kid._initiative)110                kids_going_to.sort(reverse=True, key=lambda kid: kid._initiative)
112                for kid in kids_going_to:  111                for kid in kids_going_to:  
n113                    candy_to_add = hosts[counter_curr_host].remove_candy(Host.get_max_candy)n112                    candy_to_add = self._hosts[counter_curr_host].remove_candy(Host.get_max_candy)
114                    if candy_to_add is not None:113                    if candy_to_add is not None:
115                        kid.add_candy(candy_to_add)114                        kid.add_candy(candy_to_add)
116                counter_curr_host+=1115                counter_curr_host+=1
117            kids_above_safe_uranium_level = set()116            kids_above_safe_uranium_level = set()
n118            for kid in kids:n117            for kid in self._kids:
119                if kid.is_critical():118                if kid.is_critical():
120                    kids_above_safe_uranium_level.add(kid)119                    kids_above_safe_uranium_level.add(kid)
t121            if(len(kids_above_safe_uranium_level) != 0):t120            if kids_above_safe_uranium_level:
122                return kids_above_safe_uranium_level121                return kids_above_safe_uranium_level
123        return None122        return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op