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

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

10 точки общо

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

  1class Basket:
  2    def __init__(self):
  3        self.candies = []
  4        self.candy_givers = []
  5    
  6    def add_candy(self, candy):
  7        self.candies.append(candy)
  8
  9    def add_giver(self, giver):
 10        self.candy_givers.append(giver)
 11
 12    def remove_candy(self, candy):
 13        self.candies.remove(candy)
 14
 15    def get_candies(self):
 16        return self.candies
 17    
 18    def fill(self, candies):
 19        self.candies = candies
 20
 21
 22class Candy:
 23    def __init__(self, mass, uranium):
 24        self.mass = mass
 25        self.uranium = uranium
 26    
 27    def get_uranium_quantity(self):
 28        return self.mass * self.uranium
 29    
 30    def get_mass(self):
 31        return self.mass
 32
 33
 34class Person:
 35    def __init__(self, position):
 36        self.position = position
 37        self.basket = Basket()
 38
 39    def get_position(self):
 40        return self.position
 41    
 42    def set_position(self, new_position):
 43        self.position = new_position
 44    
 45    def get_distance(self, other_person):
 46        return (self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2
 47
 48
 49class Kid(Person):
 50    MIN_URANIUM_TO_UNLOCK_POWER = 20
 51
 52    def __init__(self, position, initiative):
 53        super().__init__(position)
 54        self.initiative = initiative
 55
 56    def get_initiative(self):
 57        return self.initiative
 58    
 59    def add_candy(self, candy):
 60        self.basket.add_candy(candy)
 61    
 62    def is_critical(self):
 63        return sum(candy.get_uranium_quantity() for candy in self.basket.get_candies()) > self.MIN_URANIUM_TO_UNLOCK_POWER
 64    
 65    def visit_closest_host(self, hosts):
 66        min_distance = None
 67        closest_host = None
 68        
 69        for host in hosts:
 70            distance = self.get_distance(host)
 71            host_position = host.get_position()
 72            closest_position = closest_host.get_position() if closest_host else None
 73
 74            if host in self.basket.candy_givers:
 75                continue
 76
 77            if (
 78                min_distance is None or
 79                (distance, host_position[0], host_position[1]) < (min_distance, closest_position[0], closest_position[1])
 80            ):
 81                min_distance = distance
 82                closest_host = host
 83
 84        return closest_host
 85    
 86
 87class Host(Person):
 88    def __init__(self, position, candies):
 89        super().__init__(position)
 90        self.basket.fill([Candy(mass, uranium) for mass, uranium in candies])
 91
 92    def remove_candy(self, candy_picker):
 93        if not len(self.basket.get_candies()):
 94            return
 95        
 96        picked_candy = candy_picker(self.basket.get_candies())
 97        self.basket.remove_candy(picked_candy)
 98
 99        return picked_candy
100    
101    def give_candy(self, kids, candy_picker):
102        kids = sorted(kids, key=lambda kid: kid.initiative, reverse=True)
103        for kid in kids:
104            picked_candy = self.remove_candy(candy_picker)
105            kid.basket.add_giver(self)
106            if picked_candy:
107                kid.add_candy(picked_candy)
108
109
110class FluxCapacitor:
111    def __init__(self, participants):
112        self.participants = participants
113
114    def get_victim(self):
115        kids = [participant for participant in self.participants if type(participant) is Kid]
116        hosts = [participant for participant in self.participants if type(participant) is Host]
117        kids_who_glow_in_the_dark = set()
118
119        while not len(kids_who_glow_in_the_dark):
120            visitings = []
121
122            for kid in kids:
123                closest_host = kid.visit_closest_host(hosts)
124                if closest_host:
125                    visitings.append((kid, closest_host))
126                    kid.set_position(closest_host.get_position())
127
128            if not len(visitings):
129                return
130            
131            for host in hosts:
132                host.give_candy(
133                    (kid_host_pair[0] for kid_host_pair in visitings if kid_host_pair[1] == host),
134                    self.get_biggest_mass_candy
135                )
136
137            kids_who_glow_in_the_dark = {kid for kid in kids if kid.is_critical()}
138
139        return kids_who_glow_in_the_dark
140
141    @staticmethod
142    def get_biggest_mass_candy(candies):
143        return max(candies, key=lambda candy: candy.get_mass())

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

OK

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

n1class Bascket:n1class Basket:
2    def __init__(self):2    def __init__(self):
3        self.candies = []3        self.candies = []
4        self.candy_givers = []4        self.candy_givers = []
5    5    
6    def add_candy(self, candy):6    def add_candy(self, candy):
7        self.candies.append(candy)7        self.candies.append(candy)
88
9    def add_giver(self, giver):9    def add_giver(self, giver):
10        self.candy_givers.append(giver)10        self.candy_givers.append(giver)
1111
12    def remove_candy(self, candy):12    def remove_candy(self, candy):
13        self.candies.remove(candy)13        self.candies.remove(candy)
1414
15    def get_candies(self):15    def get_candies(self):
16        return self.candies16        return self.candies
17    17    
18    def fill(self, candies):18    def fill(self, candies):
19        self.candies = candies19        self.candies = candies
2020
2121
22class Candy:22class Candy:
23    def __init__(self, mass, uranium):23    def __init__(self, mass, uranium):
24        self.mass = mass24        self.mass = mass
25        self.uranium = uranium25        self.uranium = uranium
26    26    
27    def get_uranium_quantity(self):27    def get_uranium_quantity(self):
28        return self.mass * self.uranium28        return self.mass * self.uranium
29    29    
30    def get_mass(self):30    def get_mass(self):
31        return self.mass31        return self.mass
3232
3333
34class Person:34class Person:
35    def __init__(self, position):35    def __init__(self, position):
36        self.position = position36        self.position = position
n37        self.bascket = Bascket()n37        self.basket = Basket()
3838
39    def get_position(self):39    def get_position(self):
40        return self.position40        return self.position
41    41    
42    def set_position(self, new_position):42    def set_position(self, new_position):
43        self.position = new_position43        self.position = new_position
44    44    
45    def get_distance(self, other_person):45    def get_distance(self, other_person):
46        return (self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 246        return (self.position[0] - other_person.position[0]) ** 2 + (self.position[1] - other_person.position[1]) ** 2
4747
4848
49class Kid(Person):49class Kid(Person):
nn50    MIN_URANIUM_TO_UNLOCK_POWER = 20
51 
50    def __init__(self, position, initiative):52    def __init__(self, position, initiative):
51        super().__init__(position)53        super().__init__(position)
52        self.initiative = initiative54        self.initiative = initiative
5355
54    def get_initiative(self):56    def get_initiative(self):
55        return self.initiative57        return self.initiative
56    58    
57    def add_candy(self, candy):59    def add_candy(self, candy):
n58        self.bascket.add_candy(candy)n60        self.basket.add_candy(candy)
59    61    
60    def is_critical(self):62    def is_critical(self):
n61        return sum(candy.get_uranium_quantity() for candy in self.bascket.get_candies()) > 20n63        return sum(candy.get_uranium_quantity() for candy in self.basket.get_candies()) > self.MIN_URANIUM_TO_UNLOCK_POWER
62    64    
63    def visit_closest_host(self, hosts):65    def visit_closest_host(self, hosts):
64        min_distance = None66        min_distance = None
65        closest_host = None67        closest_host = None
nn68        
66        for host in hosts:69        for host in hosts:
67            distance = self.get_distance(host)70            distance = self.get_distance(host)
68            host_position = host.get_position()71            host_position = host.get_position()
nn72            closest_position = closest_host.get_position() if closest_host else None
73 
69            if host in self.bascket.candy_givers:74            if host in self.basket.candy_givers:
70                continue75                continue
nn76 
71            if (77            if (
72                min_distance is None or78                min_distance is None or
n73                distance < min_distance or n79                (distance, host_position[0], host_position[1]) < (min_distance, closest_position[0], closest_position[1])
74                distance == min_distance and (
75                    host_position[0] < closest_host.get_position()[0] or 
76                    host_position[1] < closest_host.get_position()[1]
77                )
78            ):80            ):
79                min_distance = distance81                min_distance = distance
80                closest_host = host82                closest_host = host
8183
82        return closest_host84        return closest_host
83    85    
8486
85class Host(Person):87class Host(Person):
86    def __init__(self, position, candies):88    def __init__(self, position, candies):
87        super().__init__(position)89        super().__init__(position)
n88        self.bascket.fill([Candy(mass, uranium) for mass, uranium in candies])n90        self.basket.fill([Candy(mass, uranium) for mass, uranium in candies])
8991
90    def remove_candy(self, candy_picker):92    def remove_candy(self, candy_picker):
n91        if not len(self.bascket.get_candies()):n93        if not len(self.basket.get_candies()):
92            return94            return
nn95        
93        picked_candy = candy_picker(self.bascket.get_candies())96        picked_candy = candy_picker(self.basket.get_candies())
94        self.bascket.remove_candy(picked_candy)97        self.basket.remove_candy(picked_candy)
98 
95        return picked_candy99        return picked_candy
96    100    
97    def give_candy(self, kids, candy_picker):101    def give_candy(self, kids, candy_picker):
98        kids = sorted(kids, key=lambda kid: kid.initiative, reverse=True)102        kids = sorted(kids, key=lambda kid: kid.initiative, reverse=True)
99        for kid in kids:103        for kid in kids:
100            picked_candy = self.remove_candy(candy_picker)104            picked_candy = self.remove_candy(candy_picker)
n101            kid.bascket.add_giver(self)n105            kid.basket.add_giver(self)
102            if picked_candy:106            if picked_candy:
103                kid.add_candy(picked_candy)107                kid.add_candy(picked_candy)
104108
105109
106class FluxCapacitor:110class FluxCapacitor:
107    def __init__(self, participants):111    def __init__(self, participants):
108        self.participants = participants112        self.participants = participants
109113
110    def get_victim(self):114    def get_victim(self):
n111        kids = [participant for participant in self.participants if type(participant) == Kid]n115        kids = [participant for participant in self.participants if type(participant) is Kid]
112        hosts = [participant for participant in self.participants if type(participant) == Host]116        hosts = [participant for participant in self.participants if type(participant) is Host]
113        kids_who_glow_in_the_dark = set()117        kids_who_glow_in_the_dark = set()
114118
115        while not len(kids_who_glow_in_the_dark):119        while not len(kids_who_glow_in_the_dark):
116            visitings = []120            visitings = []
nn121 
117            for kid in kids:122            for kid in kids:
118                closest_host = kid.visit_closest_host(hosts)123                closest_host = kid.visit_closest_host(hosts)
119                if closest_host:124                if closest_host:
120                    visitings.append((kid, closest_host))125                    visitings.append((kid, closest_host))
121                    kid.set_position(closest_host.get_position())126                    kid.set_position(closest_host.get_position())
nn127 
122            if not len(visitings):128            if not len(visitings):
123                return129                return
nn130            
124            for host in hosts:131            for host in hosts:
125                host.give_candy(132                host.give_candy(
126                    (kid_host_pair[0] for kid_host_pair in visitings if kid_host_pair[1] == host),133                    (kid_host_pair[0] for kid_host_pair in visitings if kid_host_pair[1] == host),
n127                    get_biggest_mass_candyn134                    self.get_biggest_mass_candy
128                )135                )
nn136 
129            kids_who_glow_in_the_dark = {kid for kid in kids if kid.is_critical()}137            kids_who_glow_in_the_dark = {kid for kid in kids if kid.is_critical()}
130138
131        return kids_who_glow_in_the_dark139        return kids_who_glow_in_the_dark
132140
t133    t141    @staticmethod
134def get_biggest_mass_candy(candies):142    def get_biggest_mass_candy(candies):
135    return max(candies, key=lambda candy: candy.get_mass())143        return max(candies, key=lambda candy: candy.get_mass())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op