Advertisement
ssoni

plates.py

Jan 5th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  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.  
  11. #“… maximum of 6 characters (letters or numbers) and a minimum of 2 characters.”
  12. if len(s) < 2 or len(s) > 6:
  13. return False
  14.  
  15. #“All vanity plates must start with at least two letters.”
  16. if not s[0].isalpha() or not s[1].isalpha():
  17. return False
  18.  
  19. #check if there is a digit
  20. pos = 0
  21. fdp = -1
  22. for c in s:
  23. if c.isdigit():
  24. fdp = pos
  25. break
  26. else:
  27. pos = pos + 1
  28.  
  29. if fdp != -1:
  30.  
  31. #The first number used cannot be a ‘0’.”
  32. if s[fdp] == '0':
  33. return False
  34.  
  35. pos = 0
  36. for c in s:
  37. if pos>fdp and s[pos].isalpha():
  38. return False
  39. pos = pos + 1
  40.  
  41. if not s.isalnum():
  42. return False
  43.  
  44. return True
  45.  
  46. main()
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement