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

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

10 точки общо

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

  1import math
  2
  3
  4class Candy:
  5
  6    def __init__(self, mass: int, uranium: int):
  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    CRITICAL_MASS = 20
 31
 32    def __init__(self, position: tuple, initiative: int):
 33        self.position = position
 34        self.initiative = initiative
 35        self.candies = []
 36        self.visited = set()
 37
 38    def get_initiative(self):
 39        return self.initiative
 40
 41    def add_candy(self, candy: Candy):
 42        self.candies.append(candy)
 43
 44    def is_critical(self):
 45        uranium_mass = 0
 46        for candy in self.candies:
 47            uranium_mass += candy.get_uranium_quantity()
 48
 49        return uranium_mass > self.CRITICAL_MASS
 50
 51
 52class Host(Person):
 53
 54    def __init__(self, position: tuple, candies: list):
 55        self.candies = []
 56        for candy in candies:
 57            self.candies.append(Candy(candy[0], candy[1]))
 58        self.position = position
 59
 60    def remove_candy(self, condition):
 61        if len(self.candies) == 0:
 62            return None
 63        candy = condition(self.candies)
 64        self.candies.remove(candy)
 65        return candy
 66
 67
 68class FluxCapacitor:
 69
 70    def __init__(self, participants: set):
 71        self.kids = []
 72        self.hosts = []
 73
 74        for person in participants:
 75            if type(person) is Kid:
 76                self.kids.append(person)
 77            else:
 78                self.hosts.append(person)
 79        self.kids.sort(reverse=True, key=lambda kid: kid.get_initiative())
 80
 81    def get_victim(self):
 82        dead_kids = set()
 83        if len(self.kids) == 0:
 84            return None
 85
 86        while True:
 87            for kid in self.kids:
 88                closest_host = None
 89                distance = float('inf')
 90                for host in self.hosts:
 91                    if host in kid.visited:
 92                        continue
 93                    new_distance = math.dist(kid.get_position(), host.get_position())
 94                    if distance > new_distance:
 95                        distance = new_distance
 96                        closest_host = host
 97                    elif distance == new_distance:
 98                        if closest_host.get_position() > host.get_position():
 99                            closest_host = host
100
101                if closest_host is None:
102                    return None
103                taken_candy = closest_host.remove_candy(determine_max_mass)
104
105                if taken_candy is not None:
106                    kid.add_candy(taken_candy)
107                kid.visited.add(closest_host)
108
109                if kid.is_critical():
110                    dead_kids.add(kid)
111
112            if dead_kids:
113                return dead_kids
114
115
116def determine_max_mass(candies: list):
117    max = 0
118    for candy in candies:
119        if max < candy.get_mass():
120            max = candy.get_mass()
121            max_candy = candy
122
123    return max_candy

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

OK

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

f1import mathf1import math
22
33
4class Candy:4class Candy:
55
6    def __init__(self, mass: int, uranium: int):6    def __init__(self, mass: int, uranium: int):
7        self.mass = mass7        self.mass = mass
8        self.uranium = uranium8        self.uranium = uranium
99
10    def get_uranium_quantity(self):10    def get_uranium_quantity(self):
11        return (self.mass * self.uranium)11        return (self.mass * self.uranium)
1212
13    def get_mass(self):13    def get_mass(self):
14        return self.mass14        return self.mass
1515
1616
17class Person:17class Person:
1818
19    def __init__(self, position: tuple):19    def __init__(self, position: tuple):
20        self.position = position20        self.position = position
2121
22    def get_position(self):22    def get_position(self):
23        return self.position23        return self.position
2424
25    def set_position(self, new_position: tuple):25    def set_position(self, new_position: tuple):
26        self.position = new_position26        self.position = new_position
2727
2828
29class Kid(Person):29class Kid(Person):
nn30    CRITICAL_MASS = 20
3031
31    def __init__(self, position: tuple, initiative: int):32    def __init__(self, position: tuple, initiative: int):
32        self.position = position33        self.position = position
33        self.initiative = initiative34        self.initiative = initiative
34        self.candies = []35        self.candies = []
35        self.visited = set()36        self.visited = set()
3637
37    def get_initiative(self):38    def get_initiative(self):
38        return self.initiative39        return self.initiative
3940
40    def add_candy(self, candy: Candy):41    def add_candy(self, candy: Candy):
41        self.candies.append(candy)42        self.candies.append(candy)
4243
43    def is_critical(self):44    def is_critical(self):
44        uranium_mass = 045        uranium_mass = 0
45        for candy in self.candies:46        for candy in self.candies:
46            uranium_mass += candy.get_uranium_quantity()47            uranium_mass += candy.get_uranium_quantity()
4748
n48        if uranium_mass > 20:n49        return uranium_mass > self.CRITICAL_MASS
49            return True
50        else:
51            return False
5250
5351
54class Host(Person):52class Host(Person):
5553
56    def __init__(self, position: tuple, candies: list):54    def __init__(self, position: tuple, candies: list):
57        self.candies = []55        self.candies = []
58        for candy in candies:56        for candy in candies:
59            self.candies.append(Candy(candy[0], candy[1]))57            self.candies.append(Candy(candy[0], candy[1]))
60        self.position = position58        self.position = position
6159
62    def remove_candy(self, condition):60    def remove_candy(self, condition):
63        if len(self.candies) == 0:61        if len(self.candies) == 0:
64            return None62            return None
65        candy = condition(self.candies)63        candy = condition(self.candies)
66        self.candies.remove(candy)64        self.candies.remove(candy)
67        return candy65        return candy
6866
6967
70class FluxCapacitor:68class FluxCapacitor:
7169
72    def __init__(self, participants: set):70    def __init__(self, participants: set):
73        self.kids = []71        self.kids = []
74        self.hosts = []72        self.hosts = []
7573
76        for person in participants:74        for person in participants:
77            if type(person) is Kid:75            if type(person) is Kid:
78                self.kids.append(person)76                self.kids.append(person)
79            else:77            else:
80                self.hosts.append(person)78                self.hosts.append(person)
81        self.kids.sort(reverse=True, key=lambda kid: kid.get_initiative())79        self.kids.sort(reverse=True, key=lambda kid: kid.get_initiative())
8280
83    def get_victim(self):81    def get_victim(self):
84        dead_kids = set()82        dead_kids = set()
85        if len(self.kids) == 0:83        if len(self.kids) == 0:
86            return None84            return None
8785
88        while True:86        while True:
n89            kid: Kidn
90            for kid in self.kids:87            for kid in self.kids:
91                closest_host = None88                closest_host = None
92                distance = float('inf')89                distance = float('inf')
n93                host: Hostn
94                for host in self.hosts:90                for host in self.hosts:
95                    if host in kid.visited:91                    if host in kid.visited:
96                        continue92                        continue
97                    new_distance = math.dist(kid.get_position(), host.get_position())93                    new_distance = math.dist(kid.get_position(), host.get_position())
98                    if distance > new_distance:94                    if distance > new_distance:
99                        distance = new_distance95                        distance = new_distance
100                        closest_host = host96                        closest_host = host
101                    elif distance == new_distance:97                    elif distance == new_distance:
102                        if closest_host.get_position() > host.get_position():98                        if closest_host.get_position() > host.get_position():
103                            closest_host = host99                            closest_host = host
104100
105                if closest_host is None:101                if closest_host is None:
106                    return None102                    return None
107                taken_candy = closest_host.remove_candy(determine_max_mass)103                taken_candy = closest_host.remove_candy(determine_max_mass)
108104
109                if taken_candy is not None:105                if taken_candy is not None:
110                    kid.add_candy(taken_candy)106                    kid.add_candy(taken_candy)
111                kid.visited.add(closest_host)107                kid.visited.add(closest_host)
112108
113                if kid.is_critical():109                if kid.is_critical():
114                    dead_kids.add(kid)110                    dead_kids.add(kid)
115111
n116            if len(dead_kids) != 0:n112            if dead_kids:
117                return dead_kids113                return dead_kids
118114
119115
120def determine_max_mass(candies: list):116def determine_max_mass(candies: list):
121    max = 0117    max = 0
t122    candy: Candyt
123    for candy in candies:118    for candy in candies:
124        if max < candy.get_mass():119        if max < candy.get_mass():
125            max = candy.get_mass()120            max = candy.get_mass()
126            max_candy = candy121            max_candy = candy
127122
128    return max_candy123    return max_candy
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op