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

Резултати
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
 17class Person:
 18
 19    def __init__(self, position):
 20        self.position = position
 21
 22    def get_position(self):
 23        return self.position
 24
 25    def set_position(self, position):
 26        self.position = position
 27
 28
 29class Kid(Person):
 30
 31    CRITICAL_QUANTITY = 20
 32
 33    def __init__(self, position, initiative):
 34        super().__init__(position)
 35        self.initiative = initiative
 36        self.basket = []
 37        self.visited_hosts = set()
 38
 39    def get_initiative(self):
 40        return self.initiative
 41
 42    def add_candy(self, candy):
 43        self.basket.append(candy)
 44
 45    def is_critical(self):
 46        sum = 0
 47        for candy in self.basket:
 48            sum += candy.get_uranium_quantity()
 49        return sum > self.CRITICAL_QUANTITY
 50
 51    def visit_closest_host(self, hosts):
 52        if len(self.visited_hosts) == len(hosts):
 53            return
 54
 55        closest_host = None
 56        for host in hosts:
 57            if host in self.visited_hosts:
 58                continue
 59            if closest_host is None:
 60                closest_host = host
 61            if math.dist(self.position, host.get_position()) < math.dist(self.position, closest_host.get_position()):
 62                closest_host = host
 63            elif math.dist(self.position, host.get_position()) == math.dist(self.position, closest_host.get_position()):
 64                if (host.get_position()[0] < closest_host.get_position()[0]
 65                    or host.get_position()[1] < closest_host.get_position()[1]):
 66                    closest_host = host
 67        
 68        closest_host.visited_by_kid(self)
 69        self.visited_hosts.add(closest_host)
 70        self.position = closest_host.get_position()
 71
 72
 73class Host(Person):
 74
 75    def __init__(self, position, candies):
 76        super().__init__(position)
 77        self.visitors = []
 78
 79        self.candies = set()
 80        for pair in candies:
 81            candy = Candy(pair[0], pair[1])
 82            self.candies.add(candy)
 83
 84    def remove_candy(self, remove_by_condition):
 85        if len(self.candies) == 0:
 86            return None
 87        removed = remove_by_condition(self.candies)
 88        self.candies.discard(removed)
 89        return removed
 90
 91    def visited_by_kid(self, kid):
 92        self.visitors.append(kid)
 93
 94    def treat(self):
 95        self.visitors.sort(key=lambda x: x.get_initiative(), reverse=True)
 96        for visitor in self.visitors:
 97            candy = self.remove_candy(removal_condition)
 98            if candy:
 99                visitor.add_candy(candy)
100        self.visitors.clear()
101
102
103class FluxCapacitor:
104
105    def __init__(self, participants):
106        self.participants = participants
107        self.hosts = [participant for participant in self.participants if isinstance(participant, Host)]
108        self.kids = [participant for participant in self.participants if isinstance(participant, Kid)]
109
110    def get_victim(self):
111        visitors = len(self.kids)
112
113        while visitors > 0:
114            visitors = len(self.kids)
115            for kid in self.kids:
116                kid.visit_closest_host(self.hosts)
117                visitors -= len(kid.visited_hosts) == len(self.hosts)
118
119            for host in self.hosts:
120                host.treat()
121
122            critical_kids = set()
123            for kid in self.kids:
124                if kid.is_critical():
125                    critical_kids.add(kid)
126
127            if len(critical_kids) > 0:
128                return critical_kids
129
130        return None
131
132
133def removal_condition(candies):
134    largest_candy = list(candies)[0]
135    for candy in candies:
136        if candy.get_mass() > largest_candy.get_mass():
137            largest_candy = candy
138    
139    return largest_candy

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

OK

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

f1import mathf1import math
22
33
4class Candy:4class Candy:
nn5 
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
89
9    def get_uranium_quantity(self):10    def get_uranium_quantity(self):
10        return self.uranium * self.mass11        return self.uranium * self.mass
1112
12    def get_mass(self):13    def get_mass(self):
13        return self.mass14        return self.mass
1415
1516
16class Person:17class Person:
nn18 
17    def __init__(self, position):19    def __init__(self, position):
18        self.position = position20        self.position = position
1921
20    def get_position(self):22    def get_position(self):
21        return self.position23        return self.position
2224
23    def set_position(self, position):25    def set_position(self, position):
24        self.position = position26        self.position = position
2527
2628
27class Kid(Person):29class Kid(Person):
nn30 
31    CRITICAL_QUANTITY = 20
32 
28    def __init__(self, position, initiative):33    def __init__(self, position, initiative):
29        super().__init__(position)34        super().__init__(position)
30        self.initiative = initiative35        self.initiative = initiative
31        self.basket = []36        self.basket = []
32        self.visited_hosts = set()37        self.visited_hosts = set()
3338
34    def get_initiative(self):39    def get_initiative(self):
35        return self.initiative40        return self.initiative
3641
37    def add_candy(self, candy):42    def add_candy(self, candy):
38        self.basket.append(candy)43        self.basket.append(candy)
3944
40    def is_critical(self):45    def is_critical(self):
41        sum = 046        sum = 0
42        for candy in self.basket:47        for candy in self.basket:
43            sum += candy.get_uranium_quantity()48            sum += candy.get_uranium_quantity()
n44            if sum > 20:n49        return sum > self.CRITICAL_QUANTITY
45                return True
46        return False
4750
48    def visit_closest_host(self, hosts):51    def visit_closest_host(self, hosts):
49        if len(self.visited_hosts) == len(hosts):52        if len(self.visited_hosts) == len(hosts):
50            return53            return
5154
52        closest_host = None55        closest_host = None
53        for host in hosts:56        for host in hosts:
54            if host in self.visited_hosts:57            if host in self.visited_hosts:
55                continue58                continue
56            if closest_host is None:59            if closest_host is None:
57                closest_host = host60                closest_host = host
58            if math.dist(self.position, host.get_position()) < math.dist(self.position, closest_host.get_position()):61            if math.dist(self.position, host.get_position()) < math.dist(self.position, closest_host.get_position()):
59                closest_host = host62                closest_host = host
60            elif math.dist(self.position, host.get_position()) == math.dist(self.position, closest_host.get_position()):63            elif math.dist(self.position, host.get_position()) == math.dist(self.position, closest_host.get_position()):
61                if (host.get_position()[0] < closest_host.get_position()[0]64                if (host.get_position()[0] < closest_host.get_position()[0]
n62                or host.get_position()[1] < closest_host.get_position()[1]):n65                    or host.get_position()[1] < closest_host.get_position()[1]):
63                    closest_host = host66                    closest_host = host
64        67        
65        closest_host.visited_by_kid(self)68        closest_host.visited_by_kid(self)
66        self.visited_hosts.add(closest_host)69        self.visited_hosts.add(closest_host)
67        self.position = closest_host.get_position()70        self.position = closest_host.get_position()
6871
6972
70class Host(Person):73class Host(Person):
nn74 
71    def __init__(self, position, candies):75    def __init__(self, position, candies):
72        super().__init__(position)76        super().__init__(position)
73        self.visitors = []77        self.visitors = []
7478
75        self.candies = set()79        self.candies = set()
76        for pair in candies:80        for pair in candies:
77            candy = Candy(pair[0], pair[1])81            candy = Candy(pair[0], pair[1])
78            self.candies.add(candy)82            self.candies.add(candy)
7983
80    def remove_candy(self, remove_by_condition):84    def remove_candy(self, remove_by_condition):
81        if len(self.candies) == 0:85        if len(self.candies) == 0:
82            return None86            return None
83        removed = remove_by_condition(self.candies)87        removed = remove_by_condition(self.candies)
84        self.candies.discard(removed)88        self.candies.discard(removed)
85        return removed89        return removed
8690
87    def visited_by_kid(self, kid):91    def visited_by_kid(self, kid):
88        self.visitors.append(kid)92        self.visitors.append(kid)
8993
90    def treat(self):94    def treat(self):
91        self.visitors.sort(key=lambda x: x.get_initiative(), reverse=True)95        self.visitors.sort(key=lambda x: x.get_initiative(), reverse=True)
92        for visitor in self.visitors:96        for visitor in self.visitors:
93            candy = self.remove_candy(removal_condition)97            candy = self.remove_candy(removal_condition)
94            if candy:98            if candy:
95                visitor.add_candy(candy)99                visitor.add_candy(candy)
96        self.visitors.clear()100        self.visitors.clear()
97101
98102
99class FluxCapacitor:103class FluxCapacitor:
nn104 
100    def __init__(self, participants):105    def __init__(self, participants):
101        self.participants = participants106        self.participants = participants
nn107        self.hosts = [participant for participant in self.participants if isinstance(participant, Host)]
108        self.kids = [participant for participant in self.participants if isinstance(participant, Kid)]
102109
103    def get_victim(self):110    def get_victim(self):
n104        hosts = [participant for participant in self.participants if isinstance(participant, Host)]n
105        kids = [participant for participant in self.participants if isinstance(participant, Kid)]
106 
107        visitors = len(kids)111        visitors = len(self.kids)
108112
109        while visitors > 0:113        while visitors > 0:
n110            visitors = len(kids)n114            visitors = len(self.kids)
111            for kid in kids:115            for kid in self.kids:
112                kid.visit_closest_host(hosts)116                kid.visit_closest_host(self.hosts)
113                visitors -= len(kid.visited_hosts) == len(hosts)117                visitors -= len(kid.visited_hosts) == len(self.hosts)
114118
n115            for host in hosts:n119            for host in self.hosts:
116                host.treat()120                host.treat()
117121
118            critical_kids = set()122            critical_kids = set()
t119            for kid in kids:t123            for kid in self.kids:
120                if kid.is_critical():124                if kid.is_critical():
121                    critical_kids.add(kid)125                    critical_kids.add(kid)
122126
123            if len(critical_kids) > 0:127            if len(critical_kids) > 0:
124                return critical_kids128                return critical_kids
125129
126        return None130        return None
127131
128132
129def removal_condition(candies):133def removal_condition(candies):
130    largest_candy = list(candies)[0]134    largest_candy = list(candies)[0]
131    for candy in candies:135    for candy in candies:
132        if candy.get_mass() > largest_candy.get_mass():136        if candy.get_mass() > largest_candy.get_mass():
133            largest_candy = candy137            largest_candy = candy
134    138    
135    return largest_candy139    return largest_candy
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op