Order turnaround tasks

Write codeEasy18 min

Your task

A ground crew works a fixed list of turnaround tasks on a parked aircraft, one task at a time, and the dispatcher wants to see every workable running order before the shift starts. task_names lists the tasks, and every name is distinct. must_precede holds rules written as "fuel|board", meaning fuel has to finish before board may start. A rule can be listed more than once, and it always names two different tasks that both appear in task_names. Return every order that runs each task exactly once and breaks no rule, each written as the task names joined by a comma and a space. Return the orders sorted in ascending order, so the answer does not depend on how the list was typed. Return an empty list when there are no tasks, and when the rules contradict each other so that no order works. There are at most 7 tasks.

Examples

list_turnaround_orders(task_names: list[str], must_precede: list[str]) -> list[str]
Example 1
Inputlist_turnaround_orders(["fuel", "clean", "board"], ["fuel|board"])
Output["clean, fuel, board", "fuel, board, clean", "fuel, clean, board"]
Example 2
Inputlist_turnaround_orders(["a", "b"], ["a|b", "b|a"])
Output[]
Example 3
Inputlist_turnaround_orders(["tow", "wash"], [])
Output["tow, wash", "wash, tow"]