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

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

10 точки общо

12 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

  1class Candy:
  2    def __init__(self, mass, uranium):
  3        self.mass = mass
  4        self.uranium = uranium
  5
  6    def get_uranium_quantity(self):
  7        uranium_quantity = self.mass * self.uranium
  8        return uranium_quantity
  9
 10    def get_mass(self):
 11        return self.mass
 12
 13class Person:
 14    def __init__(self, position):
 15        self.position = position
 16
 17    def get_position(self):
 18        return self.position
 19
 20    def set_position(self, new_position):
 21        self.position = new_position
 22
 23
 24class Kid(Person):
 25    def __init__(self, position, initiative):
 26        super().__init__(position)
 27        self.initiative = initiative
 28        self.candy_basket = []
 29
 30    def get_initiative(self):
 31        return self.initiative
 32
 33    def add_candy(self, candy_instance):
 34        self.candy_basket.append(candy_instance)
 35
 36    def is_critical(self):
 37        total_uranium = sum(candy.get_uranium_quantity() for candy in self.candy_basket)
 38
 39        return total_uranium > 20
 40
 41
 42class Host(Person):
 43    def __init__(self, position, candies):
 44        super().__init__(position)
 45        self.candy_basket = []
 46
 47        for candy_args in candies:
 48            candy_instance = Candy(*candy_args)
 49            self.candy_basket.append(candy_instance)
 50
 51    def remove_candy(self, selection_function):
 52        if not self.candy_basket:
 53            return None
 54        else:
 55            selected_candy = selection_function(self.candy_basket)
 56
 57            self.candy_basket.remove(selected_candy)
 58
 59            return selected_candy
 60
 61def max_candy_mass(candies):
 62    return max(candies, key=lambda candy: candy.get_mass())
 63
 64class FluxCapacitor:
 65    def __init__(self, participants):
 66        self.participants = participants
 67
 68    def get_victim(self):
 69        while True:
 70            next_round_kids = set() 
 71            victims = set()
 72            all_visited_hosts = set()
 73
 74            for kid in self.participants:
 75                if isinstance(kid, Kid):
 76                    if kid in all_visited_hosts:
 77                        continue
 78
 79                    closest_host = None
 80                    closest_distance = float('inf')
 81
 82                    for host in self.participants:
 83                        if isinstance(host, Host) and host not in all_visited_hosts:
 84                            distance = abs(kid.get_position()[0] - host.get_position()[0]) + abs(kid.get_position()[1] - host.get_position()[1])
 85                            if distance < closest_distance:
 86                                closest_host = host
 87                                closest_distance = distance
 88
 89                    if closest_host is not None:
 90                        candy = closest_host.remove_candy(max_candy_mass)
 91                        if candy:
 92                            kid.add_candy(candy)
 93                            if kid.is_critical():
 94                                victims.add(kid)
 95                        all_visited_hosts.add(closest_host)
 96                        next_round_kids.add(kid)
 97                    else:
 98                        all_visited_hosts = set(self.participants)
 99
100            if not next_round_kids:
101                return None
102
103            if victims:
104                return victims
105            else:
106                self.participants = next_round_kids
107
108
109candy = Candy(20, 0.3)
110person = Person((1, 2))
111kid = Kid((0, 0), 123)
112host = Host((3, 4), [(1, 1.0), (2, 0.5)])
113flux_capacitor = FluxCapacitor({kid, host})
114
115victims = flux_capacitor.get_victim()
116
117if victims:
118    print("Victims found:")
119    for victim in victims:
120        print(victim.get_position())
121else:
122    print("No victims found.")

No victims found.
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s

OK

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

f1class Candy:f1class Candy:
2    def __init__(self, mass, uranium):2    def __init__(self, mass, uranium):
3        self.mass = mass3        self.mass = mass
4        self.uranium = uranium4        self.uranium = uranium
55
6    def get_uranium_quantity(self):6    def get_uranium_quantity(self):
7        uranium_quantity = self.mass * self.uranium7        uranium_quantity = self.mass * self.uranium
8        return uranium_quantity8        return uranium_quantity
99
10    def get_mass(self):10    def get_mass(self):
11        return self.mass11        return self.mass
1212
13class Person:13class Person:
14    def __init__(self, position):14    def __init__(self, position):
15        self.position = position15        self.position = position
1616
17    def get_position(self):17    def get_position(self):
18        return self.position18        return self.position
1919
20    def set_position(self, new_position):20    def set_position(self, new_position):
21        self.position = new_position21        self.position = new_position
2222
2323
24class Kid(Person):24class Kid(Person):
25    def __init__(self, position, initiative):25    def __init__(self, position, initiative):
26        super().__init__(position)26        super().__init__(position)
27        self.initiative = initiative27        self.initiative = initiative
n28        self.candy_basket = []  # to store candiesn28        self.candy_basket = []
2929
30    def get_initiative(self):30    def get_initiative(self):
31        return self.initiative31        return self.initiative
3232
33    def add_candy(self, candy_instance):33    def add_candy(self, candy_instance):
34        self.candy_basket.append(candy_instance)34        self.candy_basket.append(candy_instance)
3535
36    def is_critical(self):36    def is_critical(self):
37        total_uranium = sum(candy.get_uranium_quantity() for candy in self.candy_basket)37        total_uranium = sum(candy.get_uranium_quantity() for candy in self.candy_basket)
3838
39        return total_uranium > 2039        return total_uranium > 20
4040
4141
42class Host(Person):42class Host(Person):
43    def __init__(self, position, candies):43    def __init__(self, position, candies):
44        super().__init__(position)44        super().__init__(position)
n45        self.candy_basket = []  # to store candiesn45        self.candy_basket = []
4646
47        for candy_args in candies:47        for candy_args in candies:
48            candy_instance = Candy(*candy_args)48            candy_instance = Candy(*candy_args)
49            self.candy_basket.append(candy_instance)49            self.candy_basket.append(candy_instance)
5050
51    def remove_candy(self, selection_function):51    def remove_candy(self, selection_function):
52        if not self.candy_basket:52        if not self.candy_basket:
53            return None53            return None
54        else:54        else:
55            selected_candy = selection_function(self.candy_basket)55            selected_candy = selection_function(self.candy_basket)
5656
57            self.candy_basket.remove(selected_candy)57            self.candy_basket.remove(selected_candy)
5858
59            return selected_candy59            return selected_candy
6060
61def max_candy_mass(candies):61def max_candy_mass(candies):
62    return max(candies, key=lambda candy: candy.get_mass())62    return max(candies, key=lambda candy: candy.get_mass())
nn63 
63class FluxCapacitor:64class FluxCapacitor:
64    def __init__(self, participants):65    def __init__(self, participants):
65        self.participants = participants66        self.participants = participants
6667
67    def get_victim(self):68    def get_victim(self):
68        while True:69        while True:
69            next_round_kids = set() 70            next_round_kids = set() 
70            victims = set()71            victims = set()
71            all_visited_hosts = set()72            all_visited_hosts = set()
7273
n73            # Process kids' interactions with hostsn
74            for kid in self.participants:74            for kid in self.participants:
75                if isinstance(kid, Kid):75                if isinstance(kid, Kid):
76                    if kid in all_visited_hosts:76                    if kid in all_visited_hosts:
n77                        # kids that have visited all hosts - skipn
78                        continue77                        continue
7978
80                    closest_host = None79                    closest_host = None
81                    closest_distance = float('inf')80                    closest_distance = float('inf')
8281
83                    for host in self.participants:82                    for host in self.participants:
84                        if isinstance(host, Host) and host not in all_visited_hosts:83                        if isinstance(host, Host) and host not in all_visited_hosts:
85                            distance = abs(kid.get_position()[0] - host.get_position()[0]) + abs(kid.get_position()[1] - host.get_position()[1])84                            distance = abs(kid.get_position()[0] - host.get_position()[0]) + abs(kid.get_position()[1] - host.get_position()[1])
86                            if distance < closest_distance:85                            if distance < closest_distance:
87                                closest_host = host86                                closest_host = host
88                                closest_distance = distance87                                closest_distance = distance
8988
90                    if closest_host is not None:89                    if closest_host is not None:
91                        candy = closest_host.remove_candy(max_candy_mass)90                        candy = closest_host.remove_candy(max_candy_mass)
92                        if candy:91                        if candy:
93                            kid.add_candy(candy)92                            kid.add_candy(candy)
94                            if kid.is_critical():93                            if kid.is_critical():
95                                victims.add(kid)94                                victims.add(kid)
96                        all_visited_hosts.add(closest_host)95                        all_visited_hosts.add(closest_host)
97                        next_round_kids.add(kid)96                        next_round_kids.add(kid)
98                    else:97                    else:
n99                        all_visited_hosts = set(self.participants)  # all hosts visitedn98                        all_visited_hosts = set(self.participants)
10099
101            if not next_round_kids:100            if not next_round_kids:
n102                # all kids visited all hosts; no one reached critical massn
103                return None101                return None
104102
105            if victims:103            if victims:
106                return victims104                return victims
107            else:105            else:
108                self.participants = next_round_kids106                self.participants = next_round_kids
109107
110108
111candy = Candy(20, 0.3)109candy = Candy(20, 0.3)
112person = Person((1, 2))110person = Person((1, 2))
113kid = Kid((0, 0), 123)111kid = Kid((0, 0), 123)
114host = Host((3, 4), [(1, 1.0), (2, 0.5)])112host = Host((3, 4), [(1, 1.0), (2, 0.5)])
115flux_capacitor = FluxCapacitor({kid, host})113flux_capacitor = FluxCapacitor({kid, host})
116114
t117# get victimst
118victims = flux_capacitor.get_victim()115victims = flux_capacitor.get_victim()
119116
120if victims:117if victims:
121    print("Victims found:")118    print("Victims found:")
122    for victim in victims:119    for victim in victims:
123        print(victim.get_position())120        print(victim.get_position())
124else:121else:
125    print("No victims found.")122    print("No victims found.")
126123
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op