Предизвикателства > Осмодекемврийско пътуване > Решения > Решението на Михаил Цанков

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

2 точки общо

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

 1from itertools import permutations
 2
 3def set_partitions(collection):
 4    """Function gifted by a wise wizard."""
 5    if len(collection) == 1:
 6        yield [collection]
 7        return
 8    
 9    first = collection[0]
10    for smaller in set_partitions(collection[1:]):
11        for n, subset in enumerate(smaller):
12            yield smaller[:n] + [[ first ] + subset]  + smaller[n+1:]
13        yield [[first]] + smaller
14
15
16def organize(cars, students):
17    if len(students) > len(cars) * 4:
18        return False
19    
20    def filter_students_sets(students_set):
21        if len(students_set) > len(cars):
22            return False
23        for students in students_set:
24            if len(students) > 4:
25                return False
26        return True
27    
28    def fix_students_sets(student_set):
29        if len(student_set) < len(cars):
30            for _ in range(len(cars) - len(student_set)):
31                student_set.append([])
32        return student_set
33  
34    students_partitons_sets = list(map(fix_students_sets,filter(filter_students_sets, set_partitions(students))))
35    
36    for car_permutation in permutations(cars):
37        for students_permutation in students_partitons_sets:
38            for car, students_set in zip(car_permutation, students_permutation):
39                for student in students_set:
40                    car.add_student(student)
41            if all(student.is_comfy() for student in students):
42                return True
43            for car, students_set in zip(car_permutation, students_permutation):
44                for student in students_set:
45                    car.remove_student(student)
46    return False

.....
----------------------------------------------------------------------
Ran 5 tests in 0.070s

OK

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

n1 n
2from itertools import permutations1from itertools import permutations
32
nn3def set_partitions(collection):
4    """Function gifted by a wise wizard."""
5    if len(collection) == 1:
6        yield [collection]
7        return
8    
9    first = collection[0]
10    for smaller in set_partitions(collection[1:]):
11        for n, subset in enumerate(smaller):
12            yield smaller[:n] + [[ first ] + subset]  + smaller[n+1:]
13        yield [[first]] + smaller
14 
15 
4def organize(cars, students):16def organize(cars, students):
n5    for the_new_order in permutations(students):n17    if len(students) > len(cars) * 4:
6        the_new_order_iter = iter(the_new_order)18        return False
7        for car in cars:19    
8            for student in the_new_order_iter:20    def filter_students_sets(students_set):
21        if len(students_set) > len(cars):
22            return False
23        for students in students_set:
24            if len(students) > 4:
9                try:25                return False
26        return True
27    
28    def fix_students_sets(student_set):
29        if len(student_set) < len(cars):
30            for _ in range(len(cars) - len(student_set)):
31                student_set.append([])
32        return student_set
33  
34    students_partitons_sets = list(map(fix_students_sets,filter(filter_students_sets, set_partitions(students))))
35    
36    for car_permutation in permutations(cars):
37        for students_permutation in students_partitons_sets:
38            for car, students_set in zip(car_permutation, students_permutation):
39                for student in students_set:
10                    car.add_student(student)40                    car.add_student(student)
n11                except:n
12                    break       
13        if all(student.is_comfy() for student in the_new_order):41            if all(student.is_comfy() for student in students):
14            return True         42                return True
15        the_new_order_iter = iter(the_new_order)43            for car, students_set in zip(car_permutation, students_permutation):
16        for car in cars:44                for student in students_set:
17            for student in the_new_order_iter:
18                try:
19                    car.remove_student(student)45                    car.remove_student(student)
t20                except:t
21                    break 
22    return False46    return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op