Skip to content

Graphs · stretch

An order to take courses

needs maps each course to the courses it requires first. Write course_order(needs) returning a list of every course such that each comes after everything it needs — or None if that is impossible because the requirements loop.

When several courses are ready at once, take them in alphabetical order, so there is one right answer.

Run adds print(course_order({"algebra": [], "calculus": ["algebra"], "physics": ["calculus"]})) after your code, to try it.

Stuck on the idea rather than the code? The Groups and orderings lesson walks through it.