A CI pipeline runs steps in order and stops at the first failure, unless a step is marked optional. Simulate one.
What to build
A program that prints the log of a CI pipeline.
Requirements
- Each step line is
NAME RESULTorNAME RESULT optional; RESULT ispassorfail. - Go through the steps in order. A step before any failure is printed
NAME: passedwhen it passes. If a step fails and it is not optional, printNAME: FAILED; every later step is then printedNAME: skipped. - A failing step that is optional prints
NAME: failed (allowed)and the pipeline goes on. - Finally print
result: greenif no required step failed, otherwiseresult: red.
Input
The first line has n (1 to 20). Each of the next n lines is a step.
Output
One line per step, then the result.
Constraints
1 ≤ n ≤ 20.
Example
Input:
3
lint pass
tests fail
build pass
Output:
lint: passed
tests: FAILED
build: skipped
result: red
Why it works
One variable that remembers whether a required step has failed decides everything: before it is set the steps run normally, after it is set they are skipped, and the final result is read from the same variable.
Examples
3 lint pass tests fail build pass
lint: passed tests: FAILED build: skipped result: red
2 lint pass build pass
lint: passed build: passed result: green
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Keep a variable that remembers the first required failure.
Hint 2
After that failure, print skipped for every step.
Hint 3
A failing optional step is allowed: print its own message and go on.
Solution
Show one correct solution (try it yourself first)
n = int(input())
broken = False
for _ in range(n):
parts = input().split()
name, result = parts[0], parts[1]
optional = len(parts) > 2 and parts[2] == "optional"
if broken:
print(f"{name}: skipped")
elif result == "pass":
print(f"{name}: passed")
elif optional:
print(f"{name}: failed (allowed)")
else:
print(f"{name}: FAILED")
broken = True
print("result: red" if broken else "result: green")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step