We have a job definition as follows:
from __future__ import annotations
from dataclasses import dataclass
from typing import List
@dataclass
class Job:
name: str
cmd: str
deps: List[Job]
We want to implement a method that takes a set of job definitions and prints all of the commands:
def print_jobs(jobs: List[Job]):
pass
Examples for the candidate:
# No commands / null
print_jobs([])
Result:
# Single command
a = Job(name="a", cmd="1", deps=[])
print_jobs([a])
Result:
Command is: 1
# Single command w/dependencies
a = Job(name="a", cmd="1", deps=[])
b = Job(name="b", cmd="2", deps=[a])
print_jobs([b])
Result:
Command is: 1
Command is: 2
# Two commands w/dependencies and not all jobs are in original list
a = Job(name="a", cmd="1", deps=[])
c = Job(name="c", cmd="3", deps=[])
b = Job(name="b", cmd="2", deps=[a, c])
print_jobs([a, b])
Result:
Command is: 1
Command is: 1
Command is: 3
Command is: 2
This problem is a dependency-order traversal task. Each Job may depend on other jobs, and the required output is to print all prerequisite commands before printing the current job's command. A standard approach is depth-first search with a visited set to avoid printing the same job more than once when dependencies are shared. The examples show that the function may need to print jobs that are not explicitly present in the original input list if they are reachable through dependencies.