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

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

10 точки общо

12 успешни теста
0 неуспешни теста
Код (Fixed most pep8 things)
Скрий всички коментари

  1import math
  2
  3
  4class Candy:
  5
  6    def __init__(self, mass: int, uranium: float):
  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
 17class Person:
 18
 19    def __init__(self, position: tuple):
 20        self.position = position
 21
 22    def get_position(self):
 23        return self.position
 24
 25    def set_position(self, new_position: tuple):
 26        self.position = new_position
 27
 28
 29class Kid(Person):
 30
 31    def __init__(self, position: tuple, initiative: int):
 32        super().__init__(position)
 33        self.initiative = initiative
 34        self.candy_basket = []
 35        self.hosts_visited = set()
 36
 37    def get_initiative(self):
 38        return self.initiative
 39
 40    def add_candy(self, candy: Candy):
 41        self.candy_basket.append(candy)
 42
 43    def is_critical(self):
 44        current_uranium = 0
 45        candy: Candy
 46        for candy in self.candy_basket:
 47            current_uranium += candy.get_uranium_quantity()
 48
 49        if current_uranium > 20:
 50            return True
 51        return False
 52
 53    def get_hosts_visited(self):
 54        return self.hosts_visited
 55
 56    def visit_host(self, host):
 57        self.hosts_visited.add(host)
 58
 59
 60class Host(Person):
 61
 62    def __init__(self, position: tuple, candies: list):
 63        super().__init__(position)
 64        self.candies = []
 65        for candy_tuple in candies:
 66            self.candies.append(Candy(candy_tuple[0], candy_tuple[1]))
 67
 68    def remove_candy(self, filter_func):
 69        if not self.candies:
 70            return None
 71
 72        to_remove = filter_func(self.candies)
 73        if to_remove is None:
 74            return None
 75        self.candies.remove(to_remove)
 76
 77        return to_remove
 78
 79
 80class FluxCapacitor:
 81
 82    def __init__(self, participants: set):
 83
 84        self.kids = []
 85        self.hosts = []
 86        person: Person
 87        for person in participants:
 88            if type(person) == Kid:
 89                self.kids.append(person)
 90            else:
 91                self.hosts.append(person)
 92
 93        self.kids.sort(key=lambda x: x.initiative, reverse=True)
 94
 95    def get_victim(self):
 96        victims = set()
 97
 98        if len(self.kids) == 0:
 99            return None
100
101        loop = True
102
103        while loop:
104
105            kid: Kid
106            for kid in self.kids:
107                closest_host = self._find_closest_host_to_kid(kid)
108                if closest_host is None:
109                    loop = False
110                    break
111
112                given = closest_host.remove_candy(self.get_biggest_candy)
113                if given is not None:
114                    kid.add_candy(given)
115
116                if kid.is_critical():
117                    victims.add(kid)
118                    loop = False
119
120                kid.visit_host(closest_host)
121                kid.set_position(closest_host.get_position())
122
123
124        if victims:
125            return victims
126        return None
127
128    def _find_closest_host_to_kid(self, kid: Kid):
129        current_closest_dist = -1
130        closest_host = None
131
132        host: Host
133        for host in self.hosts:
134            curr_dist = math.dist(kid.get_position(), host.get_position())
135            if host in kid.get_hosts_visited():
136                continue
137
138            if current_closest_dist < 0 or curr_dist < current_closest_dist:
139                closest_host = host
140                current_closest_dist = curr_dist
141            elif curr_dist == current_closest_dist and host.get_position() < closest_host.get_position():
142                closest_host = host
143                current_closest_dist = curr_dist
144
145        return closest_host
146
147    def get_biggest_candy(self, host_basket: set):
148        biggest = None
149
150        candy: Candy
151        for candy in host_basket:
152            if biggest is None or biggest.get_mass() < candy.get_mass():
153                biggest = candy
154
155        return biggest
156
157    def get_kids(self):
158        return self.kids
159
160
161def get_biggest_candy(candies: list):
162    biggest = None
163
164    candy: Candy
165    for candy in candies:
166        if biggest is None or biggest.get_mass() < candy.get_mass():
167            biggest = candy
168
169    return biggest

............
----------------------------------------------------------------------
Ran 12 tests in 0.000s

OK

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

f1import mathf1import math
n2 n
32
43
5class Candy:4class Candy:
65
7    def __init__(self, mass: int, uranium: float):6    def __init__(self, mass: int, uranium: float):
8        self.mass = mass7        self.mass = mass
9        self.uranium = uranium8        self.uranium = uranium
109
11    def get_uranium_quantity(self):10    def get_uranium_quantity(self):
12        return self.mass * self.uranium11        return self.mass * self.uranium
n13    n12 
14    def get_mass(self):13    def get_mass(self):
15        return self.mass14        return self.mass
n16    n15 
1716
18class Person:17class Person:
1918
20    def __init__(self, position: tuple):19    def __init__(self, position: tuple):
21        self.position = position20        self.position = position
2221
23    def get_position(self):22    def get_position(self):
24        return self.position23        return self.position
n25    n24 
26    def set_position(self, new_position: tuple):25    def set_position(self, new_position: tuple):
n27        self.position = new_position        n26        self.position = new_position
2827
2928
30class Kid(Person):29class Kid(Person):
3130
32    def __init__(self, position: tuple, initiative: int):31    def __init__(self, position: tuple, initiative: int):
33        super().__init__(position)32        super().__init__(position)
34        self.initiative = initiative33        self.initiative = initiative
35        self.candy_basket = []34        self.candy_basket = []
36        self.hosts_visited = set()35        self.hosts_visited = set()
3736
38    def get_initiative(self):37    def get_initiative(self):
39        return self.initiative38        return self.initiative
n40    n39 
41    def add_candy(self, candy: Candy):40    def add_candy(self, candy: Candy):
42        self.candy_basket.append(candy)41        self.candy_basket.append(candy)
4342
44    def is_critical(self):43    def is_critical(self):
45        current_uranium = 044        current_uranium = 0
46        candy: Candy45        candy: Candy
47        for candy in self.candy_basket:46        for candy in self.candy_basket:
n48            current_uranium += candy.get_uranium_quantity()    n47            current_uranium += candy.get_uranium_quantity()
4948
50        if current_uranium > 20:49        if current_uranium > 20:
51            return True50            return True
52        return False51        return False
n53    n52 
54    def get_hosts_visited(self):53    def get_hosts_visited(self):
55        return self.hosts_visited54        return self.hosts_visited
n56    n55 
57    def visit_host(self, host):56    def visit_host(self, host):
58        self.hosts_visited.add(host)57        self.hosts_visited.add(host)
n59    n58 
6059
61class Host(Person):60class Host(Person):
6261
63    def __init__(self, position: tuple, candies: list):62    def __init__(self, position: tuple, candies: list):
64        super().__init__(position)63        super().__init__(position)
65        self.candies = []64        self.candies = []
66        for candy_tuple in candies:65        for candy_tuple in candies:
67            self.candies.append(Candy(candy_tuple[0], candy_tuple[1]))66            self.candies.append(Candy(candy_tuple[0], candy_tuple[1]))
6867
69    def remove_candy(self, filter_func):68    def remove_candy(self, filter_func):
70        if not self.candies:69        if not self.candies:
71            return None70            return None
7271
73        to_remove = filter_func(self.candies)72        to_remove = filter_func(self.candies)
74        if to_remove is None:73        if to_remove is None:
75            return None74            return None
76        self.candies.remove(to_remove)75        self.candies.remove(to_remove)
7776
78        return to_remove77        return to_remove
n79    n78 
8079
81class FluxCapacitor:80class FluxCapacitor:
8281
83    def __init__(self, participants: set):82    def __init__(self, participants: set):
8483
85        self.kids = []84        self.kids = []
86        self.hosts = []85        self.hosts = []
87        person: Person86        person: Person
88        for person in participants:87        for person in participants:
89            if type(person) == Kid:88            if type(person) == Kid:
90                self.kids.append(person)89                self.kids.append(person)
91            else:90            else:
92                self.hosts.append(person)91                self.hosts.append(person)
9392
94        self.kids.sort(key=lambda x: x.initiative, reverse=True)93        self.kids.sort(key=lambda x: x.initiative, reverse=True)
9594
96    def get_victim(self):95    def get_victim(self):
97        victims = set()96        victims = set()
9897
99        if len(self.kids) == 0:98        if len(self.kids) == 0:
100            return None99            return None
101100
102        loop = True101        loop = True
103102
104        while loop:103        while loop:
105104
106            kid: Kid105            kid: Kid
107            for kid in self.kids:106            for kid in self.kids:
108                closest_host = self._find_closest_host_to_kid(kid)107                closest_host = self._find_closest_host_to_kid(kid)
109                if closest_host is None:108                if closest_host is None:
110                    loop = False109                    loop = False
111                    break110                    break
n112                n111 
113                given = closest_host.remove_candy(self.get_biggest_candy)112                given = closest_host.remove_candy(self.get_biggest_candy)
114                if given is not None:113                if given is not None:
115                    kid.add_candy(given)114                    kid.add_candy(given)
n116                n115 
117                if kid.is_critical():116                if kid.is_critical():
118                    victims.add(kid)117                    victims.add(kid)
119                    loop = False118                    loop = False
120119
121                kid.visit_host(closest_host)120                kid.visit_host(closest_host)
122                kid.set_position(closest_host.get_position())121                kid.set_position(closest_host.get_position())
123122
n124            n123 
125        if victims:124        if victims:
126            return victims125            return victims
n127        return None    n126        return None
128    127 
129    def _find_closest_host_to_kid(self, kid: Kid):128    def _find_closest_host_to_kid(self, kid: Kid):
130        current_closest_dist = -1129        current_closest_dist = -1
131        closest_host = None130        closest_host = None
132131
133        host: Host132        host: Host
134        for host in self.hosts:133        for host in self.hosts:
135            curr_dist = math.dist(kid.get_position(), host.get_position())134            curr_dist = math.dist(kid.get_position(), host.get_position())
136            if host in kid.get_hosts_visited():135            if host in kid.get_hosts_visited():
137                continue136                continue
138137
139            if current_closest_dist < 0 or curr_dist < current_closest_dist:138            if current_closest_dist < 0 or curr_dist < current_closest_dist:
140                closest_host = host139                closest_host = host
141                current_closest_dist = curr_dist140                current_closest_dist = curr_dist
142            elif curr_dist == current_closest_dist and host.get_position() < closest_host.get_position():141            elif curr_dist == current_closest_dist and host.get_position() < closest_host.get_position():
143                closest_host = host142                closest_host = host
144                current_closest_dist = curr_dist143                current_closest_dist = curr_dist
n145            n144 
146        return closest_host145        return closest_host
147146
148    def get_biggest_candy(self, host_basket: set):147    def get_biggest_candy(self, host_basket: set):
149        biggest = None148        biggest = None
150149
151        candy: Candy150        candy: Candy
152        for candy in host_basket:151        for candy in host_basket:
n153            if biggest == None or biggest.get_mass() < candy.get_mass():n152            if biggest is None or biggest.get_mass() < candy.get_mass():
154                biggest = candy153                biggest = candy
155154
156        return biggest155        return biggest
157156
158    def get_kids(self):157    def get_kids(self):
159        return self.kids158        return self.kids
n160    n159 
161160
162def get_biggest_candy(candies: list):161def get_biggest_candy(candies: list):
n163        biggest = Nonen162    biggest = None
164163
n165        candy: Candyn164    candy: Candy
166        for candy in candies:165    for candy in candies:
167            if biggest == None or biggest.get_mass() < candy.get_mass():166        if biggest is None or biggest.get_mass() < candy.get_mass():
168                biggest = candy167            biggest = candy
169168
t170        return biggestt169    return biggest
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
22
33
44
5class Candy:5class Candy:
66
7    def __init__(self, mass: int, uranium: float):7    def __init__(self, mass: int, uranium: float):
8        self.mass = mass8        self.mass = mass
9        self.uranium = uranium9        self.uranium = uranium
1010
11    def get_uranium_quantity(self):11    def get_uranium_quantity(self):
12        return self.mass * self.uranium12        return self.mass * self.uranium
13    13    
14    def get_mass(self):14    def get_mass(self):
15        return self.mass15        return self.mass
16    16    
1717
18class Person:18class Person:
1919
20    def __init__(self, position: tuple):20    def __init__(self, position: tuple):
21        self.position = position21        self.position = position
2222
23    def get_position(self):23    def get_position(self):
24        return self.position24        return self.position
25    25    
26    def set_position(self, new_position: tuple):26    def set_position(self, new_position: tuple):
27        self.position = new_position        27        self.position = new_position        
2828
2929
30class Kid(Person):30class Kid(Person):
3131
32    def __init__(self, position: tuple, initiative: int):32    def __init__(self, position: tuple, initiative: int):
33        super().__init__(position)33        super().__init__(position)
34        self.initiative = initiative34        self.initiative = initiative
35        self.candy_basket = []35        self.candy_basket = []
36        self.hosts_visited = set()36        self.hosts_visited = set()
3737
38    def get_initiative(self):38    def get_initiative(self):
39        return self.initiative39        return self.initiative
40    40    
41    def add_candy(self, candy: Candy):41    def add_candy(self, candy: Candy):
42        self.candy_basket.append(candy)42        self.candy_basket.append(candy)
4343
44    def is_critical(self):44    def is_critical(self):
45        current_uranium = 045        current_uranium = 0
46        candy: Candy46        candy: Candy
47        for candy in self.candy_basket:47        for candy in self.candy_basket:
48            current_uranium += candy.get_uranium_quantity()    48            current_uranium += candy.get_uranium_quantity()    
4949
50        if current_uranium > 20:50        if current_uranium > 20:
51            return True51            return True
52        return False52        return False
53    53    
54    def get_hosts_visited(self):54    def get_hosts_visited(self):
55        return self.hosts_visited55        return self.hosts_visited
56    56    
57    def visit_host(self, host):57    def visit_host(self, host):
58        self.hosts_visited.add(host)58        self.hosts_visited.add(host)
59    59    
6060
61class Host(Person):61class Host(Person):
6262
63    def __init__(self, position: tuple, candies: list):63    def __init__(self, position: tuple, candies: list):
64        super().__init__(position)64        super().__init__(position)
65        self.candies = []65        self.candies = []
66        for candy_tuple in candies:66        for candy_tuple in candies:
67            self.candies.append(Candy(candy_tuple[0], candy_tuple[1]))67            self.candies.append(Candy(candy_tuple[0], candy_tuple[1]))
6868
69    def remove_candy(self, filter_func):69    def remove_candy(self, filter_func):
70        if not self.candies:70        if not self.candies:
71            return None71            return None
7272
73        to_remove = filter_func(self.candies)73        to_remove = filter_func(self.candies)
74        if to_remove is None:74        if to_remove is None:
75            return None75            return None
76        self.candies.remove(to_remove)76        self.candies.remove(to_remove)
7777
78        return to_remove78        return to_remove
79    79    
8080
81class FluxCapacitor:81class FluxCapacitor:
8282
83    def __init__(self, participants: set):83    def __init__(self, participants: set):
8484
85        self.kids = []85        self.kids = []
86        self.hosts = []86        self.hosts = []
87        person: Person87        person: Person
88        for person in participants:88        for person in participants:
89            if type(person) == Kid:89            if type(person) == Kid:
90                self.kids.append(person)90                self.kids.append(person)
91            else:91            else:
92                self.hosts.append(person)92                self.hosts.append(person)
9393
94        self.kids.sort(key=lambda x: x.initiative, reverse=True)94        self.kids.sort(key=lambda x: x.initiative, reverse=True)
9595
96    def get_victim(self):96    def get_victim(self):
97        victims = set()97        victims = set()
9898
99        if len(self.kids) == 0:99        if len(self.kids) == 0:
100            return None100            return None
101101
102        loop = True102        loop = True
103103
104        while loop:104        while loop:
105105
106            kid: Kid106            kid: Kid
107            for kid in self.kids:107            for kid in self.kids:
108                closest_host = self._find_closest_host_to_kid(kid)108                closest_host = self._find_closest_host_to_kid(kid)
109                if closest_host is None:109                if closest_host is None:
110                    loop = False110                    loop = False
111                    break111                    break
112                112                
113                given = closest_host.remove_candy(self.get_biggest_candy)113                given = closest_host.remove_candy(self.get_biggest_candy)
114                if given is not None:114                if given is not None:
115                    kid.add_candy(given)115                    kid.add_candy(given)
116                116                
117                if kid.is_critical():117                if kid.is_critical():
118                    victims.add(kid)118                    victims.add(kid)
119                    loop = False119                    loop = False
120120
121                kid.visit_host(closest_host)121                kid.visit_host(closest_host)
122                kid.set_position(closest_host.get_position())122                kid.set_position(closest_host.get_position())
123123
124            124            
125        if victims:125        if victims:
126            return victims126            return victims
127        return None    127        return None    
128    128    
129    def _find_closest_host_to_kid(self, kid: Kid):129    def _find_closest_host_to_kid(self, kid: Kid):
130        current_closest_dist = -1130        current_closest_dist = -1
131        closest_host = None131        closest_host = None
132132
133        host: Host133        host: Host
134        for host in self.hosts:134        for host in self.hosts:
135            curr_dist = math.dist(kid.get_position(), host.get_position())135            curr_dist = math.dist(kid.get_position(), host.get_position())
136            if host in kid.get_hosts_visited():136            if host in kid.get_hosts_visited():
137                continue137                continue
138138
139            if current_closest_dist < 0 or curr_dist < current_closest_dist:139            if current_closest_dist < 0 or curr_dist < current_closest_dist:
140                closest_host = host140                closest_host = host
141                current_closest_dist = curr_dist141                current_closest_dist = curr_dist
142            elif curr_dist == current_closest_dist and host.get_position() < closest_host.get_position():142            elif curr_dist == current_closest_dist and host.get_position() < closest_host.get_position():
143                closest_host = host143                closest_host = host
144                current_closest_dist = curr_dist144                current_closest_dist = curr_dist
145            145            
146        return closest_host146        return closest_host
147147
148    def get_biggest_candy(self, host_basket: set):148    def get_biggest_candy(self, host_basket: set):
149        biggest = None149        biggest = None
150150
151        candy: Candy151        candy: Candy
152        for candy in host_basket:152        for candy in host_basket:
153            if biggest == None or biggest.get_mass() < candy.get_mass():153            if biggest == None or biggest.get_mass() < candy.get_mass():
154                biggest = candy154                biggest = candy
155155
156        return biggest156        return biggest
157157
158    def get_kids(self):158    def get_kids(self):
159        return self.kids159        return self.kids
160    160    
161161
162def get_biggest_candy(candies: list):162def get_biggest_candy(candies: list):
163        biggest = None163        biggest = None
164164
165        candy: Candy165        candy: Candy
166        for candy in candies:166        for candy in candies:
167            if biggest == None or biggest.get_mass() < candy.get_mass():167            if biggest == None or biggest.get_mass() < candy.get_mass():
168                biggest = candy168                biggest = candy
169169
170        return biggest170        return biggest
t171 t
172 
173kid1 = Kid((1, 2), 3)
174kid2 = Kid((3, 4), 2)
175host1 = Host((2, 2), [(5, 0.1), (100, 0.2)])
176host2 = Host((4, 4), [(8, 0.3)])
177flux_capacitor = FluxCapacitor(participants={kid1, kid2, host1, host2})
178 
179victims = flux_capacitor.get_victim()
180#print(len(victims))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op