, , . , , , , , , .
, students tests.
students, tests = zip(*map(lambda each: each.split(':'), data.split()))
data - , , . , .
unique_tests = set(tests)
test_map = {test : set() for test in unique_tests}
for student, test in zip(students, tests):
test_map[test].add(student)
test_map :
{'T1': {'S1', 'S2', 'S3'},
'T2': {'S1', 'S3', 'S5'},
'T3': {'S2', 'S4', 'S5'},
'T4': {'S2'}}
.
import itertools
test_combos = list(itertools.chain.from_iterable(
(itertools.combinations(unique_tests, i) for i in
range(2, len(unique_tests) + 1))))
test_combos:
[('T4', 'T3'),
('T4', 'T1'),
('T4', 'T2'),
('T3', 'T1'),
('T3', 'T2'),
('T1', 'T2'),
('T4', 'T3', 'T1'),
('T4', 'T3', 'T2'),
('T4', 'T1', 'T2'),
('T3', 'T1', 'T2'),
('T4', 'T3', 'T1', 'T2')]
, -1, 4 4 . , , .. , . ( , "" .)
, , True, . , , , , .
def valid_test_combo(tests):
pairs = itertools.combinations(tests, 2)
return all(map(lambda pair:
test_map[pair[0]].isdisjoint(test_map[pair[1]]), pairs))
, :
valid_combos = set(filter(valid_test_combo, test_combos))
{('T4', 'T2')}
, , 'T2' 'T4'.
, , , :
combined_tests = set(itertools.chain.from_iterable(valid_combos))
remaining_tests = unique_tests - combined_tests
days = list(valid_combos) + list(remaining_tests)
days , :
[('T4', 'T2'), 'T3', 'T1']
:
, , set-cover problem, (NP-hard). , , , , .