A practice problem folder must hold exactly five files. Check a folder before you commit it.
What to build
A program that compares the files of a practice problem folder with the required five.
Requirements
- The required files, in this order, are
meta.json,index.mdx,starter.py,solution.pyandtests.json. - Print
missing: NAMEfor each required file that is not in the folder, in the order above. - Then print
unexpected: NAMEfor each file in the folder that is not required, in input order. - Finally print
folder okif there is nothing missing and nothing unexpected, otherwiseCOUNT problem(s)with the number of missing and unexpected files together.
Input
The first line has n (0 to 20). Each of the next n lines is a file name.
Output
The problem lines, then the final line.
Constraints
0 ≤ n ≤ 20.
Example
Input:
5
meta.json
index.mdx
starter.py
solution.py
tests.json
Output:
folder ok
Why it works
Two membership tests, one in each direction, find both kinds of problem: a required name that is absent, and a present name that is not required.
Examples
5 meta.json index.mdx starter.py solution.py tests.json
folder ok
4 meta.json index.mdx starter.py tests.json
missing: solution.py 1 problem(s)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Make a list of the five required names in the given order.
Hint 2
A name is unexpected if it is not in the required list.
Hint 3
Print the missing ones first, then the unexpected ones, then the summary.
Solution
Show one correct solution (try it yourself first)
REQUIRED = ["meta.json", "index.mdx", "starter.py", "solution.py", "tests.json"]
n = int(input())
names = [input().strip() for _ in range(n)]
problems = 0
for name in REQUIRED:
if name not in names:
print("missing:", name)
problems += 1
for name in names:
if name not in REQUIRED:
print("unexpected:", name)
problems += 1
print("folder ok" if problems == 0 else f"{problems} problem(s)")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step