1def organize(cars, students):
2 if len(students) > len(cars) * 4:
3 return False
4
5 # represents the cars each student can be in initially
6 students_cars = determine_cars(cars, students)
7 # represents the friends of each student when both are placed in a comfortable car
8 students_friends = determine_friends(cars, students_cars, students)
9
10 passengers = {index: set() for index in range(len(cars))}
11 students_idx = [index for index in range(len(students))]
12
13 # backtracking
14 def find_accommodation(passengers, students_idx):
15 if not students_idx:
16 return True
17
18 current_student = students_idx[0]
19
20 for car, car_passengers in passengers.items():
21 try:
22 if all([comfy_in_car(students_cars, current_student, car) and
23 are_friends(students_friends, current_student, passenger)
24 for passenger in car_passengers]):
25 cars[car].add_student(students[current_student])
26 passengers[car].add(current_student)
27
28 if find_accommodation(passengers, students_idx[1:]):
29 return True
30
31 cars[car].remove_student(students[current_student])
32 passengers[car].remove(current_student)
33 except EnvironmentError:
34 continue
35
36 return False
37
38 return find_accommodation(passengers, students_idx)
39
40
41def are_friends(student_friends, student1_idx, student2_idx):
42 return student2_idx in student_friends.get(student1_idx)
43
44
45def comfy_in_car(student_cars, student_idx, car_idx):
46 return car_idx in student_cars.get(student_idx)
47
48
49def determine_cars(cars, students):
50 comfy_cars = {index: set() for index in range(len(students))}
51
52 for student_idx in range(len(students)):
53 for car_idx in range(len(cars)):
54 try:
55 cars[car_idx].add_student(students[student_idx])
56 if students[student_idx].is_comfy():
57 comfy_cars[student_idx].add(car_idx)
58 cars[car_idx].remove_student(students[student_idx])
59 except EnvironmentError:
60 continue
61 return comfy_cars
62
63
64def determine_friends(cars, comfy_cars, students):
65 friends = {index: set() for index in range(len(students))}
66
67 for student, st_cars in comfy_cars.items():
68 for other_student, other_cars in comfy_cars.items():
69 if student == other_student:
70 continue
71 common_cars = set.intersection(st_cars, other_cars)
72 if len(common_cars) == 0:
73 # they can never be together
74 continue
75 some_car = cars[common_cars.pop()]
76 try:
77 some_car.add_student(students[student])
78 some_car.add_student(students[other_student])
79
80 if students[student].is_comfy() and students[other_student].is_comfy():
81 friends[student].add(other_student)
82 friends[other_student].add(student)
83
84 some_car.remove_student(students[student])
85 some_car.remove_student(students[other_student])
86 except EnvironmentError:
87 continue
88 return friends
...F.
======================================================================
FAIL: test_regular_case (test.TesFull)
Test a regular case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 48, in test_regular_case
anti_rusalov_true(self, all([x.is_comfy() is True for x in students]))
AssertionError: False is not true
----------------------------------------------------------------------
Ran 5 tests in 0.001s
FAILED (failures=1)
f | 1 | def organize(cars, students): | f | 1 | def organize(cars, students): |
2 | if len(students) > len(cars) * 4: | 2 | if len(students) > len(cars) * 4: | ||
3 | return False | 3 | return False | ||
4 | 4 | ||||
n | 5 | passengers = [[]] * len(cars) | n | 5 | # represents the cars each student can be in initially |
6 | for student in students: | 6 | students_cars = determine_cars(cars, students) | ||
7 | if not accommodate(passengers, cars, student): | 7 | # represents the friends of each student when both are placed in a comfortable car | ||
8 | return False | 8 | students_friends = determine_friends(cars, students_cars, students) | ||
9 | return True | ||||
10 | 9 | ||||
n | 11 | def accommodate(passengers, cars, student): | n | 10 | passengers = {index: set() for index in range(len(cars))} |
12 | for car in cars: | 11 | students_idx = [index for index in range(len(students))] | ||
13 | try: | ||||
14 | car.add_student(student) | ||||
15 | if student.is_comfy() and is_welcome(passengers, cars, car): | ||||
16 | passengers[cars.index(car)].append(student) | ||||
17 | return True | ||||
18 | else: | ||||
19 | car.remove_student(student) | ||||
20 | except EnvironmentError: | ||||
21 | continue | ||||
22 | return False | ||||
23 | 12 | ||||
t | 24 | def is_welcome(passengers, cars, car): | t | 13 | # backtracking |
25 | for passenger in passengers[cars.index(car)]: | 14 | def find_accommodation(passengers, students_idx): | ||
26 | if not passenger.is_comfy(): | 15 | if not students_idx: | ||
16 | return True | ||||
17 | |||||
18 | current_student = students_idx[0] | ||||
19 | |||||
20 | for car, car_passengers in passengers.items(): | ||||
21 | try: | ||||
22 | if all([comfy_in_car(students_cars, current_student, car) and | ||||
23 | are_friends(students_friends, current_student, passenger) | ||||
24 | for passenger in car_passengers]): | ||||
25 | cars[car].add_student(students[current_student]) | ||||
26 | passengers[car].add(current_student) | ||||
27 | |||||
28 | if find_accommodation(passengers, students_idx[1:]): | ||||
29 | return True | ||||
30 | |||||
31 | cars[car].remove_student(students[current_student]) | ||||
32 | passengers[car].remove(current_student) | ||||
33 | except EnvironmentError: | ||||
34 | continue | ||||
35 | |||||
27 | return False | 36 | return False | ||
37 | |||||
38 | return find_accommodation(passengers, students_idx) | ||||
39 | |||||
40 | |||||
41 | def are_friends(student_friends, student1_idx, student2_idx): | ||||
42 | return student2_idx in student_friends.get(student1_idx) | ||||
43 | |||||
44 | |||||
45 | def comfy_in_car(student_cars, student_idx, car_idx): | ||||
46 | return car_idx in student_cars.get(student_idx) | ||||
47 | |||||
48 | |||||
49 | def determine_cars(cars, students): | ||||
50 | comfy_cars = {index: set() for index in range(len(students))} | ||||
51 | |||||
52 | for student_idx in range(len(students)): | ||||
53 | for car_idx in range(len(cars)): | ||||
54 | try: | ||||
55 | cars[car_idx].add_student(students[student_idx]) | ||||
56 | if students[student_idx].is_comfy(): | ||||
57 | comfy_cars[student_idx].add(car_idx) | ||||
58 | cars[car_idx].remove_student(students[student_idx]) | ||||
59 | except EnvironmentError: | ||||
60 | continue | ||||
61 | return comfy_cars | ||||
62 | |||||
63 | |||||
64 | def determine_friends(cars, comfy_cars, students): | ||||
65 | friends = {index: set() for index in range(len(students))} | ||||
66 | |||||
67 | for student, st_cars in comfy_cars.items(): | ||||
68 | for other_student, other_cars in comfy_cars.items(): | ||||
69 | if student == other_student: | ||||
70 | continue | ||||
71 | common_cars = set.intersection(st_cars, other_cars) | ||||
72 | if len(common_cars) == 0: | ||||
73 | # they can never be together | ||||
74 | continue | ||||
75 | some_car = cars[common_cars.pop()] | ||||
76 | try: | ||||
77 | some_car.add_student(students[student]) | ||||
78 | some_car.add_student(students[other_student]) | ||||
79 | |||||
80 | if students[student].is_comfy() and students[other_student].is_comfy(): | ||||
81 | friends[student].add(other_student) | ||||
82 | friends[other_student].add(student) | ||||
83 | |||||
84 | some_car.remove_student(students[student]) | ||||
85 | some_car.remove_student(students[other_student]) | ||||
86 | except EnvironmentError: | ||||
87 | continue | ||||
28 | return True | 88 | return friends |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|