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

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

0 точки общо

0 успешни теста
0 неуспешни теста
Код (early exit modification)

 1from itertools import permutations, combinations_with_replacement
 2
 3def empty_box(box):
 4    for car, student in box:
 5        car.remove_student(student)
 6
 7def organize(cars, students):
 8    #generate a sequence of students positions to be tested
 9    for config in list(permutations(students)):
10        # for this sequence generate a distribution of the number of students in each car
11        # x1 +x2+....+xk=n assuming len(cars)=k,len(students)=n
12        for combo in combinations_with_replacement(range(len(cars)), len(students)):
13            box = []
14            std_pos = 0
15            works = True
16            # go through the generated car positions and distribute students
17            for car_pos in combo:
18                try:
19                    cars[car_pos].add_student(students[config[std_pos]])
20                except EnvironmentError:
21                    # if reached here the combo is not a solution
22                    empty_box(box)
23                    works = False
24                    break
25                else:
26                    # if student is not OK then just stop the exploration
27                    if not students[config[std_pos]].is_comfy():
28                        empty_box(box)
29                        works = False
30                        break
31                    box.append((cars[car_pos], students[config[std_pos]]))
32                    std_pos += 1
33            if works:
34                return True
35    return False

Timed out.

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

f1from itertools import permutations, combinations_with_replacementf1from itertools import permutations, combinations_with_replacement
22
3def empty_box(box):3def empty_box(box):
4    for car, student in box:4    for car, student in box:
5        car.remove_student(student)5        car.remove_student(student)
n6 n
7def check_satisfiablity(students):
8    for student in students:
9        if not student.is_comfy():
10            return False
11    return True
126
13def organize(cars, students):7def organize(cars, students):
14    #generate a sequence of students positions to be tested8    #generate a sequence of students positions to be tested
15    for config in list(permutations(students)):9    for config in list(permutations(students)):
16        # for this sequence generate a distribution of the number of students in each car10        # for this sequence generate a distribution of the number of students in each car
17        # x1 +x2+....+xk=n assuming len(cars)=k,len(students)=n11        # x1 +x2+....+xk=n assuming len(cars)=k,len(students)=n
18        for combo in combinations_with_replacement(range(len(cars)), len(students)):12        for combo in combinations_with_replacement(range(len(cars)), len(students)):
19            box = []13            box = []
20            std_pos = 014            std_pos = 0
nn15            works = True
21            # go through the generated car positions and distribute students16            # go through the generated car positions and distribute students
22            for car_pos in combo:17            for car_pos in combo:
23                try:18                try:
24                    cars[car_pos].add_student(students[config[std_pos]])19                    cars[car_pos].add_student(students[config[std_pos]])
25                except EnvironmentError:20                except EnvironmentError:
26                    # if reached here the combo is not a solution21                    # if reached here the combo is not a solution
27                    empty_box(box)22                    empty_box(box)
nn23                    works = False
28                    break24                    break
29                else:25                else:
nn26                    # if student is not OK then just stop the exploration
27                    if not students[config[std_pos]].is_comfy():
28                        empty_box(box)
29                        works = False
30                        break
30                    box.append((cars[car_pos], students[config[std_pos]]))31                    box.append((cars[car_pos], students[config[std_pos]]))
31                    std_pos += 132                    std_pos += 1
t32            # when done check the combot33            if works:
33            if check_satisfiablity(students):
34                return True34                return True
35    return False35    return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op