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

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

10 точки общо

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

  1import math
  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.uranium*self.mass
 12    
 13    def get_mass(self):
 14        return self.mass
 15    
 16    def __gt__(self, other):
 17        return self.mass > other.mass
 18    
 19
 20class Person:
 21
 22    def __init__(self, position):
 23        self.set_position(position)
 24
 25    def get_position(self):
 26        return self.position
 27
 28    def set_position(self, position):
 29        self.position = position    
 30
 31
 32class Kid(Person):
 33
 34    CRITICAL_URANIUM_DOSE = 20
 35
 36    def __init__(self, position, initiative):
 37        super().__init__(position)
 38        self.initiative = initiative
 39        self.basket = []
 40        self.visited_hosts = set()
 41
 42    def get_initiative(self):
 43        return self.initiative
 44    
 45    def add_candy(self, candy):
 46        self.basket.append(candy)
 47
 48    def is_critical(self):
 49        sum = 0
 50        for candy in self.basket:
 51            sum += candy.get_uranium_quantity()
 52        return sum > self.CRITICAL_URANIUM_DOSE
 53
 54    def __lt__(self, other):
 55        return self.initiative < other.initiative        
 56
 57    def _sort_hosts(self, host):
 58        dist = math.sqrt((self.position[0] - host.position[0]) ** 2 + (self.position[1] - host.position[1]) ** 2)
 59        return dist, host.position[0], host.position[1]
 60    
 61    def _go_to_door(self, hosts):
 62        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
 63        if not unvisited_hosts:
 64            return False
 65        unvisited_hosts.sort(key=lambda host: self._sort_hosts(host))
 66        selected_host = unvisited_hosts[0]
 67        self.visited_hosts.add(selected_host)
 68        self.position=selected_host.position
 69        selected_host.visiting_kids.append(self)
 70
 71
 72class Host(Person):
 73
 74    def __init__(self, position, candies):
 75        super().__init__(position)
 76        self.basket = []
 77        self.visiting_kids = []
 78        for candy in candies:
 79            self.basket.append(Candy(candy[0], candy[1]))
 80
 81    def remove_candy(self, func):
 82        if not self.basket:
 83            return 
 84        candy_to_remove = func(self.basket)
 85        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]
 86        return candy_to_remove
 87    
 88    def _give_candy(self):
 89        self.visiting_kids.sort(reverse = True)
 90        for kid in self.visiting_kids:
 91            candy = self.remove_candy(func=lambda basket:sorted(basket)[0])
 92            if candy is not None:
 93                kid.basket.append(candy)
 94        self.visiting_kids.clear()
 95
 96
 97class FluxCapacitor:
 98
 99    def __init__(self, participants):
100        self.participants = participants
101        self.kids = []
102        self.hosts = []
103        for person in participants:
104            if isinstance(person, Kid):
105                self.kids.append(person)
106            elif isinstance(person, Host):
107                self.hosts.append(person)
108    
109    def get_victim(self):
110        kids_with_critical_mass = set()
111        while True:
112            for kid in self.kids:
113                kid._go_to_door(self.hosts)
114            for host in self.hosts:
115                host._give_candy()
116            for kid in self.kids:
117                if kid.is_critical():
118                    kids_with_critical_mass.add(kid)
119            if kids_with_critical_mass:
120                return kids_with_critical_mass
121            all_kids_visited_all_hosts = True
122            for kid in self.kids:
123                if not kid.visited_hosts == set(self.hosts):
124                    all_kids_visited_all_hosts = False
125                    break
126            if all_kids_visited_all_hosts:
127                return None

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

OK

Дискусия
Георги Кунчев
05.11.2023 21:32

Да, долната черта важи и за методи, и за атрибути. Някои от атрибутите ти биха се радвали на една долна черта в името си, но не е нещо, за което държа настоятелно, затова не го споменах изрично.
Добромир Пеев
05.11.2023 15:46

_ се слага при private или методи и атрибути ли, че вече се обърках след като видях презентацията и последната публикация в форума. има ли смисъл в моя код да указвам кои методи и атрибути са private/protected.
История

f1import mathf1import math
22
33
4class Candy:4class Candy:
55
6    def __init__(self, mass, uranium):6    def __init__(self, mass, uranium):
7        self.mass = mass7        self.mass = mass
8        self.uranium = uranium8        self.uranium = uranium
9    9    
10    def get_uranium_quantity(self):10    def get_uranium_quantity(self):
11        return self.uranium*self.mass11        return self.uranium*self.mass
12    12    
13    def get_mass(self):13    def get_mass(self):
14        return self.mass14        return self.mass
15    15    
16    def __gt__(self, other):16    def __gt__(self, other):
17        return self.mass > other.mass17        return self.mass > other.mass
18    18    
1919
20class Person:20class Person:
2121
22    def __init__(self, position):22    def __init__(self, position):
23        self.set_position(position)23        self.set_position(position)
2424
25    def get_position(self):25    def get_position(self):
26        return self.position26        return self.position
2727
28    def set_position(self, position):28    def set_position(self, position):
29        self.position = position    29        self.position = position    
3030
3131
32class Kid(Person):32class Kid(Person):
3333
34    CRITICAL_URANIUM_DOSE = 2034    CRITICAL_URANIUM_DOSE = 20
3535
36    def __init__(self, position, initiative):36    def __init__(self, position, initiative):
37        super().__init__(position)37        super().__init__(position)
38        self.initiative = initiative38        self.initiative = initiative
39        self.basket = []39        self.basket = []
40        self.visited_hosts = set()40        self.visited_hosts = set()
4141
42    def get_initiative(self):42    def get_initiative(self):
43        return self.initiative43        return self.initiative
44    44    
45    def add_candy(self, candy):45    def add_candy(self, candy):
46        self.basket.append(candy)46        self.basket.append(candy)
4747
48    def is_critical(self):48    def is_critical(self):
49        sum = 049        sum = 0
50        for candy in self.basket:50        for candy in self.basket:
51            sum += candy.get_uranium_quantity()51            sum += candy.get_uranium_quantity()
t52        return sum >= self.CRITICAL_URANIUM_DOSEt52        return sum > self.CRITICAL_URANIUM_DOSE
5353
54    def __lt__(self, other):54    def __lt__(self, other):
55        return self.initiative < other.initiative        55        return self.initiative < other.initiative        
5656
57    def _sort_hosts(self, host):57    def _sort_hosts(self, host):
58        dist = math.sqrt((self.position[0] - host.position[0]) ** 2 + (self.position[1] - host.position[1]) ** 2)58        dist = math.sqrt((self.position[0] - host.position[0]) ** 2 + (self.position[1] - host.position[1]) ** 2)
59        return dist, host.position[0], host.position[1]59        return dist, host.position[0], host.position[1]
60    60    
61    def _go_to_door(self, hosts):61    def _go_to_door(self, hosts):
62        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]62        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
63        if not unvisited_hosts:63        if not unvisited_hosts:
64            return False64            return False
65        unvisited_hosts.sort(key=lambda host: self._sort_hosts(host))65        unvisited_hosts.sort(key=lambda host: self._sort_hosts(host))
66        selected_host = unvisited_hosts[0]66        selected_host = unvisited_hosts[0]
67        self.visited_hosts.add(selected_host)67        self.visited_hosts.add(selected_host)
68        self.position=selected_host.position68        self.position=selected_host.position
69        selected_host.visiting_kids.append(self)69        selected_host.visiting_kids.append(self)
7070
7171
72class Host(Person):72class Host(Person):
7373
74    def __init__(self, position, candies):74    def __init__(self, position, candies):
75        super().__init__(position)75        super().__init__(position)
76        self.basket = []76        self.basket = []
77        self.visiting_kids = []77        self.visiting_kids = []
78        for candy in candies:78        for candy in candies:
79            self.basket.append(Candy(candy[0], candy[1]))79            self.basket.append(Candy(candy[0], candy[1]))
8080
81    def remove_candy(self, func):81    def remove_candy(self, func):
82        if not self.basket:82        if not self.basket:
83            return 83            return 
84        candy_to_remove = func(self.basket)84        candy_to_remove = func(self.basket)
85        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]85        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]
86        return candy_to_remove86        return candy_to_remove
87    87    
88    def _give_candy(self):88    def _give_candy(self):
89        self.visiting_kids.sort(reverse = True)89        self.visiting_kids.sort(reverse = True)
90        for kid in self.visiting_kids:90        for kid in self.visiting_kids:
91            candy = self.remove_candy(func=lambda basket:sorted(basket)[0])91            candy = self.remove_candy(func=lambda basket:sorted(basket)[0])
92            if candy is not None:92            if candy is not None:
93                kid.basket.append(candy)93                kid.basket.append(candy)
94        self.visiting_kids.clear()94        self.visiting_kids.clear()
9595
9696
97class FluxCapacitor:97class FluxCapacitor:
9898
99    def __init__(self, participants):99    def __init__(self, participants):
100        self.participants = participants100        self.participants = participants
101        self.kids = []101        self.kids = []
102        self.hosts = []102        self.hosts = []
103        for person in participants:103        for person in participants:
104            if isinstance(person, Kid):104            if isinstance(person, Kid):
105                self.kids.append(person)105                self.kids.append(person)
106            elif isinstance(person, Host):106            elif isinstance(person, Host):
107                self.hosts.append(person)107                self.hosts.append(person)
108    108    
109    def get_victim(self):109    def get_victim(self):
110        kids_with_critical_mass = set()110        kids_with_critical_mass = set()
111        while True:111        while True:
112            for kid in self.kids:112            for kid in self.kids:
113                kid._go_to_door(self.hosts)113                kid._go_to_door(self.hosts)
114            for host in self.hosts:114            for host in self.hosts:
115                host._give_candy()115                host._give_candy()
116            for kid in self.kids:116            for kid in self.kids:
117                if kid.is_critical():117                if kid.is_critical():
118                    kids_with_critical_mass.add(kid)118                    kids_with_critical_mass.add(kid)
119            if kids_with_critical_mass:119            if kids_with_critical_mass:
120                return kids_with_critical_mass120                return kids_with_critical_mass
121            all_kids_visited_all_hosts = True121            all_kids_visited_all_hosts = True
122            for kid in self.kids:122            for kid in self.kids:
123                if not kid.visited_hosts == set(self.hosts):123                if not kid.visited_hosts == set(self.hosts):
124                    all_kids_visited_all_hosts = False124                    all_kids_visited_all_hosts = False
125                    break125                    break
126            if all_kids_visited_all_hosts:126            if all_kids_visited_all_hosts:
127                return None127                return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
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
8    9    
9    def get_uranium_quantity(self):10    def get_uranium_quantity(self):
10        return self.uranium*self.mass11        return self.uranium*self.mass
11    12    
12    def get_mass(self):13    def get_mass(self):
13        return self.mass14        return self.mass
14    15    
15    def __gt__(self, other):16    def __gt__(self, other):
16        return self.mass > other.mass17        return self.mass > other.mass
17    18    
1819
19class Person:20class Person:
2021
21    def __init__(self, position):22    def __init__(self, position):
22        self.set_position(position)23        self.set_position(position)
2324
24    def get_position(self):25    def get_position(self):
25        return self.position26        return self.position
2627
27    def set_position(self, position):28    def set_position(self, position):
28        self.position = position    29        self.position = position    
2930
3031
31class Kid(Person):32class Kid(Person):
3233
nn34    CRITICAL_URANIUM_DOSE = 20
35 
33    def __init__(self, position, initiative):36    def __init__(self, position, initiative):
34        super().__init__(position)37        super().__init__(position)
35        self.initiative = initiative38        self.initiative = initiative
36        self.basket = []39        self.basket = []
37        self.visited_hosts = set()40        self.visited_hosts = set()
3841
39    def get_initiative(self):42    def get_initiative(self):
40        return self.initiative43        return self.initiative
41    44    
42    def add_candy(self, candy):45    def add_candy(self, candy):
43        self.basket.append(candy)46        self.basket.append(candy)
4447
45    def is_critical(self):48    def is_critical(self):
46        sum = 049        sum = 0
47        for candy in self.basket:50        for candy in self.basket:
48            sum += candy.get_uranium_quantity()51            sum += candy.get_uranium_quantity()
n49        return sum >= 20n52        return sum >= self.CRITICAL_URANIUM_DOSE
5053
51    def __lt__(self, other):54    def __lt__(self, other):
52        return self.initiative < other.initiative        55        return self.initiative < other.initiative        
5356
54    def _sort_hosts(self, host):57    def _sort_hosts(self, host):
n55        dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2))n58        dist = math.sqrt((self.position[0] - host.position[0]) ** 2 + (self.position[1] - host.position[1]) ** 2)
56        return (dist, host.position[1], host.position[0])59        return dist, host.position[0], host.position[1]
57    60    
58    def _go_to_door(self, hosts):61    def _go_to_door(self, hosts):
59        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]62        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
60        if not unvisited_hosts:63        if not unvisited_hosts:
61            return False64            return False
n62        unvisited_hosts.sort(key = lambda host : self._sort_hosts(host))n65        unvisited_hosts.sort(key=lambda host: self._sort_hosts(host))
63        selected_host = unvisited_hosts[0]66        selected_host = unvisited_hosts[0]
64        self.visited_hosts.add(selected_host)67        self.visited_hosts.add(selected_host)
nn68        self.position=selected_host.position
65        selected_host.visiting_kids.append(self)69        selected_host.visiting_kids.append(self)
6670
6771
68class Host(Person):72class Host(Person):
6973
70    def __init__(self, position, candies):74    def __init__(self, position, candies):
71        super().__init__(position)75        super().__init__(position)
n72        self.basket=[]n76        self.basket = []
73        self.visiting_kids=[]77        self.visiting_kids = []
74        for candy in candies:78        for candy in candies:
n75            self.basket.append(Candy(candy[0],candy[1]))n79            self.basket.append(Candy(candy[0], candy[1]))
7680
77    def remove_candy(self, func):81    def remove_candy(self, func):
78        if not self.basket:82        if not self.basket:
79            return 83            return 
80        candy_to_remove = func(self.basket)84        candy_to_remove = func(self.basket)
81        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]85        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]
82        return candy_to_remove86        return candy_to_remove
83    87    
84    def _give_candy(self):88    def _give_candy(self):
85        self.visiting_kids.sort(reverse = True)89        self.visiting_kids.sort(reverse = True)
86        for kid in self.visiting_kids:90        for kid in self.visiting_kids:
n87            candy = self.remove_candy(func = lambda basket:sorted(basket)[0])n91            candy = self.remove_candy(func=lambda basket:sorted(basket)[0])
88            if candy is not None:92            if candy is not None:
89                kid.basket.append(candy)93                kid.basket.append(candy)
90        self.visiting_kids.clear()94        self.visiting_kids.clear()
9195
9296
93class FluxCapacitor:97class FluxCapacitor:
9498
95    def __init__(self, participants):99    def __init__(self, participants):
96        self.participants = participants100        self.participants = participants
97        self.kids = []101        self.kids = []
98        self.hosts = []102        self.hosts = []
99        for person in participants:103        for person in participants:
100            if isinstance(person, Kid):104            if isinstance(person, Kid):
101                self.kids.append(person)105                self.kids.append(person)
102            elif isinstance(person, Host):106            elif isinstance(person, Host):
103                self.hosts.append(person)107                self.hosts.append(person)
104    108    
105    def get_victim(self):109    def get_victim(self):
106        kids_with_critical_mass = set()110        kids_with_critical_mass = set()
t107        while(True):t111        while True:
108            for kid in self.kids:112            for kid in self.kids:
109                kid._go_to_door(self.hosts)113                kid._go_to_door(self.hosts)
110            for host in self.hosts:114            for host in self.hosts:
111                host._give_candy()115                host._give_candy()
112            for kid in self.kids:116            for kid in self.kids:
113                if kid.is_critical():117                if kid.is_critical():
114                    kids_with_critical_mass.add(kid)118                    kids_with_critical_mass.add(kid)
115            if kids_with_critical_mass:119            if kids_with_critical_mass:
116                return kids_with_critical_mass120                return kids_with_critical_mass
117            all_kids_visited_all_hosts = True121            all_kids_visited_all_hosts = True
118            for kid in self.kids:122            for kid in self.kids:
119                if not kid.visited_hosts == set(self.hosts):123                if not kid.visited_hosts == set(self.hosts):
120                    all_kids_visited_all_hosts = False124                    all_kids_visited_all_hosts = False
121                    break125                    break
122            if all_kids_visited_all_hosts:126            if all_kids_visited_all_hosts:
123                return None127                return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
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
8    8    
9    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
10        return self.uranium*self.mass10        return self.uranium*self.mass
11    11    
12    def get_mass(self):12    def get_mass(self):
13        return self.mass13        return self.mass
14    14    
15    def __gt__(self, other):15    def __gt__(self, other):
16        return self.mass > other.mass16        return self.mass > other.mass
17    17    
1818
19class Person:19class Person:
2020
21    def __init__(self, position):21    def __init__(self, position):
22        self.set_position(position)22        self.set_position(position)
2323
24    def get_position(self):24    def get_position(self):
25        return self.position25        return self.position
2626
27    def set_position(self, position):27    def set_position(self, position):
28        self.position = position    28        self.position = position    
2929
3030
31class Kid(Person):31class Kid(Person):
3232
33    def __init__(self, position, initiative):33    def __init__(self, position, initiative):
34        super().__init__(position)34        super().__init__(position)
35        self.initiative = initiative35        self.initiative = initiative
36        self.basket = []36        self.basket = []
37        self.visited_hosts = set()37        self.visited_hosts = set()
3838
39    def get_initiative(self):39    def get_initiative(self):
40        return self.initiative40        return self.initiative
41    41    
42    def add_candy(self, candy):42    def add_candy(self, candy):
43        self.basket.append(candy)43        self.basket.append(candy)
4444
45    def is_critical(self):45    def is_critical(self):
46        sum = 046        sum = 0
47        for candy in self.basket:47        for candy in self.basket:
48            sum += candy.get_uranium_quantity()48            sum += candy.get_uranium_quantity()
49        return sum >= 2049        return sum >= 20
5050
51    def __lt__(self, other):51    def __lt__(self, other):
52        return self.initiative < other.initiative        52        return self.initiative < other.initiative        
5353
54    def _sort_hosts(self, host):54    def _sort_hosts(self, host):
55        dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2))55        dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2))
56        return (dist, host.position[1], host.position[0])56        return (dist, host.position[1], host.position[0])
57    57    
58    def _go_to_door(self, hosts):58    def _go_to_door(self, hosts):
59        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]59        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
60        if not unvisited_hosts:60        if not unvisited_hosts:
61            return False61            return False
62        unvisited_hosts.sort(key = lambda host : self._sort_hosts(host))62        unvisited_hosts.sort(key = lambda host : self._sort_hosts(host))
63        selected_host = unvisited_hosts[0]63        selected_host = unvisited_hosts[0]
64        self.visited_hosts.add(selected_host)64        self.visited_hosts.add(selected_host)
65        selected_host.visiting_kids.append(self)65        selected_host.visiting_kids.append(self)
6666
6767
68class Host(Person):68class Host(Person):
6969
70    def __init__(self, position, candies):70    def __init__(self, position, candies):
71        super().__init__(position)71        super().__init__(position)
72        self.basket=[]72        self.basket=[]
73        self.visiting_kids=[]73        self.visiting_kids=[]
74        for candy in candies:74        for candy in candies:
75            self.basket.append(Candy(candy[0],candy[1]))75            self.basket.append(Candy(candy[0],candy[1]))
7676
77    def remove_candy(self, func):77    def remove_candy(self, func):
78        if not self.basket:78        if not self.basket:
79            return 79            return 
80        candy_to_remove = func(self.basket)80        candy_to_remove = func(self.basket)
81        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]81        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]
82        return candy_to_remove82        return candy_to_remove
83    83    
84    def _give_candy(self):84    def _give_candy(self):
85        self.visiting_kids.sort(reverse = True)85        self.visiting_kids.sort(reverse = True)
86        for kid in self.visiting_kids:86        for kid in self.visiting_kids:
87            candy = self.remove_candy(func = lambda basket:sorted(basket)[0])87            candy = self.remove_candy(func = lambda basket:sorted(basket)[0])
88            if candy is not None:88            if candy is not None:
89                kid.basket.append(candy)89                kid.basket.append(candy)
90        self.visiting_kids.clear()90        self.visiting_kids.clear()
9191
9292
93class FluxCapacitor:93class FluxCapacitor:
9494
95    def __init__(self, participants):95    def __init__(self, participants):
96        self.participants = participants96        self.participants = participants
97        self.kids = []97        self.kids = []
98        self.hosts = []98        self.hosts = []
99        for person in participants:99        for person in participants:
100            if isinstance(person, Kid):100            if isinstance(person, Kid):
101                self.kids.append(person)101                self.kids.append(person)
102            elif isinstance(person, Host):102            elif isinstance(person, Host):
103                self.hosts.append(person)103                self.hosts.append(person)
104    104    
105    def get_victim(self):105    def get_victim(self):
106        kids_with_critical_mass = set()106        kids_with_critical_mass = set()
107        while(True):107        while(True):
108            for kid in self.kids:108            for kid in self.kids:
109                kid._go_to_door(self.hosts)109                kid._go_to_door(self.hosts)
110            for host in self.hosts:110            for host in self.hosts:
111                host._give_candy()111                host._give_candy()
112            for kid in self.kids:112            for kid in self.kids:
113                if kid.is_critical():113                if kid.is_critical():
114                    kids_with_critical_mass.add(kid)114                    kids_with_critical_mass.add(kid)
115            if kids_with_critical_mass:115            if kids_with_critical_mass:
116                return kids_with_critical_mass116                return kids_with_critical_mass
117            all_kids_visited_all_hosts = True117            all_kids_visited_all_hosts = True
118            for kid in self.kids:118            for kid in self.kids:
119                if not kid.visited_hosts == set(self.hosts):119                if not kid.visited_hosts == set(self.hosts):
120                    all_kids_visited_all_hosts = False120                    all_kids_visited_all_hosts = False
121                    break121                    break
122            if all_kids_visited_all_hosts:122            if all_kids_visited_all_hosts:
123                return None123                return None
t124            t
125            
126candy = Candy(20, 0.3)
127person = Person((1, 2))
128kid = Kid((1, 2), 123)
129kid1 = Kid((1, 2), 15)
130kid2 = Kid((1, 2), 65)
131host = Host((2, 0), [(2, 1.0), (20, 0.6)])
132host1 = Host((0, 2), [(15, 1.0), (15, 3.5)])
133host2 = Host((2, 1), [(10, 1.0), (266, 3.5)])
134flux_capacitor = FluxCapacitor({kid,kid1,kid2, host,host1,host2})
135 
136print(flux_capacitor.get_victim())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
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
8    8    
9    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
10        return self.uranium*self.mass10        return self.uranium*self.mass
11    11    
12    def get_mass(self):12    def get_mass(self):
13        return self.mass13        return self.mass
14    14    
n15    def __gt__(self,other):n15    def __gt__(self, other):
16        return self.mass > other.mass16        return self.mass > other.mass
17    17    
1818
19class Person:19class Person:
2020
21    def __init__(self, position):21    def __init__(self, position):
22        self.set_position(position)22        self.set_position(position)
2323
24    def get_position(self):24    def get_position(self):
25        return self.position25        return self.position
2626
27    def set_position(self, position):27    def set_position(self, position):
28        self.position = position    28        self.position = position    
2929
3030
31class Kid(Person):31class Kid(Person):
3232
33    def __init__(self, position, initiative):33    def __init__(self, position, initiative):
34        super().__init__(position)34        super().__init__(position)
35        self.initiative = initiative35        self.initiative = initiative
36        self.basket = []36        self.basket = []
37        self.visited_hosts = set()37        self.visited_hosts = set()
3838
39    def get_initiative(self):39    def get_initiative(self):
40        return self.initiative40        return self.initiative
41    41    
42    def add_candy(self, candy):42    def add_candy(self, candy):
43        self.basket.append(candy)43        self.basket.append(candy)
4444
45    def is_critical(self):45    def is_critical(self):
46        sum = 046        sum = 0
47        for candy in self.basket:47        for candy in self.basket:
48            sum += candy.get_uranium_quantity()48            sum += candy.get_uranium_quantity()
49        return sum >= 2049        return sum >= 20
5050
51    def __lt__(self, other):51    def __lt__(self, other):
52        return self.initiative < other.initiative        52        return self.initiative < other.initiative        
5353
54    def _sort_hosts(self, host):54    def _sort_hosts(self, host):
55        dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2))55        dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2))
56        return (dist, host.position[1], host.position[0])56        return (dist, host.position[1], host.position[0])
57    57    
58    def _go_to_door(self, hosts):58    def _go_to_door(self, hosts):
59        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]59        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
60        if not unvisited_hosts:60        if not unvisited_hosts:
61            return False61            return False
62        unvisited_hosts.sort(key = lambda host : self._sort_hosts(host))62        unvisited_hosts.sort(key = lambda host : self._sort_hosts(host))
63        selected_host = unvisited_hosts[0]63        selected_host = unvisited_hosts[0]
64        self.visited_hosts.add(selected_host)64        self.visited_hosts.add(selected_host)
65        selected_host.visiting_kids.append(self)65        selected_host.visiting_kids.append(self)
6666
6767
68class Host(Person):68class Host(Person):
6969
70    def __init__(self, position, candies):70    def __init__(self, position, candies):
71        super().__init__(position)71        super().__init__(position)
72        self.basket=[]72        self.basket=[]
73        self.visiting_kids=[]73        self.visiting_kids=[]
74        for candy in candies:74        for candy in candies:
75            self.basket.append(Candy(candy[0],candy[1]))75            self.basket.append(Candy(candy[0],candy[1]))
7676
77    def remove_candy(self, func):77    def remove_candy(self, func):
78        if not self.basket:78        if not self.basket:
79            return 79            return 
80        candy_to_remove = func(self.basket)80        candy_to_remove = func(self.basket)
81        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]81        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]
82        return candy_to_remove82        return candy_to_remove
83    83    
84    def _give_candy(self):84    def _give_candy(self):
85        self.visiting_kids.sort(reverse = True)85        self.visiting_kids.sort(reverse = True)
86        for kid in self.visiting_kids:86        for kid in self.visiting_kids:
87            candy = self.remove_candy(func = lambda basket:sorted(basket)[0])87            candy = self.remove_candy(func = lambda basket:sorted(basket)[0])
88            if candy is not None:88            if candy is not None:
89                kid.basket.append(candy)89                kid.basket.append(candy)
90        self.visiting_kids.clear()90        self.visiting_kids.clear()
9191
9292
93class FluxCapacitor:93class FluxCapacitor:
9494
95    def __init__(self, participants):95    def __init__(self, participants):
96        self.participants = participants96        self.participants = participants
97        self.kids = []97        self.kids = []
98        self.hosts = []98        self.hosts = []
99        for person in participants:99        for person in participants:
100            if isinstance(person, Kid):100            if isinstance(person, Kid):
101                self.kids.append(person)101                self.kids.append(person)
102            elif isinstance(person, Host):102            elif isinstance(person, Host):
103                self.hosts.append(person)103                self.hosts.append(person)
104    104    
105    def get_victim(self):105    def get_victim(self):
106        kids_with_critical_mass = set()106        kids_with_critical_mass = set()
107        while(True):107        while(True):
108            for kid in self.kids:108            for kid in self.kids:
109                kid._go_to_door(self.hosts)109                kid._go_to_door(self.hosts)
110            for host in self.hosts:110            for host in self.hosts:
111                host._give_candy()111                host._give_candy()
112            for kid in self.kids:112            for kid in self.kids:
113                if kid.is_critical():113                if kid.is_critical():
114                    kids_with_critical_mass.add(kid)114                    kids_with_critical_mass.add(kid)
115            if kids_with_critical_mass:115            if kids_with_critical_mass:
116                return kids_with_critical_mass116                return kids_with_critical_mass
117            all_kids_visited_all_hosts = True117            all_kids_visited_all_hosts = True
118            for kid in self.kids:118            for kid in self.kids:
119                if not kid.visited_hosts == set(self.hosts):119                if not kid.visited_hosts == set(self.hosts):
120                    all_kids_visited_all_hosts = False120                    all_kids_visited_all_hosts = False
121                    break121                    break
122            if all_kids_visited_all_hosts:122            if all_kids_visited_all_hosts:
123                return None123                return None
tt124            
125            
126candy = Candy(20, 0.3)
127person = Person((1, 2))
128kid = Kid((1, 2), 123)
129kid1 = Kid((1, 2), 15)
130kid2 = Kid((1, 2), 65)
131host = Host((2, 0), [(2, 1.0), (20, 0.6)])
132host1 = Host((0, 2), [(15, 1.0), (15, 3.5)])
133host2 = Host((2, 1), [(10, 1.0), (266, 3.5)])
134flux_capacitor = FluxCapacitor({kid,kid1,kid2, host,host1,host2})
135 
136print(flux_capacitor.get_victim())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
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
8    8    
9    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
10        return self.uranium*self.mass10        return self.uranium*self.mass
11    11    
12    def get_mass(self):12    def get_mass(self):
13        return self.mass13        return self.mass
14    14    
15    def __gt__(self,other):15    def __gt__(self,other):
16        return self.mass > other.mass16        return self.mass > other.mass
17    17    
1818
19class Person:19class Person:
2020
21    def __init__(self, position):21    def __init__(self, position):
22        self.set_position(position)22        self.set_position(position)
2323
24    def get_position(self):24    def get_position(self):
25        return self.position25        return self.position
2626
27    def set_position(self, position):27    def set_position(self, position):
n28        self.position=position    n28        self.position = position    
2929
3030
31class Kid(Person):31class Kid(Person):
3232
n33    def __init__(self,position, initiative):n33    def __init__(self, position, initiative):
34        super().__init__(position)34        super().__init__(position)
35        self.initiative = initiative35        self.initiative = initiative
n36        self.basket=[]n36        self.basket = []
37        self.visited_hosts=set()37        self.visited_hosts = set()
38 
39    def __str__(self):
40        return f"This kid has {self.initiative} initiative"    
4138
42    def get_initiative(self):39    def get_initiative(self):
43        return self.initiative40        return self.initiative
44    41    
45    def add_candy(self, candy):42    def add_candy(self, candy):
46        self.basket.append(candy)43        self.basket.append(candy)
4744
48    def is_critical(self):45    def is_critical(self):
n49        sum=0n46        sum = 0
50        for candy in self.basket:47        for candy in self.basket:
n51            sum+=candy.get_uranium_quantity()n48            sum += candy.get_uranium_quantity()
52        return sum>=2049        return sum >= 20
5350
n54    def __lt__(self,other):n51    def __lt__(self, other):
55        return self.initiative < other.initiative        52        return self.initiative < other.initiative        
5653
n57    def sort_hosts(self,host):n54    def _sort_hosts(self, host):
58        dist = math.sqrt(math.pow(self.position[0]-host.position[0],2)+math.pow(self.position[1]-host.position[1],2))55        dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2))
59        return (dist, host.position[1], host.position[0])56        return (dist, host.position[1], host.position[0])
60    57    
n61    def go_to_door(self, hosts):n58    def _go_to_door(self, hosts):
62        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]59        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
63        if not unvisited_hosts:60        if not unvisited_hosts:
64            return False61            return False
n65        unvisited_hosts.sort(key=lambda host: self.sort_hosts(host))n62        unvisited_hosts.sort(key = lambda host : self._sort_hosts(host))
66        selected_host = unvisited_hosts[0]63        selected_host = unvisited_hosts[0]
67        self.visited_hosts.add(selected_host)64        self.visited_hosts.add(selected_host)
68        selected_host.visiting_kids.append(self)65        selected_host.visiting_kids.append(self)
nn66 
6967
70class Host(Person):68class Host(Person):
7169
72    def __init__(self, position, candies):70    def __init__(self, position, candies):
73        super().__init__(position)71        super().__init__(position)
74        self.basket=[]72        self.basket=[]
75        self.visiting_kids=[]73        self.visiting_kids=[]
76        for candy in candies:74        for candy in candies:
77            self.basket.append(Candy(candy[0],candy[1]))75            self.basket.append(Candy(candy[0],candy[1]))
7876
79    def remove_candy(self, func):77    def remove_candy(self, func):
80        if not self.basket:78        if not self.basket:
81            return 79            return 
n82        candy_to_remove=func(self.basket)n80        candy_to_remove = func(self.basket)
83        self.basket=[candy for candy in self.basket if candy is not candy_to_remove]81        self.basket = [candy for candy in self.basket if candy is not candy_to_remove]
84        return candy_to_remove82        return candy_to_remove
85    83    
n86    def give_candy(self):n84    def _give_candy(self):
87        self.visiting_kids.sort(reverse=True)85        self.visiting_kids.sort(reverse = True)
88        for kid in self.visiting_kids:86        for kid in self.visiting_kids:
n89            candy=self.remove_candy(func=lambda basket:sorted(basket)[0])n87            candy = self.remove_candy(func = lambda basket:sorted(basket)[0])
90            if candy is not None:88            if candy is not None:
91                kid.basket.append(candy)89                kid.basket.append(candy)
92        self.visiting_kids.clear()90        self.visiting_kids.clear()
9391
9492
95class FluxCapacitor:93class FluxCapacitor:
9694
97    def __init__(self, participants):95    def __init__(self, participants):
n98        self.participants=participantsn96        self.participants = participants
99        self.kids=[]97        self.kids = []
100        self.hosts=[]98        self.hosts = []
101        for person in participants:99        for person in participants:
102            if isinstance(person, Kid):100            if isinstance(person, Kid):
103                self.kids.append(person)101                self.kids.append(person)
104            elif isinstance(person, Host):102            elif isinstance(person, Host):
105                self.hosts.append(person)103                self.hosts.append(person)
106    104    
107    def get_victim(self):105    def get_victim(self):
n108        kids_with_critical_mass=set()n106        kids_with_critical_mass = set()
109        while(True):107        while(True):
110            for kid in self.kids:108            for kid in self.kids:
n111                kid.go_to_door(self.hosts)n109                kid._go_to_door(self.hosts)
112            for host in self.hosts:110            for host in self.hosts:
n113                host.give_candy()n111                host._give_candy()
114            for kid in self.kids:112            for kid in self.kids:
115                if kid.is_critical():113                if kid.is_critical():
116                    kids_with_critical_mass.add(kid)114                    kids_with_critical_mass.add(kid)
117            if kids_with_critical_mass:115            if kids_with_critical_mass:
118                return kids_with_critical_mass116                return kids_with_critical_mass
119            all_kids_visited_all_hosts = True117            all_kids_visited_all_hosts = True
n120 n
121            for kid in self.kids:118            for kid in self.kids:
122                if not kid.visited_hosts == set(self.hosts):119                if not kid.visited_hosts == set(self.hosts):
123                    all_kids_visited_all_hosts = False120                    all_kids_visited_all_hosts = False
124                    break121                    break
n125 n
126            if all_kids_visited_all_hosts:122            if all_kids_visited_all_hosts:
127                return None123                return None
t128            t
129candy = Candy(20, 0.3)
130person = Person((1, 2))
131kid = Kid((0, 0), 123)
132kid1 = Kid((1, 2), 15)
133kid2 = Kid((6, 5), 65)
134host = Host((2, 0), [(2, 1.0), (20, 0.6)])
135host1 = Host((0, 2), [(15, 1.0), (15, 3.5)])
136host2 = Host((2, 1), [(10, 1.0), (266, 3.5)])
137flux_capacitor = FluxCapacitor({kid,kid1,kid2, host,host1,host2})
138flux_capacitor.get_victim()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
n2from functools import cmp_to_keyn
32
4class Candy:3class Candy:
54
6    def __init__(self, mass, uranium):5    def __init__(self, mass, uranium):
7        self.mass = mass6        self.mass = mass
8        self.uranium = uranium7        self.uranium = uranium
9    8    
10    def get_uranium_quantity(self):9    def get_uranium_quantity(self):
11        return self.uranium*self.mass10        return self.uranium*self.mass
12    11    
13    def get_mass(self):12    def get_mass(self):
14        return self.mass13        return self.mass
15    14    
16    def __gt__(self,other):15    def __gt__(self,other):
17        return self.mass > other.mass16        return self.mass > other.mass
18    17    
1918
20class Person:19class Person:
2120
22    def __init__(self, position):21    def __init__(self, position):
23        self.set_position(position)22        self.set_position(position)
2423
25    def get_position(self):24    def get_position(self):
26        return self.position25        return self.position
2726
28    def set_position(self, position):27    def set_position(self, position):
29        self.position=position    28        self.position=position    
3029
3130
32class Kid(Person):31class Kid(Person):
3332
34    def __init__(self,position, initiative):33    def __init__(self,position, initiative):
35        super().__init__(position)34        super().__init__(position)
36        self.initiative = initiative35        self.initiative = initiative
37        self.basket=[]36        self.basket=[]
38        self.visited_hosts=set()37        self.visited_hosts=set()
3938
40    def __str__(self):39    def __str__(self):
41        return f"This kid has {self.initiative} initiative"    40        return f"This kid has {self.initiative} initiative"    
4241
43    def get_initiative(self):42    def get_initiative(self):
44        return self.initiative43        return self.initiative
45    44    
46    def add_candy(self, candy):45    def add_candy(self, candy):
47        self.basket.append(candy)46        self.basket.append(candy)
4847
49    def is_critical(self):48    def is_critical(self):
50        sum=049        sum=0
51        for candy in self.basket:50        for candy in self.basket:
52            sum+=candy.get_uranium_quantity()51            sum+=candy.get_uranium_quantity()
53        return sum>=2052        return sum>=20
5453
55    def __lt__(self,other):54    def __lt__(self,other):
56        return self.initiative < other.initiative        55        return self.initiative < other.initiative        
5756
58    def sort_hosts(self,host):57    def sort_hosts(self,host):
n59        return math.sqrt(math.pow(self.position[0]-host.position[0],2)+math.pow(self.position[0]-host.position[0],2))n58        dist = math.sqrt(math.pow(self.position[0]-host.position[0],2)+math.pow(self.position[1]-host.position[1],2))
59        return (dist, host.position[1], host.position[0])
60    60    
61    def go_to_door(self, hosts):61    def go_to_door(self, hosts):
62        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]62        unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
63        if not unvisited_hosts:63        if not unvisited_hosts:
64            return False64            return False
65        unvisited_hosts.sort(key=lambda host: self.sort_hosts(host))65        unvisited_hosts.sort(key=lambda host: self.sort_hosts(host))
66        selected_host = unvisited_hosts[0]66        selected_host = unvisited_hosts[0]
67        self.visited_hosts.add(selected_host)67        self.visited_hosts.add(selected_host)
68        selected_host.visiting_kids.append(self)68        selected_host.visiting_kids.append(self)
6969
70class Host(Person):70class Host(Person):
7171
72    def __init__(self, position, candies):72    def __init__(self, position, candies):
73        super().__init__(position)73        super().__init__(position)
74        self.basket=[]74        self.basket=[]
75        self.visiting_kids=[]75        self.visiting_kids=[]
76        for candy in candies:76        for candy in candies:
77            self.basket.append(Candy(candy[0],candy[1]))77            self.basket.append(Candy(candy[0],candy[1]))
7878
79    def remove_candy(self, func):79    def remove_candy(self, func):
80        if not self.basket:80        if not self.basket:
81            return 81            return 
82        candy_to_remove=func(self.basket)82        candy_to_remove=func(self.basket)
83        self.basket=[candy for candy in self.basket if candy is not candy_to_remove]83        self.basket=[candy for candy in self.basket if candy is not candy_to_remove]
84        return candy_to_remove84        return candy_to_remove
85    85    
86    def give_candy(self):86    def give_candy(self):
87        self.visiting_kids.sort(reverse=True)87        self.visiting_kids.sort(reverse=True)
88        for kid in self.visiting_kids:88        for kid in self.visiting_kids:
89            candy=self.remove_candy(func=lambda basket:sorted(basket)[0])89            candy=self.remove_candy(func=lambda basket:sorted(basket)[0])
90            if candy is not None:90            if candy is not None:
91                kid.basket.append(candy)91                kid.basket.append(candy)
92        self.visiting_kids.clear()92        self.visiting_kids.clear()
9393
9494
95class FluxCapacitor:95class FluxCapacitor:
9696
97    def __init__(self, participants):97    def __init__(self, participants):
98        self.participants=participants98        self.participants=participants
99        self.kids=[]99        self.kids=[]
100        self.hosts=[]100        self.hosts=[]
101        for person in participants:101        for person in participants:
102            if isinstance(person, Kid):102            if isinstance(person, Kid):
103                self.kids.append(person)103                self.kids.append(person)
104            elif isinstance(person, Host):104            elif isinstance(person, Host):
105                self.hosts.append(person)105                self.hosts.append(person)
106    106    
107    def get_victim(self):107    def get_victim(self):
108        kids_with_critical_mass=set()108        kids_with_critical_mass=set()
109        while(True):109        while(True):
110            for kid in self.kids:110            for kid in self.kids:
111                kid.go_to_door(self.hosts)111                kid.go_to_door(self.hosts)
112            for host in self.hosts:112            for host in self.hosts:
113                host.give_candy()113                host.give_candy()
114            for kid in self.kids:114            for kid in self.kids:
115                if kid.is_critical():115                if kid.is_critical():
116                    kids_with_critical_mass.add(kid)116                    kids_with_critical_mass.add(kid)
117            if kids_with_critical_mass:117            if kids_with_critical_mass:
118                return kids_with_critical_mass118                return kids_with_critical_mass
119            all_kids_visited_all_hosts = True119            all_kids_visited_all_hosts = True
120120
121            for kid in self.kids:121            for kid in self.kids:
122                if not kid.visited_hosts == set(self.hosts):122                if not kid.visited_hosts == set(self.hosts):
123                    all_kids_visited_all_hosts = False123                    all_kids_visited_all_hosts = False
124                    break124                    break
125125
126            if all_kids_visited_all_hosts:126            if all_kids_visited_all_hosts:
127                return None127                return None
tt128            
129candy = Candy(20, 0.3)
130person = Person((1, 2))
131kid = Kid((0, 0), 123)
132kid1 = Kid((1, 2), 15)
133kid2 = Kid((6, 5), 65)
134host = Host((2, 0), [(2, 1.0), (20, 0.6)])
135host1 = Host((0, 2), [(15, 1.0), (15, 3.5)])
136host2 = Host((2, 1), [(10, 1.0), (266, 3.5)])
137flux_capacitor = FluxCapacitor({kid,kid1,kid2, host,host1,host2})
138flux_capacitor.get_victim()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op