f | from itertools import combinations, permutations | f | from itertools import combinations, permutations |
| from copy import deepcopy | | from copy import deepcopy |
t | | t | |
| from challenge_04_helper_classes import Car, Student | | |
| | | |
| def is_everyone_comfy(students): | | def is_everyone_comfy(students): |
| """Check if everyone is comfy in their current car.""" | | """Check if everyone is comfy in their current car.""" |
| for student in students: | | for student in students: |
| if not student.is_comfy(): | | if not student.is_comfy(): |
| return False | | return False |
| | | |
| return True | | return True |
| | | |
| # implementation from StackOverflow - same as more_itertools.set_partitions | | # implementation from StackOverflow - same as more_itertools.set_partitions |
| def set_partitions_helper(collection): | | def set_partitions_helper(collection): |
| """Returns a generator that yields set partitions of a collection.""" | | """Returns a generator that yields set partitions of a collection.""" |
| if len(collection) == 1: | | if len(collection) == 1: |
| yield [collection] | | yield [collection] |
| return | | return |
| | | |
| first = collection[0] | | first = collection[0] |
| for smaller in set_partitions_helper(collection[1:]): | | for smaller in set_partitions_helper(collection[1:]): |
| for n, subset in enumerate(smaller): | | for n, subset in enumerate(smaller): |
| yield smaller[:n] + [[ first ] + subset] + smaller[n+1:] | | yield smaller[:n] + [[ first ] + subset] + smaller[n+1:] |
| yield [[first]] + smaller | | yield [[first]] + smaller |
| | | |
| | | |
| def organize(cars, students): | | def organize(cars, students): |
| """Organize the students in cars.""" | | """Organize the students in cars.""" |
| if len(students) == 0: | | if len(students) == 0: |
| return True | | return True |
| | | |
| if len(cars) * 4 < len(students): | | if len(cars) * 4 < len(students): |
| return False | | return False |
| | | |
| students_configurations_generator = set_partitions_helper(students) | | students_configurations_generator = set_partitions_helper(students) |
| | | |
| cars_configurations = [j for i in range(1, len(cars) + 1) for j in list(combinations(cars, i))] | | cars_configurations = [j for i in range(1, len(cars) + 1) for j in list(combinations(cars, i))] |
| | | |
| try: | | try: |
| while True: | | while True: |
| students_configuration = next(students_configurations_generator) | | students_configuration = next(students_configurations_generator) |
| if not all(len(i) <= 4 for i in students_configuration): | | if not all(len(i) <= 4 for i in students_configuration): |
| continue | | continue |
| for cars_conf in cars_configurations: | | for cars_conf in cars_configurations: |
| if len(cars_conf) != len(students_configuration): | | if len(cars_conf) != len(students_configuration): |
| continue | | continue |
| | | |
| for cars_perm in permutations(cars_conf): | | for cars_perm in permutations(cars_conf): |
| cars_copy = deepcopy(cars_perm) | | cars_copy = deepcopy(cars_perm) |
| students_copy = deepcopy(students_configuration) | | students_copy = deepcopy(students_configuration) |
| for i in range(len(students_configuration)): | | for i in range(len(students_configuration)): |
| for student in students_copy[i]: | | for student in students_copy[i]: |
| cars_copy[i].add_student(student) | | cars_copy[i].add_student(student) |
| | | |
| if is_everyone_comfy([item for sublist in students_copy for item in sublist]): | | if is_everyone_comfy([item for sublist in students_copy for item in sublist]): |
| for i in range(len(students_configuration)): | | for i in range(len(students_configuration)): |
| for student in students_configuration[i]: | | for student in students_configuration[i]: |
| cars_perm[i].add_student(student) | | cars_perm[i].add_student(student) |
| return True | | return True |
| except StopIteration as s: | | except StopIteration as s: |
| return False | | return False |
| | | |