f | class Candy: | f | class Candy: |
n | | n | |
| def __init__(self, mass, uranium): | | def __init__(self, mass, uranium): |
| self.mass = mass | | self.mass = mass |
| self.uranium = uranium | | self.uranium = uranium |
| | | |
| def get_uranium_quantity(self): | | def get_uranium_quantity(self): |
| return self.mass * self.uranium | | return self.mass * self.uranium |
| | | |
| def get_mass(self): | | def get_mass(self): |
| return self.mass | | return self.mass |
| | | |
| class Person: | | class Person: |
n | | n | |
| def __init__(self, position): | | def __init__(self, position): |
n | self.position = position # tupel of ints | n | self.position = position # tuple of ints |
| | | |
| def get_position(self): | | def get_position(self): |
| return self.position | | return self.position |
| | | |
| def set_position(self, new_position): | | def set_position(self, new_position): |
| self.position = new_position | | self.position = new_position |
| | | |
| class Kid(Person): | | class Kid(Person): |
n | | n | |
| | | THRESHOLD = 20 |
| | | |
| def __init__(self, position, initiative): | | def __init__(self, position, initiative): |
| Person.__init__(self, position) | | Person.__init__(self, position) |
| self.initiative = initiative | | self.initiative = initiative |
| self.candy_arr = [] # here I will gather the kids candies | | self.candy_arr = [] # here I will gather the kids candies |
| | | |
| def get_initiative(self): | | def get_initiative(self): |
| return self.initiative | | return self.initiative |
| | | |
| def add_candy(self, candy): # put a candy in the candy_arr | | def add_candy(self, candy): # put a candy in the candy_arr |
| self.candy_arr.append(candy) | | self.candy_arr.append(candy) |
| | | |
| def is_critical(self): | | def is_critical(self): |
n | return sum(candy.get_uranium_quantity() for candy in self.candy_arr) > 20 | n | return sum(candy.get_uranium_quantity() for candy in self.candy_arr) > Kid.THRESHOLD |
| | | |
n | | n | @staticmethod |
| def sort_hosts(self, host): | | def __sort_hosts(host): |
| x, y = host.get_position() | | x, y = host.get_position() |
| return (x, y) | | return (x, y) |
| | | |
| def find_nearest_host(self, hosts): | | def find_nearest_host(self, hosts): |
| if not hosts: | | if not hosts: |
| return None | | return None |
| else: | | else: |
n | sorted_hosts = sorted(hosts, key=self.sort_hosts) | n | sorted_hosts = sorted(hosts, key=self.__sort_hosts) # min func can be used |
| nearest_host = sorted_hosts[0] | | nearest_host = sorted_hosts[0] |
| | | |
| return nearest_host | | return nearest_host |
| | | |
| class Host(Person): | | class Host(Person): |
n | | n | |
| def __init__(self, position, candies): | | def __init__(self, position, candies): |
| Person.__init__(self, position) | | Person.__init__(self, position) |
| self.candy_arr = [Candy(mass, uranium) for mass, uranium in candies] # list of tupes of two elements ( int, float ) | | self.candy_arr = [Candy(mass, uranium) for mass, uranium in candies] # list of tupes of two elements ( int, float ) |
| | | |
| def remove_candy(self, func): | | def remove_candy(self, func): |
| if not self.candy_arr: | | if not self.candy_arr: |
| return None | | return None |
| | | |
| chosen_candy = func(self.candy_arr) | | chosen_candy = func(self.candy_arr) |
| self.candy_arr.remove(chosen_candy) | | self.candy_arr.remove(chosen_candy) |
| return chosen_candy | | return chosen_candy |
| | | |
n | | n | def candy_max_mass(candies): # max mass |
| | | |
| | | def get_mass(candy): |
| | | return candy.get_mass() |
| | | |
| | | return max(candies, key=get_mass) |
| | | |
| class FluxCapacitor: | | class FluxCapacitor: |
n | | n | |
| def __init__(self, participants): | | def __init__(self, participants): |
| self.participants = participants # set of Kid and Host instances | | self.participants = participants # set of Kid and Host instances |
| | | |
n | | n | @staticmethod |
| def sort_kids_by_initiative(self, kid): | | def __sort_kids_by_initiative(kid): |
| return kid.get_initiative() | | return kid.get_initiative() |
| | | |
| def get_victim(self): | | def get_victim(self): |
| victims = set() | | victims = set() |
n | | n | |
| kids = set() | | kids = set() |
n | | n | hosts = set() |
| for kid in self.participants: | | for item in self.participants: |
| if type(kid) == Kid: | | if type(item) == Kid: |
| kids.add(kid) | | kids.add(item) |
| | | elif type(item) == Host: |
| | | hosts.add(item) |
| | | |
n | sorted_kids = sorted(kids, key=self.sort_kids_by_initiative, reverse=True) | n | sorted_kids = sorted(kids, key=self.__sort_kids_by_initiative, reverse=True) |
| | | |
| for kid in sorted_kids: | | for kid in sorted_kids: |
n | nearest_host = kid.find_nearest_host([host for host in self.participants if type(host) == Host]) | n | nearest_host = kid.find_nearest_host([host for host in hosts]) |
| if nearest_host: | | if nearest_host: |
| for candy in nearest_host.candy_arr: | | for candy in nearest_host.candy_arr: |
| kid.add_candy(candy) | | kid.add_candy(candy) |
n | nearest_host.arr = [] | n | |
| | | |
| for kid in sorted_kids: | | for kid in sorted_kids: |
| if kid.is_critical(): # kid is critical -> true or false, < or > 20 | | if kid.is_critical(): # kid is critical -> true or false, < or > 20 |
| victims.add(kid) | | victims.add(kid) |
| | | |
| if victims: | | if victims: |
| return victims | | return victims |
| return None | | return None |
t | | t | |
| def get_mass(candy): | | |
| return candy.get_mass() | | |
| | | |
| def custom_func(candies): # max mass | | |
| return max(candies, key=get_mass) | | |