A MAC address is six pairs of hex digits separated by colons. Decide whether each line is one.
What to build
A program that classifies each text as valid, invalid or broadcast.
Requirements
- An address is valid when it has six parts separated by
:and each part is exactly two hex digits (upper or lower case). - The valid address
ff:ff:ff:ff:ff:ff(in any case) isbroadcast. - Print each address, a space, and its class.
Input
A line with n, then n lines, each one text to check (no spaces inside).
Output
n lines like 3c:22:fb:91:0a:7e valid.
Constraints
1 ≤ n ≤ 20.
Example
Input:
3
3c:22:fb:91:0a:7e
ff:ff:ff:ff:ff:ff
3c:22:fb:91:0a
Output:
3c:22:fb:91:0a:7e valid
ff:ff:ff:ff:ff:ff broadcast
3c:22:fb:91:0a invalid
Why it works
The format has a fixed shape (six parts of two hex digits), so checking the parts one by one decides it. Only a valid address can be the broadcast address.
Examples
3 3c:22:fb:91:0a:7e ff:ff:ff:ff:ff:ff 3c:22:fb:91:0a
3c:22:fb:91:0a:7e valid ff:ff:ff:ff:ff:ff broadcast 3c:22:fb:91:0a invalid
2 00:1A:2b:3C:4d:5E zz:22:fb:91:0a:7e
00:1A:2b:3C:4d:5E valid zz:22:fb:91:0a:7e invalid
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Split the text at ":" and check that there are exactly 6 parts.
Hint 2
Each part must have exactly 2 characters, all in 0123456789abcdefABCDEF.
Hint 3
Check the broadcast address only after you know the address is valid; compare it in lower case.
Solution
Show one correct solution (try it yourself first)
n = int(input())
digits = "0123456789abcdefABCDEF"
for _ in range(n):
a = input().strip()
parts = a.split(":")
ok = len(parts) == 6
if ok:
for p in parts:
if len(p) != 2 or p[0] not in digits or p[1] not in digits:
ok = False
if not ok:
print(a, "invalid")
elif a.lower() == "ff:ff:ff:ff:ff:ff":
print(a, "broadcast")
else:
print(a, "valid")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step