Every practice problem needs a good set of tests. Write the validator that the project's own checks perform.
What to build
A program that finds the problems in a set of tests.
Requirements
- Each test line is
KIND|INPUT|OUTPUTwhere KIND isS(sample) orH(hidden). INPUT and OUTPUT are text without|. - If there are fewer than 2
Stests printproblem: needs at least 2 sample tests. If there are fewer than 3Htests printproblem: needs at least 3 hidden tests. - Then, in test order (numbering from 0): for a test whose OUTPUT is empty print
problem: test I has no expected output; for a test whose INPUT equals the input of an earlier test printproblem: test I repeats the input of test J, where J is the first test with that input. A test with both problems prints the empty-output line first. - Finally print
COUNT problem(s).
Input
The first line has n (1 to 30). Each of the next n lines is a test.
Output
The problem lines, then the count line.
Constraints
1 ≤ n ≤ 30.
Example
Input:
5
S|2 3|5
S|10 -4|6
H|0 0|0
H|7 8|15
H|1 1|2
Output:
0 problem(s)
Why it works
The set-level rules need only the counts, and the test-level rules need one pass with a dictionary that remembers where each input was first seen, so each rule is a small, separate check.
Examples
5 S|2 3|5 S|10 -4|6 H|0 0|0 H|7 8|15 H|1 1|2
0 problem(s)
4 S|2 3|5 H|0 0|0 H|7 8| H|1 1|2
problem: needs at least 2 sample tests problem: test 2 has no expected output 2 problem(s)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Split each line into three parts with split("|", 2): kind, input and output.
Hint 2
Remember the first position of every input in a dictionary to find repeats.
Hint 3
Print the problems in the order given, then the count.
Solution
Show one correct solution (try it yourself first)
n = int(input())
tests = []
for _ in range(n):
kind, given, wanted = input().split("|", 2)
tests.append((kind, given, wanted))
problems = []
if sum(1 for t in tests if t[0] == "S") < 2:
problems.append("needs at least 2 sample tests")
if sum(1 for t in tests if t[0] == "H") < 3:
problems.append("needs at least 3 hidden tests")
first = {}
for i, (kind, given, wanted) in enumerate(tests):
if not wanted:
problems.append(f"test {i} has no expected output")
if given in first:
problems.append(f"test {i} repeats the input of test {first[given]}")
else:
first[given] = i
for p in problems:
print("problem:", p)
print(f"{len(problems)} problem(s)")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step