Projects often want branch names that say what the branch does. Check names against a simple rule.
What to build
A program that checks branch names.
Requirements
- A valid name is a prefix
feature/,fix/ordocs/followed by a rest made only of lower-case lettersatoz, digits and hyphens. - Check in this order and report the first problem: if the name does not start with one of the prefixes print
NAME: bad prefix; if the rest is empty printNAME: empty; if the rest has any other character printNAME: bad characters. - Otherwise print
NAME: ok.
Input
The first line has n (1 to 50). Each of the next n lines is a branch name (spaces at the ends are removed).
Output
n lines.
Constraints
1 ≤ n ≤ 50.
Example
Input:
3
fix/empty-average
feature/Dark-Mode
main
Output:
fix/empty-average: ok
feature/Dark-Mode: bad characters
main: bad prefix
Why it works
The checks depend on each other: only a name with a good prefix has a rest worth checking, so testing in the stated order gives each name exactly one verdict.
Examples
3 fix/empty-average feature/Dark-Mode main
fix/empty-average: ok feature/Dark-Mode: bad characters main: bad prefix
2 docs/ docs/install-steps
docs/: empty docs/install-steps: ok
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
startswith() takes a tuple of prefixes.
Hint 2
Cut the prefix off with name.partition("/")[2] and test the rest.
Hint 3
Check in this order: prefix, empty, characters.
Solution
Show one correct solution (try it yourself first)
n = int(input())
allowed = "abcdefghijklmnopqrstuvwxyz0123456789-"
for _ in range(n):
name = input().strip()
if not name.startswith(("feature/", "fix/", "docs/")):
verdict = "bad prefix"
else:
rest = name.partition("/")[2]
if not rest:
verdict = "empty"
elif any(ch not in allowed for ch in rest):
verdict = "bad characters"
else:
verdict = "ok"
print(f"{name}: {verdict}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step