-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path92_Dropbox_Sort_Courses_with_Prereqs.py
executable file
·81 lines (63 loc) · 2.52 KB
/
92_Dropbox_Sort_Courses_with_Prereqs.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
This problem was asked by Airbnb.
We're given a hashmap associating each courseId key with a list of courseIds values,
which represents that the prerequisites of courseId are courseIds.
Return a sorted ordering of courses such that we can finish all courses.
Return null if there is no such ordering.
For example, given {'CSC300': ['CSC100', 'CSC200'], 'CSC200': ['CSC100'], 'CSC100': []},
should return ['CSC100', 'CSC200', 'CSCS300'].
"""
def sort_courses(course_dict):
visit_dict = {}
course_order = []
def helper(course, prereqs, curr_order=[]):
if course in visit_dict and visit_dict[course] is 'visited':
# course visited so prereq completed
return curr_order
if course in visit_dict and visit_dict[course] is 'visiting':
# ERROR must be a loop, return none
return None
visit_dict[course] = 'visiting' # set course we are look at to visiting
if len(prereqs) == 0:
# if no prereqs required for this course then set to 'visited' and return the course
visit_dict[course] = 'visited'
return curr_order + [course]
# loop over all the prereq courses to find their prereqs
for c in prereqs:
curr_order = helper(c, course_dict[c], curr_order)
if curr_order is None:
# if curr_order is None, that means a loop was found, return None
return None
visit_dict[course] = 'visited'
return curr_order + [course]
for course, prereqs in course_dict.items():
course_order = helper(course, prereqs, course_order)
if course_order is None:
return None
return course_order
if __name__ == '__main__':
# ['CSC100', 'CSC200', 'CSC300']
print(sort_courses({'CSC300': ['CSC100', 'CSC200'], 'CSC200': ['CSC100'], 'CSC100': []}))
# ['CSC100', 'CSC200', 'CSC300', 'CSC700', 'CSC400', 'CSC800', 'CSC500']
print(sort_courses({
'CSC500': ['CSC300', 'CSC400', 'CSC800'],
'CSC800': ['CSC700'],
'CSC400': ['CSC700'],
'CSC300': ['CSC100', 'CSC200'],
'CSC700': [],
'CSC200': [],
'CSC100': []}))
# ['CSC100', 'CSC200', 'CSC400', 'CSC300']
print(sort_courses({
'CSC400': ['CSC200'],
'CSC300': ['CSC100', 'CSC200'],
'CSC200': ['CSC100'],
'CSC100': []
}))
# None
print(sort_courses({
'CSC400': ['CSC300'],
'CSC300': ['CSC100', 'CSC200'],
'CSC200': ['CSC100'],
'CSC100': ['CSC400']
}))