Overview
Order the steps of a build so every step comes after the ones it needs, by counting unmet prerequisites, and report a cycle instead of looping forever.
- count unmet prerequisites
- take a task at zero, unlock the next
- tasks left but none ready: a cycle
Short Course
To order tasks with prerequisites, count each task's unmet prerequisites. Repeatedly take a task at zero and lower the count of the tasks it unlocks. Trap: if tasks remain but none is at zero, there's a cycle; stop instead of looping forever.
needs = {"deploy": 2, "test": 1, "build": 0}
ready = [t for t, n in needs.items() if n == 0]
ready # ['build']: nothing blocks it
Task
Write build_order(tasks, rules): rules holds (before, after) pairs.
Return all tasks in an order that respects every rule, taking the
alphabetically first ready task each time, or None if there's a cycle.
Example: build_order(['test', 'build', 'lint'], [('build', 'test')]) →
['build', 'lint', 'test'].
Test Suites
Pending
Next
Output