nikolask

CS50P - PSET2_PLATES ver. 2

Aug 11th, 2023 (edited)
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 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.         check_digits_after_digit(s)
  11.         and text_length(s)
  12.         and punctuation_check(s)
  13.         and two_letters(s)
  14.         and zero_first_digit(s)
  15.     )
  16.  
  17. # check if the first letters are alpha
  18. def two_letters(text):
  19.     return text[:2].isalpha()
  20.  
  21. # check the length of the string
  22. def text_length(text):
  23.     return 2 <= len(text) <= 6
  24.  
  25. # check if there is a number in the plate and if it the first one is zero
  26. def zero_first_digit(text):
  27.     for c in text:
  28.         if c.isdigit() and int(c) != 0: # check if there is a number
  29.             print("good to go 0 not first digit")
  30.             return True
  31.         else:
  32.             if c.isdigit() and int(c) == 0:
  33.                 print("first digit is zero")
  34.                 return False
  35.  
  36. # Check if the characters following a digit to the end of the string, are digits
  37. def check_digits_after_digit(text): # if everything is alpha
  38.     print("test alpha / numbers check is running")
  39.     if text.isalpha():
  40.         print("all chars are ALPHA")
  41.         return True
  42.     else:
  43.         found_digit = False # initialize var to store location of first digit and set it to False
  44.         for c in text:
  45.             if c.isdigit():
  46.                 found_digit = True # found first digit set flag to true
  47.                 idx = text.index(c) # get the index location of the digit
  48.                 break
  49.  
  50.         if found_digit:
  51.             print("index is", idx)
  52.             if text[idx:].isdigit(): # if all the rest chars are digit
  53.                 print("all the rest are digits")
  54.                 return True
  55.             else: # if the rest are not digit
  56.                 print("not all digits")
  57.                 return False
  58.  
  59.  
  60. # check if there is punctuation in text
  61. def punctuation_check(text):
  62.         for c in text:
  63.             if c in "'.,;:?! ":
  64.                 print("punctuation fault")
  65.                 return False
  66.             else:
  67.                 return True
  68.  
  69. # if __name__ == "__main__":
  70. #     main()
  71.  
  72. test = ["CS50", "ECTO88", "NRVOUS",  "CS05", "CS50P2", "PI3.14", "H", "OUTATIME", "CS50P", "HELLO"]
  73.  
  74. for test_case in test:
  75.     print("Considering", test_case)
  76.     if is_valid(test_case):
  77.         print("The outcome of", test_case, "is Valid")
  78.         print("----")
  79.     else:
  80.         print("The outcome of", test_case, "is Invalid")
  81.         print("----")
Tags: CS50P
Advertisement
Add Comment
Please, Sign In to add comment