Advertisement
nikolask

CS50P - PSET2_PLATES - Solved

Aug 12th, 2023 (edited)
1,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | Source Code | 0 0
  1. def main():
  2.     plate = input("Plate: ")
  3.     if is_valid(plate):
  4.         print("Valid")
  5.     else:
  6.         print("Invalid")
  7.  
  8. def is_valid(s):
  9.     return (
  10.         two_letters(s)
  11.         and text_length(s)
  12.         and punctuation_check(s)
  13.         and check_digits_after_digit(s)
  14.     )
  15.  
  16. # check if the first letters are alpha
  17. def two_letters(text):
  18.     return text[:2].isalpha()
  19.  
  20. # check the length of the string
  21. def text_length(text):
  22.     return 2 <= len(text) <= 6
  23.  
  24. # Check if the characters following a digit to the end of the string, are digits
  25. def check_digits_after_digit(text):
  26.     if text.isalpha():
  27.         return True
  28.     else:
  29.         found_digit = False
  30.         for c in text:
  31.             if c.isdigit():
  32.                 found_digit = True
  33.                 idx = text.index(c)
  34.                 break
  35.  
  36.         if found_digit:
  37.             if c == "0":
  38.                 return False
  39.             if text[idx:].isdigit():
  40.                 return True
  41.             else:
  42.                 return False
  43.  
  44.  
  45. # check if there is punctuation in text
  46. def punctuation_check(text):
  47.     return not any(c in "'.,;:?! " for c in text)
  48.  
  49. if __name__=="__main__":
  50.     main()
  51.  
  52.  
  53. # test = ["CS50", "ECTO88", "NRVOUS",  "CS05", "CS50P2", "PI3.14", "H", "OUTATIME", "CS50P", "HELLO"]
  54.  
  55. # for test_case in test:
  56. #     print("Considering", test_case)
  57. #     if is_valid(test_case):
  58. #         print("The outcome of", test_case, "is Valid")
  59. #         print("----")
  60. #     else:
  61. #         print("The outcome of", test_case, "is Invalid")
  62. #         print("----")
Tags: CS50P
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement