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

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

2 точки общо

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

 1from itertools import combinations, permutations
 2from copy import deepcopy
 3
 4def is_everyone_comfy(students):
 5    """Check if everyone is comfy in their current car."""
 6    for student in students:
 7        if not student.is_comfy():
 8            return False
 9            
10    return True
11            
12# implementation from StackOverflow - same as more_itertools.set_partitions
13def set_partitions_helper(collection):
14    """Returns a generator that yields set partitions of a collection."""
15    if len(collection) == 1:
16        yield [collection]
17        return
18    
19    first = collection[0]
20    for smaller in set_partitions_helper(collection[1:]):
21        for n, subset in enumerate(smaller):
22            yield smaller[:n] + [[ first ] + subset]  + smaller[n+1:]
23        yield [[first]] + smaller
24        
25
26def organize(cars, students):
27    """Organize the students in cars."""
28    if len(students) == 0:
29        return True
30    
31    if len(cars) * 4 < len(students):
32        return False
33    
34    students_configurations_generator = set_partitions_helper(students)
35
36    cars_configurations = [j for i in range(1, len(cars) + 1) for j in list(combinations(cars, i))]
37    
38    try:
39       while True:
40            students_configuration = next(students_configurations_generator)
41            if  not all(len(i) <= 4 for i in students_configuration):
42                continue
43            for cars_conf in cars_configurations:
44                if len(cars_conf) != len(students_configuration):
45                    continue
46
47                for cars_perm in permutations(cars_conf):
48                    cars_copy = deepcopy(cars_perm)
49                    students_copy = deepcopy(students_configuration)
50                    for i in range(len(students_configuration)):
51                        for student in students_copy[i]:
52                            cars_copy[i].add_student(student)
53
54                    if is_everyone_comfy([item for sublist in students_copy for item in sublist]):
55                        for i in range(len(students_configuration)):
56                            for student in students_configuration[i]:
57                                cars_perm[i].add_student(student)
58                        return True
59    except StopIteration as s:
60        return False
61 

.....
----------------------------------------------------------------------
Ran 5 tests in 0.006s

OK

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

f1from itertools import combinations, permutationsf1from itertools import combinations, permutations
2from copy import deepcopy2from copy import deepcopy
t3 t
4from challenge_04_helper_classes import Car, Student
53
6def is_everyone_comfy(students):4def is_everyone_comfy(students):
7    """Check if everyone is comfy in their current car."""5    """Check if everyone is comfy in their current car."""
8    for student in students:6    for student in students:
9        if not student.is_comfy():7        if not student.is_comfy():
10            return False8            return False
11            9            
12    return True10    return True
13            11            
14# implementation from StackOverflow - same as more_itertools.set_partitions12# implementation from StackOverflow - same as more_itertools.set_partitions
15def set_partitions_helper(collection):13def set_partitions_helper(collection):
16    """Returns a generator that yields set partitions of a collection."""14    """Returns a generator that yields set partitions of a collection."""
17    if len(collection) == 1:15    if len(collection) == 1:
18        yield [collection]16        yield [collection]
19        return17        return
20    18    
21    first = collection[0]19    first = collection[0]
22    for smaller in set_partitions_helper(collection[1:]):20    for smaller in set_partitions_helper(collection[1:]):
23        for n, subset in enumerate(smaller):21        for n, subset in enumerate(smaller):
24            yield smaller[:n] + [[ first ] + subset]  + smaller[n+1:]22            yield smaller[:n] + [[ first ] + subset]  + smaller[n+1:]
25        yield [[first]] + smaller23        yield [[first]] + smaller
26        24        
2725
28def organize(cars, students):26def organize(cars, students):
29    """Organize the students in cars."""27    """Organize the students in cars."""
30    if len(students) == 0:28    if len(students) == 0:
31        return True29        return True
32    30    
33    if len(cars) * 4 < len(students):31    if len(cars) * 4 < len(students):
34        return False32        return False
35    33    
36    students_configurations_generator = set_partitions_helper(students)34    students_configurations_generator = set_partitions_helper(students)
3735
38    cars_configurations = [j for i in range(1, len(cars) + 1) for j in list(combinations(cars, i))]36    cars_configurations = [j for i in range(1, len(cars) + 1) for j in list(combinations(cars, i))]
39    37    
40    try:38    try:
41       while True:39       while True:
42            students_configuration = next(students_configurations_generator)40            students_configuration = next(students_configurations_generator)
43            if  not all(len(i) <= 4 for i in students_configuration):41            if  not all(len(i) <= 4 for i in students_configuration):
44                continue42                continue
45            for cars_conf in cars_configurations:43            for cars_conf in cars_configurations:
46                if len(cars_conf) != len(students_configuration):44                if len(cars_conf) != len(students_configuration):
47                    continue45                    continue
4846
49                for cars_perm in permutations(cars_conf):47                for cars_perm in permutations(cars_conf):
50                    cars_copy = deepcopy(cars_perm)48                    cars_copy = deepcopy(cars_perm)
51                    students_copy = deepcopy(students_configuration)49                    students_copy = deepcopy(students_configuration)
52                    for i in range(len(students_configuration)):50                    for i in range(len(students_configuration)):
53                        for student in students_copy[i]:51                        for student in students_copy[i]:
54                            cars_copy[i].add_student(student)52                            cars_copy[i].add_student(student)
5553
56                    if is_everyone_comfy([item for sublist in students_copy for item in sublist]):54                    if is_everyone_comfy([item for sublist in students_copy for item in sublist]):
57                        for i in range(len(students_configuration)):55                        for i in range(len(students_configuration)):
58                            for student in students_configuration[i]:56                            for student in students_configuration[i]:
59                                cars_perm[i].add_student(student)57                                cars_perm[i].add_student(student)
60                        return True58                        return True
61    except StopIteration as s:59    except StopIteration as s:
62        return False60        return False
63 61 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op