Skip to content

Commit b6d5338

Browse files
committed
test_plates
1 parent 017ba74 commit b6d5338

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Problem Set 5/test_plates/plates.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def main():
2+
plate = input("Plate: ")
3+
if is_valid(plate):
4+
print("Valid")
5+
else:
6+
print("Invalid")
7+
8+
9+
def is_valid(s):
10+
# Check if the length is between 2 and 6
11+
if len(s) < 2 or len(s) > 6:
12+
return False
13+
14+
# Check if the first two characters are alphabetic
15+
if not (s[0].isalpha() and s[1].isalpha()):
16+
return False
17+
18+
# Check for invalid characters
19+
for c in s:
20+
if c in [' ', '?', '.', ',', '!']:
21+
return False
22+
23+
# Find the index where numbers start, if any
24+
i = 0
25+
while i < len(s) and s[i].isalpha():
26+
i += 1
27+
28+
# Ensure all characters after the first non-alpha are digits
29+
if i < len(s):
30+
for j in range(i, len(s)):
31+
if not s[j].isdigit():
32+
return False
33+
# Ensure there's no leading zero in the numeric part
34+
if s[j] == '0' and j == i:
35+
return False
36+
37+
return True
38+
39+
if __name__ == "__main__":
40+
main()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from plates import is_valid
2+
3+
def main():
4+
test_invalid()
5+
test_valid()
6+
7+
def test_invalid():
8+
# Too short
9+
assert is_valid("C") == False
10+
# Too long
11+
assert is_valid("OUTATIME") == False
12+
# Starts with numbers
13+
assert is_valid("12CS") == False
14+
# Contains special characters
15+
assert is_valid("PI3.14") == False
16+
# Numbers in the middle
17+
assert is_valid("CS50P") == False
18+
# Leading zero
19+
assert is_valid("CS05") == False
20+
# Lowercase and leading zero
21+
assert is_valid("50") == False
22+
23+
def test_valid():
24+
# Valid plate
25+
assert is_valid("CS50") == True
26+
# Valid plate with all letters
27+
assert is_valid("HELLO") == True
28+
# Valid plate with numbers at the end
29+
assert is_valid("AAA222") == True
30+
31+
if __name__ == "__main__":
32+
main()

0 commit comments

Comments
 (0)