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

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

10 точки общо

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

 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
20    def get_position(self):
21        return self.position
22
23    def set_position(self, new_position):
24        self.position = new_position
25
26
27class Kid(Person):
28    def __init__(self, position, initiative):
29        super().__init__(position)
30        self.initiative = initiative
31        self.candies = []
32
33    def get_initiative(self):
34        return self.initiative
35
36    def add_candy(self, candy):
37        self.candies.append(candy)
38
39    def is_critical(self):
40        uranium_quantity = sum(candy.get_uranium_quantity() for candy in self.candies)
41        return uranium_quantity > 20
42
43
44class Host(Person):
45    def __init__(self, position, candies):
46        super().__init__(position)
47        self.candies = [Candy(mass, uranium) for mass, uranium in candies]
48    
49    def remove_candy(self, choose_candy):
50        if not self.candies:
51            return None
52        candy_to_remove = choose_candy(self.candies)
53        self.candies.remove(candy_to_remove)
54        return candy_to_remove
55
56
57class FluxCapacitor:
58    def __init__(self, participants):
59        self.participants = participants
60    
61    def get_victim(self):
62        kids = [participant for participant in self.participants if isinstance(participant, Kid)]
63        hosts = [participant for participant in self.participants if isinstance(participant, Host)]
64        critical_kids = set()
65        while hosts and kids:
66            for kid in sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True):
67                min_distance = float('inf')
68                closest_host = None
69                for host in hosts:
70                    distance = math.sqrt((host.get_position()[0] - kid.get_position()[0])**2 + (host.get_position()[1] - kid.get_position()[1])**2)
71                    if distance < min_distance:
72                        min_distance = distance
73                        closest_host = host
74                if closest_host:
75                    selected_candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda candy: candy.get_mass()))
76                    if selected_candy:
77                        kid.add_candy(selected_candy)
78                        kid.set_position(closest_host.get_position())
79                        if kid.is_critical():
80                            critical_kids.add(kid)
81                    hosts.remove(closest_host)
82            if not hosts:
83                break  
84        return critical_kids if critical_kids else None

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

OK

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

f1import mathf1import math
nn2 
3 
2class Candy:4class Candy:
3    def __init__(self, mass, uranium):5    def __init__(self, mass, uranium):
4        self.mass = mass6        self.mass = mass
5        self.uranium = uranium7        self.uranium = uranium
68
7    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
8        return self.mass * self.uranium10        return self.mass * self.uranium
911
10    def get_mass(self):12    def get_mass(self):
11        return self.mass13        return self.mass
1214
1315
14class Person:16class Person:
15    def __init__(self, position):17    def __init__(self, position):
16        self.position = position18        self.position = position
1719
18    def get_position(self):20    def get_position(self):
19        return self.position21        return self.position
2022
21    def set_position(self, new_position):23    def set_position(self, new_position):
22        self.position = new_position24        self.position = new_position
2325
2426
25class Kid(Person):27class Kid(Person):
26    def __init__(self, position, initiative):28    def __init__(self, position, initiative):
27        super().__init__(position)29        super().__init__(position)
28        self.initiative = initiative30        self.initiative = initiative
29        self.candies = []31        self.candies = []
3032
31    def get_initiative(self):33    def get_initiative(self):
32        return self.initiative34        return self.initiative
3335
34    def add_candy(self, candy):36    def add_candy(self, candy):
35        self.candies.append(candy)37        self.candies.append(candy)
3638
37    def is_critical(self):39    def is_critical(self):
38        uranium_quantity = sum(candy.get_uranium_quantity() for candy in self.candies)40        uranium_quantity = sum(candy.get_uranium_quantity() for candy in self.candies)
39        return uranium_quantity > 2041        return uranium_quantity > 20
4042
4143
42class Host(Person):44class Host(Person):
43    def __init__(self, position, candies):45    def __init__(self, position, candies):
44        super().__init__(position)46        super().__init__(position)
45        self.candies = [Candy(mass, uranium) for mass, uranium in candies]47        self.candies = [Candy(mass, uranium) for mass, uranium in candies]
46    48    
47    def remove_candy(self, choose_candy):49    def remove_candy(self, choose_candy):
48        if not self.candies:50        if not self.candies:
49            return None51            return None
50        candy_to_remove = choose_candy(self.candies)52        candy_to_remove = choose_candy(self.candies)
51        self.candies.remove(candy_to_remove)53        self.candies.remove(candy_to_remove)
52        return candy_to_remove54        return candy_to_remove
5355
5456
55class FluxCapacitor:57class FluxCapacitor:
56    def __init__(self, participants):58    def __init__(self, participants):
57        self.participants = participants59        self.participants = participants
58    60    
59    def get_victim(self):61    def get_victim(self):
60        kids = [participant for participant in self.participants if isinstance(participant, Kid)]62        kids = [participant for participant in self.participants if isinstance(participant, Kid)]
61        hosts = [participant for participant in self.participants if isinstance(participant, Host)]63        hosts = [participant for participant in self.participants if isinstance(participant, Host)]
62        critical_kids = set()64        critical_kids = set()
63        while hosts and kids:65        while hosts and kids:
64            for kid in sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True):66            for kid in sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True):
65                min_distance = float('inf')67                min_distance = float('inf')
66                closest_host = None68                closest_host = None
67                for host in hosts:69                for host in hosts:
68                    distance = math.sqrt((host.get_position()[0] - kid.get_position()[0])**2 + (host.get_position()[1] - kid.get_position()[1])**2)70                    distance = math.sqrt((host.get_position()[0] - kid.get_position()[0])**2 + (host.get_position()[1] - kid.get_position()[1])**2)
69                    if distance < min_distance:71                    if distance < min_distance:
70                        min_distance = distance72                        min_distance = distance
71                        closest_host = host73                        closest_host = host
72                if closest_host:74                if closest_host:
73                    selected_candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda candy: candy.get_mass()))75                    selected_candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda candy: candy.get_mass()))
74                    if selected_candy:76                    if selected_candy:
75                        kid.add_candy(selected_candy)77                        kid.add_candy(selected_candy)
76                        kid.set_position(closest_host.get_position())78                        kid.set_position(closest_host.get_position())
77                        if kid.is_critical():79                        if kid.is_critical():
78                            critical_kids.add(kid)80                            critical_kids.add(kid)
79                    hosts.remove(closest_host)81                    hosts.remove(closest_host)
n80            if len(hosts) == 0:n82            if not hosts:
81                break  83                break  
t82        if critical_kids:t84        return critical_kids if critical_kids else None
83            return critical_kids
84        return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op