forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_092.py
67 lines (53 loc) · 1.49 KB
/
problem_092.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def get_course_order_helper(prereqs, indep, order):
if not indep:
return None, None, None
elif not prereqs:
return prereqs, indep, order
new_indep = set()
for dc in prereqs:
required = prereqs[dc] - indep
if not len(required):
new_indep.add(dc)
order.append(dc)
for course in new_indep:
del prereqs[course]
return get_course_order_helper(prereqs, indep.union(new_indep), order)
def get_course_order(prereqs):
indep = set()
order = list()
for course in prereqs:
if not prereqs[course]:
indep.add(course)
order.append(course)
else:
prereqs[course] = set(prereqs[course])
for course in indep:
del prereqs[course]
_, _, order = get_course_order_helper(prereqs, indep, order)
return order
prereqs = {
'CSC100': [],
'CSC200': [],
'CSC300': []
}
assert get_course_order(prereqs) == ['CSC100', 'CSC200', 'CSC300']
prereqs = {
'CSC300': ['CSC100', 'CSC200'],
'CSC200': ['CSC100'],
'CSC100': []
}
assert get_course_order(prereqs) == ['CSC100', 'CSC200', 'CSC300']
prereqs = {
'CSC400': ['CSC200'],
'CSC300': ['CSC100', 'CSC200'],
'CSC200': ['CSC100'],
'CSC100': []
}
assert get_course_order(prereqs) == ['CSC100', 'CSC200', 'CSC400', 'CSC300']
prereqs = {
'CSC400': ['CSC300'],
'CSC300': ['CSC100', 'CSC200'],
'CSC200': ['CSC100'],
'CSC100': ['CSC400']
}
assert not get_course_order(prereqs)