Preparing git repo for final project
This commit is contained in:
39
problems/pset5/test_plates/plates.py
Normal file
39
problems/pset5/test_plates/plates.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import re
|
||||
|
||||
|
||||
def main():
|
||||
plate = input("Plate: ")
|
||||
if is_valid(plate):
|
||||
print("Valid")
|
||||
else:
|
||||
print("Invalid")
|
||||
|
||||
|
||||
def is_valid(s):
|
||||
if len(s) < 2 or len(s) > 6:
|
||||
return False
|
||||
return re.match(r"^[A-Z]{2,}([1-9]\d+)?(?![A-Z])$", s) is not None
|
||||
|
||||
|
||||
def loopy_is_valid(s):
|
||||
length = 0
|
||||
has_digits = False
|
||||
for n, c in enumerate(s):
|
||||
if n > 6:
|
||||
return False
|
||||
if not c.isalnum():
|
||||
return False
|
||||
if n < 2 and not c.isalpha():
|
||||
return False
|
||||
if c.isdigit():
|
||||
if not has_digits and c == '0':
|
||||
return False
|
||||
has_digits = True
|
||||
elif has_digits:
|
||||
return False
|
||||
length += 1
|
||||
return length >= 2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
39
problems/pset5/test_plates/test_plates.py
Normal file
39
problems/pset5/test_plates/test_plates.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from plates import is_valid
|
||||
|
||||
|
||||
def main():
|
||||
test_incipit()
|
||||
test_length()
|
||||
test_digits()
|
||||
test_forbidden_characters()
|
||||
|
||||
|
||||
def test_incipit():
|
||||
assert is_valid('11AA11') == False
|
||||
assert is_valid('11AA') == False
|
||||
assert is_valid('1AA2') == False
|
||||
assert is_valid('2AAP') == False
|
||||
assert is_valid('A1111') == False
|
||||
assert is_valid('AA1111') == True
|
||||
|
||||
|
||||
def test_length():
|
||||
assert is_valid('') == False
|
||||
assert is_valid('A') == False
|
||||
assert is_valid('AAAAAA111111') == False
|
||||
|
||||
|
||||
def test_digits():
|
||||
assert is_valid('AA111A') == False
|
||||
assert is_valid('AA0111') == False
|
||||
|
||||
|
||||
def test_forbidden_characters():
|
||||
assert is_valid('AA1.1') == False
|
||||
assert is_valid('AA1,1') == False
|
||||
assert is_valid('AA1 1') == False
|
||||
assert is_valid('AA11!') == False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user