1def organize(cars, students):
2 def organize_(cars, students_):
3 for student in students_:
4 rest = [student_ for student_ in students_ if student_ is not student]
5 for car in cars:
6 try:
7 car.add_student(student)
8 except EnvironmentError:
9 continue
10 if not organize_(cars, rest):
11 car.remove_student(student)
12 return all(student.is_comfy() for student in students)
13 return organize_(cars, students)
.....
----------------------------------------------------------------------
Ran 5 tests in 0.003s
OK
Георги Кунчев
06.12.2023 16:40Втора версия е по-бърза, но трета+ е по-четима.
|
| n | 1 | class Organize: | n | 1 | def organize(cars, students): |
| 2 | """Organize students into cars.""" | ||||
| 3 | |||||
| 4 | def __init__(self): | ||||
| 5 | """Initializator.""" | ||||
| 6 | self.cars = None | ||||
| 7 | self.students = None | ||||
| 8 | |||||
| 9 | def all_are_comfy(self): | ||||
| 10 | """Check if all students are comfy.""" | ||||
| 11 | for student in self.students: | ||||
| 12 | if student.is_comfy() is False: | ||||
| 13 | return False | ||||
| 14 | return True | ||||
| 15 | |||||
| 16 | def all_are_seated(self): | ||||
| 17 | """Check if all students are seated.""" | ||||
| 18 | for student in self.students: | ||||
| 19 | if student.is_comfy() is None: | ||||
| 20 | return False | ||||
| 21 | return True | ||||
| 22 | |||||
| 23 | def organize(self, cars, students): | 2 | def organize_(cars, students_): | ||
| 24 | """Organizer students into car by backtracking.""" | ||||
| 25 | if not self.all_are_comfy(): | ||||
| 26 | return False | ||||
| 27 | for student in students: | 3 | for student in students_: | ||
| 4 | rest = [student_ for student_ in students_ if student_ is not student] | ||||
| 28 | for car in cars: | 5 | for car in cars: | ||
| n | 29 | if self.all_are_seated() and self.all_are_comfy(): | n | ||
| 30 | return True | ||||
| 31 | try: | 6 | try: | ||
| 32 | car.add_student(student) | 7 | car.add_student(student) | ||
| 33 | except EnvironmentError: | 8 | except EnvironmentError: | ||
| 34 | continue | 9 | continue | ||
| n | 35 | if not self.organize(cars, [x for x in students if x is not student]): | n | 10 | if not organize_(cars, rest): |
| 36 | car.remove_student(student) | 11 | car.remove_student(student) | ||
| t | 37 | return self.all_are_seated() and self.all_are_comfy() | t | 12 | return all(student.is_comfy() for student in students) |
| 38 | |||||
| 39 | def __call__(self, cars, students): | ||||
| 40 | """Attempt to organize students into cars.""" | ||||
| 41 | self.cars = cars | ||||
| 42 | self.students = students | ||||
| 43 | return self.organize(cars, students) | 13 | return organize_(cars, students) | ||
| 44 | |||||
| 45 | organize = Organize() |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | class Organize: | f | 1 | class Organize: |
| n | n | 2 | """Organize students into cars.""" | ||
| 2 | 3 | ||||
| 3 | def __init__(self): | 4 | def __init__(self): | ||
| n | n | 5 | """Initializator.""" | ||
| 4 | self.cars = None | 6 | self.cars = None | ||
| 5 | self.students = None | 7 | self.students = None | ||
| 6 | 8 | ||||
| 7 | def all_are_comfy(self): | 9 | def all_are_comfy(self): | ||
| n | n | 10 | """Check if all students are comfy.""" | ||
| 8 | for student in self.students: | 11 | for student in self.students: | ||
| 9 | if student.is_comfy() is False: | 12 | if student.is_comfy() is False: | ||
| 10 | return False | 13 | return False | ||
| 11 | return True | 14 | return True | ||
| 12 | 15 | ||||
| 13 | def all_are_seated(self): | 16 | def all_are_seated(self): | ||
| n | n | 17 | """Check if all students are seated.""" | ||
| 14 | for student in self.students: | 18 | for student in self.students: | ||
| 15 | if student.is_comfy() is None: | 19 | if student.is_comfy() is None: | ||
| 16 | return False | 20 | return False | ||
| 17 | return True | 21 | return True | ||
| n | 18 | n | 22 | ||
| 19 | def __call__(self, cars, students): | 23 | def organize(self, cars, students): | ||
| 20 | if self.cars is None: | 24 | """Organizer students into car by backtracking.""" | ||
| 21 | self.cars = cars | ||||
| 22 | if self.students is None: | ||||
| 23 | self.students = students | ||||
| 24 | if not self.all_are_comfy(): | 25 | if not self.all_are_comfy(): | ||
| 25 | return False | 26 | return False | ||
| 26 | for student in students: | 27 | for student in students: | ||
| 27 | for car in cars: | 28 | for car in cars: | ||
| 28 | if self.all_are_seated() and self.all_are_comfy(): | 29 | if self.all_are_seated() and self.all_are_comfy(): | ||
| 29 | return True | 30 | return True | ||
| 30 | try: | 31 | try: | ||
| 31 | car.add_student(student) | 32 | car.add_student(student) | ||
| 32 | except EnvironmentError: | 33 | except EnvironmentError: | ||
| 33 | continue | 34 | continue | ||
| n | 34 | if not self(cars, [x for x in students if x is not student]): | n | 35 | if not self.organize(cars, [x for x in students if x is not student]): |
| 35 | car.remove_student(student) | 36 | car.remove_student(student) | ||
| 36 | return self.all_are_seated() and self.all_are_comfy() | 37 | return self.all_are_seated() and self.all_are_comfy() | ||
| 37 | 38 | ||||
| t | t | 39 | def __call__(self, cars, students): | ||
| 40 | """Attempt to organize students into cars.""" | ||||
| 41 | self.cars = cars | ||||
| 42 | self.students = students | ||||
| 43 | return self.organize(cars, students) | ||||
| 44 | |||||
| 38 | organize = Organize() | 45 | organize = Organize() |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||