Pipeline Stages Ready to Run
Problem Statement
Given a CI pipeline's stage dependencies and the set of completed stages, list which stages are ready to run next.
Background
A stage is ready if it has not completed yet and all of its dependencies are in the completed set. Return the ready stages sorted alphabetically.
Your Task
def ready_stages(deps, completed):
- deps: dict mapping stage name -> list of prerequisite stage names.
- completed: list/set of completed stage names.
- Return the sorted list of ready stage names.
Input Format
- deps (dict), completed (list of str).
Output Format
- A sorted list of strings.
Sample
print(ready_stages({"build": [], "test": ["build"], "deploy": ["test"]}, ["build"]))
Output:
['test']
Example
print(ready_stages({"build": [], "test": ["build"], "deploy": ["test"]}, ["build"]))['test']
- Convert the completed stages list
["build"]into a set to allow for efficient membership checks during dependency validation. - Evaluate the "build" stage: it is already in the completed set, so it is excluded from the ready list.
- Evaluate the "test" stage: it is not completed, and its only dependency "build" is present in the completed set, satisfying the condition that all prerequisites are met.
- Evaluate the "deploy" stage: it is not completed, but its dependency "test" is not in the completed set, so it is not ready to run.
- The final output is ['test']
Constraints
- Ready = not completed AND all deps completed.
- Return names sorted ascending.