Предизвикателства > Осмодекемврийско пътуване > Решения > Решението на Георги Кунчев

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

2 точки общо

5 успешни теста
0 неуспешни теста
Код (Optimize)

 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

Втора версия е по-бърза, но трета+ е по-четима.
История

n1class Organize:n1def 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:
n29                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                    continue9                    continue
n35                if not self.organize(cars, [x for x in students if x is not student]):n10                if not organize_(cars, rest):
36                    car.remove_student(student)11                    car.remove_student(student)
t37        return self.all_are_seated() and self.all_are_comfy()t12        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 
45organize = Organize()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class Organize:f1class Organize:
nn2    """Organize students into cars."""
23
3    def __init__(self):4    def __init__(self):
nn5        """Initializator."""
4        self.cars = None6        self.cars = None
5        self.students = None7        self.students = None
6    8    
7    def all_are_comfy(self):9    def all_are_comfy(self):
nn10        """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 False13                return False
11        return True14        return True
12    15    
13    def all_are_seated(self):16    def all_are_seated(self):
nn17        """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 False20                return False
17        return True21        return True
n18 n22    
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 False26            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 True30                    return True
30                try:31                try:
31                    car.add_student(student)32                    car.add_student(student)
32                except EnvironmentError:33                except EnvironmentError:
33                    continue34                    continue
n34                if not self(cars, [x for x in students if x is not student]):n35                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()
3738
tt39    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 
38organize = Organize()45organize = Organize()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op