A test runner ends with a summary. Write the summary of a run.
What to build
A program that counts passing and failing tests and lists the failures.
Requirements
- Each line is
NAME RESULTwhere RESULT isPASSorFAIL. - Print
P passed, F failed. - If F is more than 0, also print
failed: NAMESwith the names of the failed tests in input order, separated by a comma and a space.
Input
The first line has n (1 to 100). Each of the next n lines is NAME RESULT, without spaces in the name.
Output
The summary line, and the failed line when there are failures.
Constraints
1 ≤ n ≤ 100.
Example
Input:
3
test_add PASS
test_empty FAIL
test_big PASS
Output:
2 passed, 1 failed
failed: test_empty
Why it works
The summary only needs two counters and the list of failed names, all of which one pass over the lines can build.
Examples
3 test_add PASS test_empty FAIL test_big PASS
2 passed, 1 failed failed: test_empty
2 test_a PASS test_b PASS
2 passed, 0 failed
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Split each line into the name and the result: name, result = line.split().
Hint 2
Keep a list of the names whose result is FAIL.
Hint 3
Print the failed line only when there is at least one failure.
Solution
Show one correct solution (try it yourself first)
n = int(input())
passed = 0
failed = []
for _ in range(n):
name, result = input().split()
if result == "PASS":
passed += 1
else:
failed.append(name)
print(f"{passed} passed, {len(failed)} failed")
if failed:
print("failed: " + ", ".join(failed))
▶ Open this solution in the compiler, then press Visualize to watch it run step by step