1def organize(cars, students):
2 if len(cars) >= len(students):
3 return True
4 group1, group2 = split_cars_into_groups(cars, students[0])
5
6 students_group_one = []
7 students_group_other = []
8
9 #group the students by the two categories of cars
10 if (len(group1)>=1 and len(group2)>=1) or len(group1)>=1:
11 for student in students:
12 group1[0].add_student(student)
13 if student.is_comfy():
14 students_group_one.append(student)
15 else:
16 students_group_other.append(student)
17 group1[0].remove_student(student)
18
19 if len(students_group_one)>=2:
20 if not is_half_classes_possible(students_group_one, group1):
21 return False
22
23 if len(group2)>=1 and len(students_group_other)>=2:
24 if not is_half_classes_possible(students_group_other, group2):
25 return False
26 elif len(group2)>=1:
27 for student in students:
28 group2[0].add_student(student)
29 if student.is_comfy():
30 students_group_other.append(student)
31 else:
32 students_group_one.append(student)
33 group2[0].remove_student(student)
34
35 if len(students_group_other)>=2:
36 if not is_half_classes_possible(students_group_other, group2):
37 return False
38 else:
39 return False
40 return True
41
42def split_cars_into_groups(cars, student):
43 one_type_of_car = []
44 other_type_of_car = []
45
46 #group cars into warm and cold
47 for car in cars:
48 car.add_student(student)
49
50 if student.is_comfy():
51 one_type_of_car.append(car)
52 else:
53 other_type_of_car.append(car)
54 car.remove_student(student)
55
56 return one_type_of_car, other_type_of_car
57
58def is_half_classes_possible(students, group_of_cars):
59 students_equivalence_classes = [[] for key in range(4)]
60 i = 0
61
62 group_of_cars[0].add_student(students[0])
63 group_of_cars[0].add_student(students[1])
64
65 if students[0].is_comfy():
66 students_equivalence_classes[i].extend([students[0], students[1]])
67 i = 1
68 else:
69 students_equivalence_classes[i].append(students[0])
70 students_equivalence_classes[i+1].append(students[1])
71 i = 2
72
73 group_of_cars[0].remove_student(students[0])
74 group_of_cars[0].remove_student(students[1])
75
76 for student in students[2:]:
77 if i == 4:
78 break
79 group_of_cars[0].add_student(student)
80
81 for eq_class in students_equivalence_classes:
82 if i == 4:
83 break
84
85 group_of_cars[0].add_student(eq_class[0])
86
87 if student.is_comfy():
88 eq_class.append(student)
89 group_of_cars[0].remove_student(eq_class[0])
90 break
91 elif i < 4:
92 #add the student to the next empty equivalence class
93 students_equivalence_classes[i].append(student)
94 i += 1
95 group_of_cars[0].remove_student(eq_class[0])
96 break
97 group_of_cars[0].remove_student(eq_class[0])
98
99 group_of_cars[0].remove_student(student)
100 if len(group_of_cars) < i:
101 return False
102 return True
F..FF
======================================================================
FAIL: test_big_input (test.TesFull)
Test a big case to ensure no huge bruteforcing.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 91, in test_big_input
anti_rusalov_true(self, all([x.is_comfy() is True for x in students]))
AssertionError: False is not true
======================================================================
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
======================================================================
FAIL: test_single_solution_case (test.TesFull)
Test a single-solution case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 65, in test_single_solution_case
anti_rusalov_true(self, organize(cars, students))
AssertionError: False is not true
----------------------------------------------------------------------
Ran 5 tests in 0.001s
FAILED (failures=3)
![]()
Николай Николаев
07.12.2023 17:40Пробвах да реша задачата с класове на еквивалентност, но в последствие видях, че студентите може и да нямат предпочитания, така че вероятно не е вярно 😅.
|