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

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

10 точки общо

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

  1from math import sqrt
  2
  3
  4class Candy:
  5
  6    def __init__(self, mass, uranium):
  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):
 20        self.position = position
 21
 22    def get_position(self):
 23        return self.position
 24    
 25    def set_position(self, new_position):
 26        self.position = new_position
 27
 28
 29class Kid(Person):
 30    
 31
 32    def __init__(self, position, initiative):
 33        super().__init__(position)
 34        self.initiative = initiative
 35        self.basket = []
 36        self.CRITICAL_LEVEL_URANIUM = 20
 37
 38    def get_initiative(self):
 39        return self.initiative
 40    
 41    def add_candy(self, candy):
 42        print(candy.mass, candy.uranium)
 43        self.basket.append(candy)
 44
 45    def is_critical(self):
 46        return sum(candy.get_uranium_quantity() for candy in self.basket) > self.CRITICAL_LEVEL_URANIUM
 47
 48
 49class Host(Person):
 50    def __init__(self, position, candies):
 51        super().__init__(position)
 52        self.candies = [Candy(mass, uranium) for mass,uranium in candies]
 53
 54    def remove_candy(self, func):
 55        if not self.candies:
 56            return None
 57
 58        selected_candy = func(self.candies)
 59        self.candies.remove(selected_candy)
 60        return selected_candy
 61
 62
 63class FluxCapacitor:
 64    def __init__(self, participants):
 65        self.kids = []
 66        self.hosts = []
 67
 68        for participant in participants: 
 69            if type(participant) == Kid:
 70                self.kids.append(participant)
 71            else:
 72                self.hosts.append(participant)
 73        
 74        self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True)            
 75
 76    def get_victim(self):
 77        critical_kids = set()
 78        closest_host = None
 79        round = 0
 80        visited = [[False for _ in self.kids] for _ in self.hosts]
 81    
 82        while len(critical_kids) == 0 and round < len(self.hosts):
 83            for kid in self.kids:
 84                min_distance = float('inf')
 85                same_distance_hosts = []
 86                for host in self.hosts:
 87                    if visited[self.hosts.index(host)][self.kids.index(kid)]:
 88                        continue
 89                    distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2)
 90                    if distance < min_distance:
 91                        min_distance = distance
 92                        closest_host = host
 93                        same_distance_hosts = [host]
 94                    elif distance == min_distance:
 95                        same_distance_hosts.append(host)
 96                    if len(same_distance_hosts) > 1:
 97                        closest_host = min(same_distance_hosts, key=lambda host: host.get_position())
 98                
 99                visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True
100                kid.set_position(closest_host.get_position())
101
102                if len(closest_host.candies) != 0: 
103                    kid.add_candy(closest_host.remove_candy(FluxCapacitor.find_max_element_in_list_by_criteria))
104
105                if kid.is_critical():
106                    critical_kids.add(kid)
107            round += 1
108
109        return critical_kids or None
110    
111
112    def find_max_element_in_list_by_criteria(list):
113        return max(list, key=lambda candy: candy.get_mass())

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

OK

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

f1from math import sqrtf1from math import sqrt
nn2 
23
3class Candy:4class Candy:
45
5    def __init__(self, mass, uranium):6    def __init__(self, mass, uranium):
6        self.mass = mass7        self.mass = mass
7        self.uranium = uranium8        self.uranium = uranium
89
9    def get_uranium_quantity(self):10    def get_uranium_quantity(self):
10        return self.mass * self.uranium11        return self.mass * self.uranium
11    12    
12    def get_mass(self):13    def get_mass(self):
13        return self.mass14        return self.mass
1415
nn16 
15class Person:17class Person:
1618
17    def __init__(self, position):19    def __init__(self, position):
18        self.position = position20        self.position = position
1921
20    def get_position(self):22    def get_position(self):
21        return self.position23        return self.position
22    24    
23    def set_position(self, new_position):25    def set_position(self, new_position):
24        self.position = new_position26        self.position = new_position
2527
nn28 
26class Kid(Person):29class Kid(Person):
27    30    
nn31 
28    def __init__(self, position, initiative):32    def __init__(self, position, initiative):
29        super().__init__(position)33        super().__init__(position)
30        self.initiative = initiative34        self.initiative = initiative
31        self.basket = []35        self.basket = []
nn36        self.CRITICAL_LEVEL_URANIUM = 20
3237
33    def get_initiative(self):38    def get_initiative(self):
34        return self.initiative39        return self.initiative
35    40    
36    def add_candy(self, candy):41    def add_candy(self, candy):
37        print(candy.mass, candy.uranium)42        print(candy.mass, candy.uranium)
38        self.basket.append(candy)43        self.basket.append(candy)
3944
40    def is_critical(self):45    def is_critical(self):
n41        return sum(candy.get_uranium_quantity() for candy in self.basket) > 20n46        return sum(candy.get_uranium_quantity() for candy in self.basket) > self.CRITICAL_LEVEL_URANIUM
47 
4248
43class Host(Person):49class Host(Person):
44    def __init__(self, position, candies):50    def __init__(self, position, candies):
45        super().__init__(position)51        super().__init__(position)
46        self.candies = [Candy(mass, uranium) for mass,uranium in candies]52        self.candies = [Candy(mass, uranium) for mass,uranium in candies]
4753
48    def remove_candy(self, func):54    def remove_candy(self, func):
49        if not self.candies:55        if not self.candies:
50            return None56            return None
5157
52        selected_candy = func(self.candies)58        selected_candy = func(self.candies)
53        self.candies.remove(selected_candy)59        self.candies.remove(selected_candy)
54        return selected_candy60        return selected_candy
5561
5662
57class FluxCapacitor:63class FluxCapacitor:
58    def __init__(self, participants):64    def __init__(self, participants):
59        self.kids = []65        self.kids = []
60        self.hosts = []66        self.hosts = []
6167
62        for participant in participants: 68        for participant in participants: 
63            if type(participant) == Kid:69            if type(participant) == Kid:
64                self.kids.append(participant)70                self.kids.append(participant)
nn71            else:
72                self.hosts.append(participant)
65        73        
n66        self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True)n74        self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True)            
67        
68        for participant in participants: 
69            if type(participant) == Host:
70                self.hosts.append(participant)
7175
72    def get_victim(self):76    def get_victim(self):
73        critical_kids = set()77        critical_kids = set()
74        closest_host = None78        closest_host = None
n75        same_distance_hosts = []n
76        round = 079        round = 0
77        visited = [[False for _ in self.kids] for _ in self.hosts]80        visited = [[False for _ in self.kids] for _ in self.hosts]
78    81    
79        while len(critical_kids) == 0 and round < len(self.hosts):82        while len(critical_kids) == 0 and round < len(self.hosts):
80            for kid in self.kids:83            for kid in self.kids:
81                min_distance = float('inf')84                min_distance = float('inf')
nn85                same_distance_hosts = []
82                for host in self.hosts:86                for host in self.hosts:
83                    if visited[self.hosts.index(host)][self.kids.index(kid)]:87                    if visited[self.hosts.index(host)][self.kids.index(kid)]:
84                        continue88                        continue
85                    distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2)89                    distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2)
86                    if distance < min_distance:90                    if distance < min_distance:
87                        min_distance = distance91                        min_distance = distance
88                        closest_host = host92                        closest_host = host
89                        same_distance_hosts = [host]93                        same_distance_hosts = [host]
90                    elif distance == min_distance:94                    elif distance == min_distance:
91                        same_distance_hosts.append(host)95                        same_distance_hosts.append(host)
92                    if len(same_distance_hosts) > 1:96                    if len(same_distance_hosts) > 1:
93                        closest_host = min(same_distance_hosts, key=lambda host: host.get_position())97                        closest_host = min(same_distance_hosts, key=lambda host: host.get_position())
94                98                
n95                if len(closest_host.candies) == 0:n99                visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True
96                    continue100                kid.set_position(closest_host.get_position())
97101
nn102                if len(closest_host.candies) != 0: 
98                kid.add_candy(closest_host.remove_candy(find_max_element_in_list_by_criteria))103                    kid.add_candy(closest_host.remove_candy(FluxCapacitor.find_max_element_in_list_by_criteria))
99                kid.set_position(closest_host.get_position())
100                visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True
101                same_distance_hosts = []
102104
103                if kid.is_critical():105                if kid.is_critical():
104                    critical_kids.add(kid)106                    critical_kids.add(kid)
105            round += 1107            round += 1
106108
n107        if len(critical_kids) == 0:n
108            return None
109        return critical_kids109        return critical_kids or None
110    110    
tt111 
111def find_max_element_in_list_by_criteria(list):112    def find_max_element_in_list_by_criteria(list):
112    return max(list, key=lambda candy: candy.get_mass())113        return max(list, key=lambda candy: candy.get_mass())
113114
114115
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1from math import sqrtf1from math import sqrt
22
3class Candy:3class Candy:
44
5    def __init__(self, mass, uranium):5    def __init__(self, mass, uranium):
6        self.mass = mass6        self.mass = mass
7        self.uranium = uranium7        self.uranium = uranium
88
9    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
10        return self.mass * self.uranium10        return self.mass * self.uranium
11    11    
12    def get_mass(self):12    def get_mass(self):
13        return self.mass13        return self.mass
1414
15class Person:15class Person:
1616
17    def __init__(self, position):17    def __init__(self, position):
18        self.position = position18        self.position = position
1919
20    def get_position(self):20    def get_position(self):
21        return self.position21        return self.position
22    22    
23    def set_position(self, new_position):23    def set_position(self, new_position):
24        self.position = new_position24        self.position = new_position
2525
26class Kid(Person):26class Kid(Person):
27    27    
28    def __init__(self, position, initiative):28    def __init__(self, position, initiative):
29        super().__init__(position)29        super().__init__(position)
30        self.initiative = initiative30        self.initiative = initiative
31        self.basket = []31        self.basket = []
3232
33    def get_initiative(self):33    def get_initiative(self):
34        return self.initiative34        return self.initiative
35    35    
36    def add_candy(self, candy):36    def add_candy(self, candy):
37        print(candy.mass, candy.uranium)37        print(candy.mass, candy.uranium)
38        self.basket.append(candy)38        self.basket.append(candy)
3939
40    def is_critical(self):40    def is_critical(self):
41        return sum(candy.get_uranium_quantity() for candy in self.basket) > 2041        return sum(candy.get_uranium_quantity() for candy in self.basket) > 20
4242
43class Host(Person):43class Host(Person):
44    def __init__(self, position, candies):44    def __init__(self, position, candies):
45        super().__init__(position)45        super().__init__(position)
46        self.candies = [Candy(mass, uranium) for mass,uranium in candies]46        self.candies = [Candy(mass, uranium) for mass,uranium in candies]
4747
48    def remove_candy(self, func):48    def remove_candy(self, func):
49        if not self.candies:49        if not self.candies:
50            return None50            return None
5151
52        selected_candy = func(self.candies)52        selected_candy = func(self.candies)
53        self.candies.remove(selected_candy)53        self.candies.remove(selected_candy)
54        return selected_candy54        return selected_candy
5555
5656
57class FluxCapacitor:57class FluxCapacitor:
58    def __init__(self, participants):58    def __init__(self, participants):
59        self.kids = []59        self.kids = []
60        self.hosts = []60        self.hosts = []
6161
62        for participant in participants: 62        for participant in participants: 
63            if type(participant) == Kid:63            if type(participant) == Kid:
64                self.kids.append(participant)64                self.kids.append(participant)
65        65        
66        self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True)66        self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True)
67        67        
68        for participant in participants: 68        for participant in participants: 
69            if type(participant) == Host:69            if type(participant) == Host:
70                self.hosts.append(participant)70                self.hosts.append(participant)
7171
72    def get_victim(self):72    def get_victim(self):
73        critical_kids = set()73        critical_kids = set()
74        closest_host = None74        closest_host = None
75        same_distance_hosts = []75        same_distance_hosts = []
76        round = 076        round = 0
77        visited = [[False for _ in self.kids] for _ in self.hosts]77        visited = [[False for _ in self.kids] for _ in self.hosts]
78    78    
79        while len(critical_kids) == 0 and round < len(self.hosts):79        while len(critical_kids) == 0 and round < len(self.hosts):
80            for kid in self.kids:80            for kid in self.kids:
n81                print(f"Current kid: {kid.position}\n")n
82                min_distance = float('inf')81                min_distance = float('inf')
83                for host in self.hosts:82                for host in self.hosts:
n84                    print(f"Current host: {host.position}")n
85                    if visited[self.hosts.index(host)][self.kids.index(kid)]:83                    if visited[self.hosts.index(host)][self.kids.index(kid)]:
n86                        print("skipped:", self.hosts.index(host), self.kids.index(kid))n
87                        continue84                        continue
88                    distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2)85                    distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2)
89                    if distance < min_distance:86                    if distance < min_distance:
90                        min_distance = distance87                        min_distance = distance
91                        closest_host = host88                        closest_host = host
92                        same_distance_hosts = [host]89                        same_distance_hosts = [host]
93                    elif distance == min_distance:90                    elif distance == min_distance:
94                        same_distance_hosts.append(host)91                        same_distance_hosts.append(host)
95                    if len(same_distance_hosts) > 1:92                    if len(same_distance_hosts) > 1:
96                        closest_host = min(same_distance_hosts, key=lambda host: host.get_position())93                        closest_host = min(same_distance_hosts, key=lambda host: host.get_position())
97                94                
n98                print(f"Move to host: {closest_host.position}")n
99                if len(closest_host.candies) == 0:95                if len(closest_host.candies) == 0:
n100                    print("candy problem")n
101                    continue96                    continue
10297
103                kid.add_candy(closest_host.remove_candy(find_max_element_in_list_by_criteria))98                kid.add_candy(closest_host.remove_candy(find_max_element_in_list_by_criteria))
104                kid.set_position(closest_host.get_position())99                kid.set_position(closest_host.get_position())
105                visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True100                visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True
106                same_distance_hosts = []101                same_distance_hosts = []
107102
n108                print("visited:", self.hosts.index(host), self.kids.index(kid))n
109                if kid.is_critical():103                if kid.is_critical():
110                    critical_kids.add(kid)104                    critical_kids.add(kid)
111            round += 1105            round += 1
112106
113        if len(critical_kids) == 0:107        if len(critical_kids) == 0:
114            return None108            return None
115        return critical_kids109        return critical_kids
116    110    
117def find_max_element_in_list_by_criteria(list):111def find_max_element_in_list_by_criteria(list):
118    return max(list, key=lambda candy: candy.get_mass())112    return max(list, key=lambda candy: candy.get_mass())
119113
tt114 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op