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

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

10 точки общо

12 успешни теста
0 неуспешни теста
Код (methods in FluxCapacitor improved)

  1import math
  2
  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
 16class Person:
 17    def __init__(self, position):
 18        self.position = position
 19        self.basket_of_sweets = []
 20
 21    def  get_position(self):
 22        return self.position  
 23
 24    def set_position(self, new_position):
 25        self.position = new_position
 26        return self.position
 27
 28
 29class Kid(Person):
 30    def __init__(self, position, initiative):
 31        super().__init__(position)
 32        self.initiative = initiative
 33        self.history_of_positions = []
 34    
 35    def get_initiative(self):
 36        return self.initiative
 37    
 38    def add_candy(self, sweet):
 39        self.basket_of_sweets.append(sweet)
 40
 41    def is_critical(self):
 42        uranium_q_ty = 0
 43        critical_uranium_q_ty = 20
 44
 45        for sweet in self.basket_of_sweets:
 46            uranium_q_ty += sweet.get_uranium_quantity()
 47        return uranium_q_ty > critical_uranium_q_ty
 48
 49
 50class Host(Person):
 51    def __init__(self, position, candies):
 52        super().__init__(position)
 53        self.candies = candies
 54        
 55        for candy in self.candies:
 56            self.basket_of_sweets.append(Candy(*candy))
 57    
 58    def remove_candy(self, func):
 59        if not self.basket_of_sweets:
 60            return None
 61        candy = func(self.basket_of_sweets)
 62
 63        self.basket_of_sweets.remove(candy)
 64        return candy
 65    
 66
 67class FluxCapacitor:
 68    def __init__(self, participants):
 69        self.participants = participants
 70        self.kids = []
 71        self.hosts = []
 72
 73        for person in participants:
 74            if type(person) is Kid:
 75                self.kids.append(person)
 76            elif type(person) is Host:
 77                self.hosts.append(person)
 78
 79    def set_new_position(self):
 80        for kid in self.kids:
 81            distance = 0
 82            new_position = ()
 83            list_of_distances_and_positions = []
 84            for host in self.hosts:
 85                if host.get_position() in kid.history_of_positions:
 86                    continue
 87                distance = math.dist(kid.get_position(), host.get_position())
 88                list_of_distances_and_positions.append((distance, host.get_position()))
 89  
 90            new_position = min(list_of_distances_and_positions)[1]
 91            kid.set_position(new_position) 
 92            kid.history_of_positions.append(kid.get_position())
 93
 94    @staticmethod
 95    def biggest_sweet(basket_of_sweets):
 96        biggest_sweet = max(basket_of_sweets, key=lambda x: x.mass)
 97        return biggest_sweet
 98
 99    def recieve_candy(self):
100        for host in self.hosts:
101            kids_with_this_host = []
102
103            for kid in self.kids:
104                if kid.get_position() == host.get_position():
105                    kids_with_this_host.append(kid)
106            
107            kids_with_this_host.sort(key=lambda x: x.initiative, reverse = True)
108            
109            for kid in kids_with_this_host:
110                sweet = host.remove_candy(self.biggest_sweet)
111                if sweet:
112                    kid.add_candy(sweet)
113
114    def take_kids_to_heaven(self):
115        kids_in_heaven = set()
116
117        for kid in self.kids:
118            if kid.is_critical():
119                kids_in_heaven.add(kid)
120
121        return kids_in_heaven
122        
123    def get_victim(self):
124        for host in self.hosts:
125            self.set_new_position()
126            self.recieve_candy()
127            kids_in_heaven = self.take_kids_to_heaven()
128           
129            if kids_in_heaven:
130                return kids_in_heaven
131            
132        return None   

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

OK

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

f1import mathf1import math
22
33
4class Candy:4class Candy:
n5    def __init__(self, mass, uranium ):n5    def __init__(self, mass, uranium):
6        self.mass = mass6        self.mass = mass
7        self.uranium = uranium 7        self.uranium = uranium 
88
9    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
10        return self.mass * self.uranium 10        return self.mass * self.uranium 
11    11    
12    def get_mass(self):12    def get_mass(self):
13        return self.mass13        return self.mass
14    14    
1515
16class Person:16class Person:
17    def __init__(self, position):17    def __init__(self, position):
18        self.position = position18        self.position = position
19        self.basket_of_sweets = []19        self.basket_of_sweets = []
2020
21    def  get_position(self):21    def  get_position(self):
22        return self.position  22        return self.position  
2323
24    def set_position(self, new_position):24    def set_position(self, new_position):
25        self.position = new_position25        self.position = new_position
26        return self.position26        return self.position
2727
2828
29class Kid(Person):29class Kid(Person):
n30    def __init__ (self, position, initiative):n30    def __init__(self, position, initiative):
31        super().__init__(position)31        super().__init__(position)
32        self.initiative = initiative32        self.initiative = initiative
33        self.history_of_positions = []33        self.history_of_positions = []
34    34    
35    def get_initiative(self):35    def get_initiative(self):
36        return self.initiative36        return self.initiative
37    37    
n38    def add_candy(self, sweet ):n38    def add_candy(self, sweet):
39        self.basket_of_sweets.append(sweet)39        self.basket_of_sweets.append(sweet)
4040
41    def is_critical(self):41    def is_critical(self):
n42        self.history_of_positions.append(self.get_position())n
43        uranium_q_ty = 042        uranium_q_ty = 0
n44        n43        critical_uranium_q_ty = 20
44 
45        for sweet in self.basket_of_sweets:45        for sweet in self.basket_of_sweets:
46            uranium_q_ty += sweet.get_uranium_quantity()46            uranium_q_ty += sweet.get_uranium_quantity()
n47        if uranium_q_ty > 20: n47        return uranium_q_ty > critical_uranium_q_ty
48            return True
49        else:
50            return False
5148
n52     n
53def biggest_sweet(basket_of_sweets):
54        sweet_mass = 0
55        biggest_sweet = 0
56        
57        for candy in basket_of_sweets:
58            if candy.mass > sweet_mass:
59                sweet_mass = candy.mass
60                biggest_sweet = candy
61 
62        return biggest_sweet
6349
64class Host(Person):50class Host(Person):
65    def __init__(self, position, candies):51    def __init__(self, position, candies):
66        super().__init__(position)52        super().__init__(position)
67        self.candies = candies53        self.candies = candies
68        54        
69        for candy in self.candies:55        for candy in self.candies:
n70            self.basket_of_sweets.append(Candy(candy[0], candy[1]))n56            self.basket_of_sweets.append(Candy(*candy))
71    57    
n72    n
73    def remove_candy (self, func):58    def remove_candy(self, func):
74        if not self.basket_of_sweets:59        if not self.basket_of_sweets:
75            return None60            return None
76        candy = func(self.basket_of_sweets)61        candy = func(self.basket_of_sweets)
nn62 
77        self.basket_of_sweets.remove(candy)63        self.basket_of_sweets.remove(candy)
78        return candy64        return candy
79    65    
n80 n
81def distance_between_two_points(point_A, point_B):
82    return math.sqrt(math.pow(point_B[0] - point_A[0], 2) + math.pow(point_B[1] - point_A[1], 2))
83 
8466
85class FluxCapacitor:67class FluxCapacitor:
86    def __init__(self, participants):68    def __init__(self, participants):
87        self.participants = participants69        self.participants = participants
88        self.kids = []70        self.kids = []
89        self.hosts = []71        self.hosts = []
9072
n91        while participants:n
92            person = participants.pop() 73        for person in participants:
93            if type(person) is Kid:74            if type(person) is Kid:
94                self.kids.append(person)75                self.kids.append(person)
95            elif type(person) is Host:76            elif type(person) is Host:
96                self.hosts.append(person)77                self.hosts.append(person)
9778
98    def set_new_position(self):79    def set_new_position(self):
99        for kid in self.kids:80        for kid in self.kids:
100            distance = 081            distance = 0
n101            min_distance = float('inf')n
102            new_position = ()82            new_position = ()
n103 n83            list_of_distances_and_positions = []
104            for host in self.hosts:84            for host in self.hosts:
105                if host.get_position() in kid.history_of_positions:85                if host.get_position() in kid.history_of_positions:
106                    continue86                    continue
n107                distance = distance_between_two_points(kid.get_position(), host.get_position()) n87                distance = math.dist(kid.get_position(), host.get_position())
108                if min_distance > distance:88                list_of_distances_and_positions.append((distance, host.get_position()))
109                    min_distance = distance89  
110                    new_position = host.get_position()90            new_position = min(list_of_distances_and_positions)[1]
111                elif min_distance == distance:91            kid.set_position(new_position) 
112                    if new_position[0] > host.get_position()[0]:92            kid.history_of_positions.append(kid.get_position())
113                        new_position = host.get_position()
114                    elif new_position[1] > host.get_position()[1]:
115                        new_position = host.get_position()   
11693
n117            kid.set_position(new_position) n94    @staticmethod
118        95    def biggest_sweet(basket_of_sweets):
96        biggest_sweet = max(basket_of_sweets, key=lambda x: x.mass)
97        return biggest_sweet
98 
119    def recieve_candy(self):99    def recieve_candy(self):
120        for host in self.hosts:100        for host in self.hosts:
121            kids_with_this_host = []101            kids_with_this_host = []
122102
123            for kid in self.kids:103            for kid in self.kids:
124                if kid.get_position() == host.get_position():104                if kid.get_position() == host.get_position():
125                    kids_with_this_host.append(kid)105                    kids_with_this_host.append(kid)
126            106            
n127            kids_with_this_host.sort(key = lambda x: x.initiative, reverse = True)n107            kids_with_this_host.sort(key=lambda x: x.initiative, reverse = True)
128            108            
129            for kid in kids_with_this_host:109            for kid in kids_with_this_host:
n130                sweet = host.remove_candy(biggest_sweet)n110                sweet = host.remove_candy(self.biggest_sweet)
131                if sweet:111                if sweet:
132                    kid.add_candy(sweet)112                    kid.add_candy(sweet)
133113
134    def take_kids_to_heaven(self):114    def take_kids_to_heaven(self):
135        kids_in_heaven = set()115        kids_in_heaven = set()
136116
137        for kid in self.kids:117        for kid in self.kids:
138            if kid.is_critical():118            if kid.is_critical():
139                kids_in_heaven.add(kid)119                kids_in_heaven.add(kid)
140120
141        return kids_in_heaven121        return kids_in_heaven
142        122        
n143 n
144    def get_victim(self):123    def get_victim(self):
145        for host in self.hosts:124        for host in self.hosts:
146            self.set_new_position()125            self.set_new_position()
147            self.recieve_candy()126            self.recieve_candy()
148            kids_in_heaven = self.take_kids_to_heaven()127            kids_in_heaven = self.take_kids_to_heaven()
149           128           
150            if kids_in_heaven:129            if kids_in_heaven:
151                return kids_in_heaven130                return kids_in_heaven
152            131            
t153        return Nonet132        return None   
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op