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

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

7 точки общо

8 успешни теста
4 неуспешни теста
Код

  1CRITICAL_MASS = 20
  2
  3class Candy:
  4    def __init__(self, mass, uranium):
  5        self.mass = mass
  6        self.uranium = uranium
  7
  8    def get_uranuium_quantity(self):
  9        return self.uranium * self.mass
 10    
 11    def get_mass(self):
 12        return self.mass
 13    
 14
 15class Person:
 16    def __init__(self, position):
 17        self.position = position
 18
 19    def get_position(self):
 20        return self.position
 21    
 22    def set_position(self, position):
 23        self.position = position
 24
 25
 26class Kid(Person):
 27    def __init__(self, position, initiative):
 28        super().__init__(position)
 29        self.initiative = initiative
 30        self.basket = []
 31
 32    def get_initiative(self):
 33        return self.initiative
 34    
 35    def add_candy(self, new_candy):
 36        self.basket.append(new_candy)
 37
 38    def is_critical(self):  
 39        return sum(self.basket) > CRITICAL_MASS
 40    
 41
 42class Host(Person):
 43    def __init__(self, position, candies):
 44        super().__init__(position)
 45        self.candies = candies
 46
 47    def remove_candy(self, func):
 48        if len(self.candies) == 0:
 49            return None
 50        
 51        return func(self.candies)
 52    
 53    def get_candies_count(self, candies):
 54        return len(candies)
 55    
 56    def greater_distance(self, other_person):
 57        return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2)
 58    
 59
 60class FluxCapacitor:
 61    def __init__(self, participants):
 62        self.participants = participants
 63        self.kids = []
 64        self.hosts = []
 65        for participant in self.participants:
 66            if participant is Kid:
 67                self.kids.append(participant)
 68            elif participant is Host:
 69                self.hosts.append(participant)
 70
 71        self.kids.sort(key=lambda x: x.get_initiative(), reverse = True)
 72
 73        every_kid_hosts = {}
 74        for kid in self.kids:
 75            every_kid_hosts[kid] = self.hosts
 76
 77    def remove_function(self, current_candies): 
 78        current_candies.sort(key=lambda x: x.mass, reverse = True)
 79        best_candy = current_candies.pop(0)
 80        return best_candy 
 81
 82    def get_min_host(self, kid, hosts):
 83        return min(hosts, key=(lambda host:(host.greater_distance(kid), host.get_position()[0], host.get_position()[1])))
 84
 85    def get_victim(self):
 86        victims = []
 87
 88        while len(self.hosts) != 0:
 89            for kid in self.kids:
 90                best_host = self.get_min_host(kid, self.every_kid_hosts[kid])
 91
 92                best_candy = best_host.remove_candy(self.remove_function(best_host.candies))
 93                kid.add_candy(best_candy)
 94
 95                if kid.is_critical():
 96                    victims.append(kid)
 97
 98                kid.set_position(best_host.get_position())
 99
100                for host in self.hosts:
101                    if host.get_candies_count() == 0:
102                        self.hosts.discard(host)
103
104                self.every_kid_hosts[kid].discard(best_host)
105
106            if len(victims) != 0:
107                return victims
108
109        return None

E....FE..E..
======================================================================
ERROR: test_basic_usage (test.CandyTest)
Test basic usage of Candy class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 15, in test_basic_usage
self.assertEqual(candy.get_uranium_quantity(), 2.5)
AttributeError: 'Candy' object has no attribute 'get_uranium_quantity'

======================================================================
ERROR: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 65, in test_basic_usage
candy = host.remove_candy(compare_fun)
File "/tmp/solution.py", line 51, in remove_candy
return func(self.candies)
File "/tmp/test.py", line 63, in <lambda>
compare_fun = lambda candies: min(candies, key=lambda candy: candy.get_mass())
File "/tmp/test.py", line 63, in <lambda>
compare_fun = lambda candies: min(candies, key=lambda candy: candy.get_mass())
AttributeError: 'tuple' object has no attribute 'get_mass'

======================================================================
ERROR: test_candies (test.KidTest)
Test basic usage of candies in the Kid class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 48, in test_candies
self.assertFalse(kid.is_critical())
File "/tmp/solution.py", line 39, in is_critical
return sum(self.basket) > CRITICAL_MASS
TypeError: unsupported operand type(s) for +: 'int' and 'Candy'

======================================================================
FAIL: test_real_case (test.FluxCapacitorTest)
Test with real case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
File "/tmp/test.py", line 115, in test_real_case
self.assertEqual(FluxCapacitor({kid1, kid2, host1, host2}).get_victim(), {kid1, kid2})
AssertionError: None != {<solution.Kid object at 0x7f6ca47cce80>,[36 chars]4f0>}

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

FAILED (failures=1, errors=3)

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

f1CRITICAL_MASS = 20f1CRITICAL_MASS = 20
22
3class Candy:3class Candy:
4    def __init__(self, mass, uranium):4    def __init__(self, mass, uranium):
5        self.mass = mass5        self.mass = mass
6        self.uranium = uranium6        self.uranium = uranium
77
8    def get_uranuium_quantity(self):8    def get_uranuium_quantity(self):
9        return self.uranium * self.mass9        return self.uranium * self.mass
10    10    
11    def get_mass(self):11    def get_mass(self):
12        return self.mass12        return self.mass
13    13    
1414
15class Person:15class Person:
16    def __init__(self, position):16    def __init__(self, position):
17        self.position = position17        self.position = position
1818
19    def get_position(self):19    def get_position(self):
20        return self.position20        return self.position
21    21    
22    def set_position(self, position):22    def set_position(self, position):
23        self.position = position23        self.position = position
2424
2525
26class Kid(Person):26class Kid(Person):
27    def __init__(self, position, initiative):27    def __init__(self, position, initiative):
28        super().__init__(position)28        super().__init__(position)
29        self.initiative = initiative29        self.initiative = initiative
30        self.basket = []30        self.basket = []
3131
32    def get_initiative(self):32    def get_initiative(self):
33        return self.initiative33        return self.initiative
34    34    
35    def add_candy(self, new_candy):35    def add_candy(self, new_candy):
36        self.basket.append(new_candy)36        self.basket.append(new_candy)
3737
38    def is_critical(self):  38    def is_critical(self):  
39        return sum(self.basket) > CRITICAL_MASS39        return sum(self.basket) > CRITICAL_MASS
40    40    
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)
45        self.candies = candies45        self.candies = candies
4646
47    def remove_candy(self, func):47    def remove_candy(self, func):
48        if len(self.candies) == 0:48        if len(self.candies) == 0:
49            return None49            return None
50        50        
51        return func(self.candies)51        return func(self.candies)
52    52    
53    def get_candies_count(self, candies):53    def get_candies_count(self, candies):
54        return len(candies)54        return len(candies)
55    55    
56    def greater_distance(self, other_person):56    def greater_distance(self, other_person):
57        return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2)57        return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2)
58    58    
5959
60class FluxCapacitor:60class FluxCapacitor:
61    def __init__(self, participants):61    def __init__(self, participants):
62        self.participants = participants62        self.participants = participants
63        self.kids = []63        self.kids = []
64        self.hosts = []64        self.hosts = []
65        for participant in self.participants:65        for participant in self.participants:
66            if participant is Kid:66            if participant is Kid:
67                self.kids.append(participant)67                self.kids.append(participant)
t68            elif parcticipant is Host:t68            elif participant is Host:
69                self.hosts.append(participant)69                self.hosts.append(participant)
7070
71        self.kids.sort(key=lambda x: x.get_initiative(), reverse = True)71        self.kids.sort(key=lambda x: x.get_initiative(), reverse = True)
7272
73        every_kid_hosts = {}73        every_kid_hosts = {}
74        for kid in self.kids:74        for kid in self.kids:
75            every_kid_hosts[kid] = self.hosts75            every_kid_hosts[kid] = self.hosts
7676
77    def remove_function(self, current_candies): 77    def remove_function(self, current_candies): 
78        current_candies.sort(key=lambda x: x.mass, reverse = True)78        current_candies.sort(key=lambda x: x.mass, reverse = True)
79        best_candy = current_candies.pop(0)79        best_candy = current_candies.pop(0)
80        return best_candy 80        return best_candy 
8181
82    def get_min_host(self, kid, hosts):82    def get_min_host(self, kid, hosts):
83        return min(hosts, key=(lambda host:(host.greater_distance(kid), host.get_position()[0], host.get_position()[1])))83        return min(hosts, key=(lambda host:(host.greater_distance(kid), host.get_position()[0], host.get_position()[1])))
8484
85    def get_victim(self):85    def get_victim(self):
86        victims = []86        victims = []
8787
88        while len(self.hosts) != 0:88        while len(self.hosts) != 0:
89            for kid in self.kids:89            for kid in self.kids:
90                best_host = self.get_min_host(kid, self.every_kid_hosts[kid])90                best_host = self.get_min_host(kid, self.every_kid_hosts[kid])
9191
92                best_candy = best_host.remove_candy(self.remove_function(best_host.candies))92                best_candy = best_host.remove_candy(self.remove_function(best_host.candies))
93                kid.add_candy(best_candy)93                kid.add_candy(best_candy)
9494
95                if kid.is_critical():95                if kid.is_critical():
96                    victims.append(kid)96                    victims.append(kid)
9797
98                kid.set_position(best_host.get_position())98                kid.set_position(best_host.get_position())
9999
100                for host in self.hosts:100                for host in self.hosts:
101                    if host.get_candies_count() == 0:101                    if host.get_candies_count() == 0:
102                        self.hosts.discard(host)102                        self.hosts.discard(host)
103103
104                self.every_kid_hosts[kid].discard(best_host)104                self.every_kid_hosts[kid].discard(best_host)
105105
106            if len(victims) != 0:106            if len(victims) != 0:
107                return victims107                return victims
108108
109        return None109        return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1CRITICAL_MASS = 20f1CRITICAL_MASS = 20
22
3class Candy:3class Candy:
4    def __init__(self, mass, uranium):4    def __init__(self, mass, uranium):
5        self.mass = mass5        self.mass = mass
6        self.uranium = uranium6        self.uranium = uranium
77
8    def get_uranuium_quantity(self):8    def get_uranuium_quantity(self):
9        return self.uranium * self.mass9        return self.uranium * self.mass
10    10    
11    def get_mass(self):11    def get_mass(self):
12        return self.mass12        return self.mass
13    13    
1414
15class Person:15class Person:
16    def __init__(self, position):16    def __init__(self, position):
17        self.position = position17        self.position = position
1818
19    def get_position(self):19    def get_position(self):
20        return self.position20        return self.position
21    21    
22    def set_position(self, position):22    def set_position(self, position):
23        self.position = position23        self.position = position
2424
2525
26class Kid(Person):26class Kid(Person):
27    def __init__(self, position, initiative):27    def __init__(self, position, initiative):
28        super().__init__(position)28        super().__init__(position)
29        self.initiative = initiative29        self.initiative = initiative
30        self.basket = []30        self.basket = []
3131
32    def get_initiative(self):32    def get_initiative(self):
33        return self.initiative33        return self.initiative
34    34    
35    def add_candy(self, new_candy):35    def add_candy(self, new_candy):
36        self.basket.append(new_candy)36        self.basket.append(new_candy)
3737
38    def is_critical(self):  38    def is_critical(self):  
39        return sum(self.basket) > CRITICAL_MASS39        return sum(self.basket) > CRITICAL_MASS
40    40    
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)
45        self.candies = candies45        self.candies = candies
4646
47    def remove_candy(self, func):47    def remove_candy(self, func):
48        if len(self.candies) == 0:48        if len(self.candies) == 0:
49            return None49            return None
50        50        
51        return func(self.candies)51        return func(self.candies)
52    52    
53    def get_candies_count(self, candies):53    def get_candies_count(self, candies):
54        return len(candies)54        return len(candies)
55    55    
56    def greater_distance(self, other_person):56    def greater_distance(self, other_person):
57        return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2)57        return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2)
58    58    
5959
60class FluxCapacitor:60class FluxCapacitor:
61    def __init__(self, participants):61    def __init__(self, participants):
62        self.participants = participants62        self.participants = participants
63        self.kids = []63        self.kids = []
64        self.hosts = []64        self.hosts = []
65        for participant in self.participants:65        for participant in self.participants:
66            if participant is Kid:66            if participant is Kid:
67                self.kids.append(participant)67                self.kids.append(participant)
68            elif parcticipant is Host:68            elif parcticipant is Host:
69                self.hosts.append(participant)69                self.hosts.append(participant)
7070
71        self.kids.sort(key=lambda x: x.get_initiative(), reverse = True)71        self.kids.sort(key=lambda x: x.get_initiative(), reverse = True)
7272
73        every_kid_hosts = {}73        every_kid_hosts = {}
74        for kid in self.kids:74        for kid in self.kids:
75            every_kid_hosts[kid] = self.hosts75            every_kid_hosts[kid] = self.hosts
7676
77    def remove_function(self, current_candies): 77    def remove_function(self, current_candies): 
78        current_candies.sort(key=lambda x: x.mass, reverse = True)78        current_candies.sort(key=lambda x: x.mass, reverse = True)
79        best_candy = current_candies.pop(0)79        best_candy = current_candies.pop(0)
80        return best_candy 80        return best_candy 
8181
82    def get_min_host(self, kid, hosts):82    def get_min_host(self, kid, hosts):
t83        return min(hosts, key=(lamda host: host.greater_distance(kid), host.get_position()[0], host.get_position()[1]))t83        return min(hosts, key=(lambda host:(host.greater_distance(kid), host.get_position()[0], host.get_position()[1])))
8484
85    def get_victim(self):85    def get_victim(self):
86        victims = []86        victims = []
8787
88        while len(self.hosts) != 0:88        while len(self.hosts) != 0:
89            for kid in self.kids:89            for kid in self.kids:
90                best_host = self.get_min_host(kid, self.every_kid_hosts[kid])90                best_host = self.get_min_host(kid, self.every_kid_hosts[kid])
9191
92                best_candy = best_host.remove_candy(self.remove_function(best_host.candies))92                best_candy = best_host.remove_candy(self.remove_function(best_host.candies))
93                kid.add_candy(best_candy)93                kid.add_candy(best_candy)
9494
95                if kid.is_critical():95                if kid.is_critical():
96                    victims.append(kid)96                    victims.append(kid)
9797
98                kid.set_position(best_host.get_position())98                kid.set_position(best_host.get_position())
9999
100                for host in self.hosts:100                for host in self.hosts:
101                    if host.get_candies_count() == 0:101                    if host.get_candies_count() == 0:
102                        self.hosts.discard(host)102                        self.hosts.discard(host)
103103
104                self.every_kid_hosts[kid].discard(best_host)104                self.every_kid_hosts[kid].discard(best_host)
105105
106            if len(victims) != 0:106            if len(victims) != 0:
107                return victims107                return victims
108108
109        return None109        return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

nn1CRITICAL_MASS = 20
2 
1class Candy:3class Candy:
2    def __init__(self, mass, uranium):4    def __init__(self, mass, uranium):
3        self.mass = mass5        self.mass = mass
4        self.uranium = uranium6        self.uranium = uranium
57
6    def get_uranuium_quantity(self):8    def get_uranuium_quantity(self):
7        return self.uranium * self.mass9        return self.uranium * self.mass
8    10    
9    def get_mass(self):11    def get_mass(self):
10        return self.mass12        return self.mass
11    13    
1214
13class Person:15class Person:
14    def __init__(self, position):16    def __init__(self, position):
15        self.position = position17        self.position = position
1618
17    def get_position(self):19    def get_position(self):
18        return self.position20        return self.position
19    21    
20    def set_position(self, position):22    def set_position(self, position):
21        self.position = position23        self.position = position
2224
2325
24class Kid(Person):26class Kid(Person):
25    def __init__(self, position, initiative):27    def __init__(self, position, initiative):
26        super().__init__(position)28        super().__init__(position)
27        self.initiative = initiative29        self.initiative = initiative
28        self.basket = []30        self.basket = []
2931
30    def get_initiative(self):32    def get_initiative(self):
31        return self.initiative33        return self.initiative
32    34    
33    def add_candy(self, new_candy):35    def add_candy(self, new_candy):
34        self.basket.append(new_candy)36        self.basket.append(new_candy)
3537
n36    def is_critical(self):n38    def is_critical(self):  
37        mass_sum = 039        return sum(self.basket) > CRITICAL_MASS
38        for current_candy in self.basket:
39            mass_sum += current_candy.get_mass()
40 
41            if mass_sum > 20:
42                return True
43            
44        return False
45    40    
n46 n
47def remove_function(current_candies): 
48    current_candies.sort(key = lambda x: x.mass, reverse = True)
49    best_candy = current_candies.pop(0)
50    return best_candy
5141
52class Host(Person):42class Host(Person):
53    def __init__(self, position, candies):43    def __init__(self, position, candies):
54        super().__init__(position)44        super().__init__(position)
55        self.candies = candies45        self.candies = candies
5646
57    def remove_candy(self, func):47    def remove_candy(self, func):
58        if len(self.candies) == 0:48        if len(self.candies) == 0:
59            return None49            return None
60        50        
61        return func(self.candies)51        return func(self.candies)
62    52    
63    def get_candies_count(self, candies):53    def get_candies_count(self, candies):
64        return len(candies)54        return len(candies)
65    55    
n66    def distance(self, other_host):n56    def greater_distance(self, other_person):
67        return ((self.get_position()[0] - other_host.get_position()[0]) ** 2 + (self.get_position()[1] - other_host.get_position()[1]) ** 2)57        return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2)
68    58    
6959
70class FluxCapacitor:60class FluxCapacitor:
71    def __init__(self, participants):61    def __init__(self, participants):
72        self.participants = participants62        self.participants = participants
n73        kids = []n63        self.kids = []
64        self.hosts = []
74        for participant in self.participants:65        for participant in self.participants:
75            if participant is Kid:66            if participant is Kid:
n76                kids.append(participant)n67                self.kids.append(participant)
68            elif parcticipant is Host:
69                self.hosts.append(participant)
7770
n78        kids.sort(key = lambda x: x.get_initiative(), reverse = True)n71        self.kids.sort(key=lambda x: x.get_initiative(), reverse = True)
79 
80        hosts = []
81        for participant in self.participants:
82            if participant is Host:
83                hosts.append(participant)
8472
85        every_kid_hosts = {}73        every_kid_hosts = {}
n86        for kid in kids:n74        for kid in self.kids:
87            every_kid_hosts[kid] = hosts75            every_kid_hosts[kid] = self.hosts
76 
77    def remove_function(self, current_candies): 
78        current_candies.sort(key=lambda x: x.mass, reverse = True)
79        best_candy = current_candies.pop(0)
80        return best_candy 
81 
82    def get_min_host(self, kid, hosts):
83        return min(hosts, key=(lamda host: host.greater_distance(kid), host.get_position()[0], host.get_position()[1]))
8884
89    def get_victim(self):85    def get_victim(self):
90        victims = []86        victims = []
9187
92        while len(self.hosts) != 0:88        while len(self.hosts) != 0:
93            for kid in self.kids:89            for kid in self.kids:
n94                def find_best_host(host):n90                best_host = self.get_min_host(kid, self.every_kid_hosts[kid])
95                    curr_distance = host.greater_distance(kid)
96                    return(curr_distance, host.get_position()[0], host.get_position()[1])
97                
98                best_host = min(self.every_kid_hosts[kid], key = find_best_host)
9991
n100                best_candy = best_host.remove_candy(remove_function)n92                best_candy = best_host.remove_candy(self.remove_function(best_host.candies))
101                kid.add_candy(best_candy)93                kid.add_candy(best_candy)
10294
t103                if(kid.is_critical()):t95                if kid.is_critical():
104                    victims.append(kid)96                    victims.append(kid)
10597
106                kid.set_position(best_host.get_position())98                kid.set_position(best_host.get_position())
10799
108                for host in self.hosts:100                for host in self.hosts:
109                    if host.get_candies_count() == 0:101                    if host.get_candies_count() == 0:
110                        self.hosts.discard(host)102                        self.hosts.discard(host)
111103
112                self.every_kid_hosts[kid].discard(best_host)104                self.every_kid_hosts[kid].discard(best_host)
113105
114            if len(victims) != 0:106            if len(victims) != 0:
115                return victims107                return victims
116108
117        return None109        return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

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_uranuium_quantity(self):6    def get_uranuium_quantity(self):
7        return self.uranium * self.mass7        return self.uranium * self.mass
8    8    
9    def get_mass(self):9    def get_mass(self):
10        return self.mass10        return self.mass
11    11    
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
19    19    
20    def set_position(self, position):20    def set_position(self, position):
21        self.position = position21        self.position = 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
28        self.basket = []28        self.basket = []
2929
30    def get_initiative(self):30    def get_initiative(self):
31        return self.initiative31        return self.initiative
32    32    
33    def add_candy(self, new_candy):33    def add_candy(self, new_candy):
34        self.basket.append(new_candy)34        self.basket.append(new_candy)
3535
36    def is_critical(self):36    def is_critical(self):
37        mass_sum = 037        mass_sum = 0
38        for current_candy in self.basket:38        for current_candy in self.basket:
39            mass_sum += current_candy.get_mass()39            mass_sum += current_candy.get_mass()
4040
41            if mass_sum > 20:41            if mass_sum > 20:
42                return True42                return True
43            43            
44        return False44        return False
45    45    
4646
47def remove_function(current_candies): 47def remove_function(current_candies): 
n48    current_candies.sort(key = lambda objobj.mass, reverse = True)n48    current_candies.sort(key = lambda xx.mass, reverse = True)
49    best_candy = current_candies.pop(0)49    best_candy = current_candies.pop(0)
50    return best_candy50    return best_candy
5151
52class Host(Person):52class Host(Person):
53    def __init__(self, position, candies):53    def __init__(self, position, candies):
54        super().__init__(position)54        super().__init__(position)
55        self.candies = candies55        self.candies = candies
5656
57    def remove_candy(self, func):57    def remove_candy(self, func):
58        if len(self.candies) == 0:58        if len(self.candies) == 0:
59            return None59            return None
60        60        
61        return func(self.candies)61        return func(self.candies)
62    62    
63    def get_candies_count(self, candies):63    def get_candies_count(self, candies):
64        return len(candies)64        return len(candies)
65    65    
66    def distance(self, other_host):66    def distance(self, other_host):
67        return ((self.get_position()[0] - other_host.get_position()[0]) ** 2 + (self.get_position()[1] - other_host.get_position()[1]) ** 2)67        return ((self.get_position()[0] - other_host.get_position()[0]) ** 2 + (self.get_position()[1] - other_host.get_position()[1]) ** 2)
68    68    
6969
70class FluxCapacitor:70class FluxCapacitor:
71    def __init__(self, participants):71    def __init__(self, participants):
72        self.participants = participants72        self.participants = participants
n73        kids = set()n73        kids = []
74        for participant in self.participants:74        for participant in self.participants:
75            if participant is Kid:75            if participant is Kid:
n76                kids.add(participant)n76                kids.append(participant)
7777
n78        kids = kids.sort(key = lambda objobj.get_initiative(), reverse = True)n78        kids.sort(key = lambda xx.get_initiative(), reverse = True)
7979
n80        hosts = set()n80        hosts = []
81        for participant in self.participants:81        for participant in self.participants:
82            if participant is Host:82            if participant is Host:
n83                hosts.add(participant)n83                hosts.append(participant)
8484
85        every_kid_hosts = {}85        every_kid_hosts = {}
86        for kid in kids:86        for kid in kids:
87            every_kid_hosts[kid] = hosts87            every_kid_hosts[kid] = hosts
8888
89    def get_victim(self):89    def get_victim(self):
n90        victims = set()n90        victims = []
9191
92        while len(self.hosts) != 0:92        while len(self.hosts) != 0:
93            for kid in self.kids:93            for kid in self.kids:
94                def find_best_host(host):94                def find_best_host(host):
95                    curr_distance = host.greater_distance(kid)95                    curr_distance = host.greater_distance(kid)
96                    return(curr_distance, host.get_position()[0], host.get_position()[1])96                    return(curr_distance, host.get_position()[0], host.get_position()[1])
97                97                
98                best_host = min(self.every_kid_hosts[kid], key = find_best_host)98                best_host = min(self.every_kid_hosts[kid], key = find_best_host)
9999
100                best_candy = best_host.remove_candy(remove_function)100                best_candy = best_host.remove_candy(remove_function)
101                kid.add_candy(best_candy)101                kid.add_candy(best_candy)
102102
103                if(kid.is_critical()):103                if(kid.is_critical()):
t104                    victims.add(kid)t104                    victims.append(kid)
105105
106                kid.set_position(best_host.get_position())106                kid.set_position(best_host.get_position())
107107
108                for host in self.hosts:108                for host in self.hosts:
109                    if host.get_candies_count() == 0:109                    if host.get_candies_count() == 0:
110                        self.hosts.discard(host)110                        self.hosts.discard(host)
111111
112                self.every_kid_hosts[kid].discard(best_host)112                self.every_kid_hosts[kid].discard(best_host)
113113
114            if len(victims) != 0:114            if len(victims) != 0:
115                return victims115                return victims
116116
117        return None117        return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op