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

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

10 точки общо

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

  1class Candy:
  2
  3    def __init__(self, mass, uranium):
  4        self.mass = mass
  5        self.uranium = uranium
  6
  7    def get_uranium_quantity(self):
  8        return self.mass * self.uranium
  9
 10    def get_mass(self):
 11        return self.mass
 12
 13class Person:
 14
 15    def __init__(self, position):
 16        self.position = position # tuple of ints
 17
 18    def get_position(self):
 19        return self.position
 20
 21    def set_position(self, new_position):
 22        self.position = new_position
 23
 24class Kid(Person):
 25
 26    THRESHOLD = 20
 27
 28    def __init__(self, position, initiative):
 29        Person.__init__(self, position)
 30        self.initiative = initiative
 31        self.candy_arr = [] # here I will gather the kids candies
 32
 33    def get_initiative(self):
 34        return self.initiative
 35
 36    def add_candy(self, candy): # put a candy in the candy_arr
 37        self.candy_arr.append(candy)
 38
 39    def is_critical(self): 
 40        return sum(candy.get_uranium_quantity() for candy in self.candy_arr) > Kid.THRESHOLD
 41
 42    @staticmethod
 43    def __sort_hosts(host):
 44        x, y = host.get_position()
 45        return (x, y)
 46
 47    def find_nearest_host(self, hosts):
 48        if not hosts:
 49            return None
 50        else:
 51            sorted_hosts = sorted(hosts, key=self.__sort_hosts) # min func can be used
 52            nearest_host = sorted_hosts[0]
 53
 54        return nearest_host
 55
 56class Host(Person):
 57
 58    def __init__(self, position, candies):
 59        Person.__init__(self, position)
 60        self.candy_arr = [Candy(mass, uranium) for mass, uranium in candies] # list of tupes of two elements ( int, float )
 61
 62    def remove_candy(self, func):
 63        if not self.candy_arr:
 64            return None
 65
 66        chosen_candy = func(self.candy_arr)
 67        self.candy_arr.remove(chosen_candy)
 68        return chosen_candy
 69
 70    def candy_max_mass(candies): # max mass
 71
 72        def get_mass(candy):
 73            return candy.get_mass()
 74
 75        return max(candies, key=get_mass)
 76
 77class FluxCapacitor:
 78
 79    def __init__(self, participants):
 80        self.participants = participants # set of Kid and Host instances
 81
 82    @staticmethod
 83    def __sort_kids_by_initiative(kid):
 84        return kid.get_initiative()
 85
 86    def get_victim(self):
 87        victims = set()
 88
 89        kids = set()
 90        hosts = set()
 91        for item in self.participants:
 92            if type(item) == Kid:
 93                kids.add(item)
 94            elif type(item) == Host:
 95                hosts.add(item)
 96
 97        sorted_kids = sorted(kids, key=self.__sort_kids_by_initiative, reverse=True)
 98
 99        for kid in sorted_kids:
100            nearest_host = kid.find_nearest_host([host for host in hosts])
101            if nearest_host:
102                for candy in nearest_host.candy_arr:
103                    kid.add_candy(candy)
104
105        for kid in sorted_kids:
106            if kid.is_critical(): # kid is critical -> true or false, < or > 20
107                victims.add(kid)
108
109        if victims:
110            return victims
111        return None

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

OK

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

f1class Candy:f1class Candy:
nn2 
2    def __init__(self, mass, uranium):3    def __init__(self, mass, uranium):
3        self.mass = mass4        self.mass = mass
4        self.uranium = uranium5        self.uranium = uranium
56
6    def get_uranium_quantity(self):7    def get_uranium_quantity(self):
7        return self.mass * self.uranium8        return self.mass * self.uranium
89
9    def get_mass(self):10    def get_mass(self):
10        return self.mass11        return self.mass
1112
12class Person:13class Person:
nn14 
13    def __init__(self, position):15    def __init__(self, position):
n14        self.position = position # tupel of intsn16        self.position = position # tuple of ints
1517
16    def get_position(self):18    def get_position(self):
17        return self.position19        return self.position
1820
19    def set_position(self, new_position):21    def set_position(self, new_position):
20        self.position = new_position22        self.position = new_position
2123
22class Kid(Person):24class Kid(Person):
nn25 
26    THRESHOLD = 20
27 
23    def __init__(self, position, initiative):28    def __init__(self, position, initiative):
24        Person.__init__(self, position)29        Person.__init__(self, position)
25        self.initiative = initiative30        self.initiative = initiative
26        self.candy_arr = [] # here I will gather the kids candies31        self.candy_arr = [] # here I will gather the kids candies
2732
28    def get_initiative(self):33    def get_initiative(self):
29        return self.initiative34        return self.initiative
3035
31    def add_candy(self, candy): # put a candy in the candy_arr36    def add_candy(self, candy): # put a candy in the candy_arr
32        self.candy_arr.append(candy)37        self.candy_arr.append(candy)
3338
34    def is_critical(self): 39    def is_critical(self): 
n35        return sum(candy.get_uranium_quantity() for candy in self.candy_arr) > 20n40        return sum(candy.get_uranium_quantity() for candy in self.candy_arr) > Kid.THRESHOLD
3641
nn42    @staticmethod
37    def sort_hosts(self, host):43    def __sort_hosts(host):
38        x, y = host.get_position()44        x, y = host.get_position()
39        return (x, y)45        return (x, y)
4046
41    def find_nearest_host(self, hosts):47    def find_nearest_host(self, hosts):
42        if not hosts:48        if not hosts:
43            return None49            return None
44        else:50        else:
n45            sorted_hosts = sorted(hosts, key=self.sort_hosts)n51            sorted_hosts = sorted(hosts, key=self.__sort_hosts) # min func can be used
46            nearest_host = sorted_hosts[0]52            nearest_host = sorted_hosts[0]
4753
48        return nearest_host54        return nearest_host
4955
50class Host(Person):56class Host(Person):
nn57 
51    def __init__(self, position, candies):58    def __init__(self, position, candies):
52        Person.__init__(self, position)59        Person.__init__(self, position)
53        self.candy_arr = [Candy(mass, uranium) for mass, uranium in candies] # list of tupes of two elements ( int, float )60        self.candy_arr = [Candy(mass, uranium) for mass, uranium in candies] # list of tupes of two elements ( int, float )
5461
55    def remove_candy(self, func):62    def remove_candy(self, func):
56        if not self.candy_arr:63        if not self.candy_arr:
57            return None64            return None
5865
59        chosen_candy = func(self.candy_arr)66        chosen_candy = func(self.candy_arr)
60        self.candy_arr.remove(chosen_candy)67        self.candy_arr.remove(chosen_candy)
61        return chosen_candy68        return chosen_candy
6269
nn70    def candy_max_mass(candies): # max mass
71 
72        def get_mass(candy):
73            return candy.get_mass()
74 
75        return max(candies, key=get_mass)
76 
63class FluxCapacitor:77class FluxCapacitor:
nn78 
64    def __init__(self, participants):79    def __init__(self, participants):
65        self.participants = participants # set of Kid and Host instances80        self.participants = participants # set of Kid and Host instances
6681
nn82    @staticmethod
67    def sort_kids_by_initiative(self, kid):83    def __sort_kids_by_initiative(kid):
68        return kid.get_initiative()84        return kid.get_initiative()
6985
70    def get_victim(self):86    def get_victim(self):
71        victims = set()87        victims = set()
n72        n88 
73        kids = set()89        kids = set()
nn90        hosts = set()
74        for kid in self.participants:91        for item in self.participants:
75            if type(kid) == Kid:92            if type(item) == Kid:
76                kids.add(kid)93                kids.add(item)
94            elif type(item) == Host:
95                hosts.add(item)
7796
n78        sorted_kids = sorted(kids, key=self.sort_kids_by_initiative, reverse=True)n97        sorted_kids = sorted(kids, key=self.__sort_kids_by_initiative, reverse=True)
7998
80        for kid in sorted_kids:99        for kid in sorted_kids:
n81            nearest_host = kid.find_nearest_host([host for host in self.participants if type(host) == Host])n100            nearest_host = kid.find_nearest_host([host for host in hosts])
82            if nearest_host:101            if nearest_host:
83                for candy in nearest_host.candy_arr:102                for candy in nearest_host.candy_arr:
84                    kid.add_candy(candy)103                    kid.add_candy(candy)
n85                nearest_host.arr = []n
86104
87        for kid in sorted_kids:105        for kid in sorted_kids:
88            if kid.is_critical(): # kid is critical -> true or false, < or > 20106            if kid.is_critical(): # kid is critical -> true or false, < or > 20
89                victims.add(kid)107                victims.add(kid)
90108
91        if victims:109        if victims:
92            return victims110            return victims
93        return None111        return None
t94 t
95def get_mass(candy):
96    return candy.get_mass()
97 
98def custom_func(candies): # max mass
99    return max(candies, key=get_mass)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op