Advertisement
ifigazsi

Password validator

Jan 18th, 2024 (edited)
1,490
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. #### ITERATION DEF ####
  2.  
  3. def is_password_valid(password):
  4.     length_ok = len(password) > 7
  5.     lowercase_ok = any(c.islower() for c in password)
  6.     uppercase_ok = any(c.isupper() for c in password)
  7.     number_ok = any(c.isdigit() for c in password)
  8.     return sum([length_ok, lowercase_ok, uppercase_ok, number_ok]) == 4
  9.  
  10.  
  11. #### ITERATION 1 ####
  12. import string
  13.  
  14. password = "dfshtj54aA"
  15. lowercase = {x for x in string.ascii_lowercase}
  16. uppercase = {x for x in string.ascii_uppercase}
  17. numbers = {str(x) for x in range(0, 10)}
  18.  
  19. length_ok = len(password) > 7
  20. lowercase_ok = len(lowercase.intersection(password)) > 0
  21. uppercase_ok = len(uppercase.intersection(password)) > 0
  22. number_ok = len(numbers.intersection(password)) > 0
  23.  
  24. if length_ok and lowercase_ok and uppercase_ok and number_ok:
  25.     print("Password ok")
  26. else:
  27.     print("Password not ok")
  28.  
  29. #### ITERATION 2 ####
  30.  
  31. password = "dfshtj54aA"
  32. lowercase_ok = len(set(string.ascii_lowercase).intersection(password)) > 0
  33. uppercase_ok = len(set(string.ascii_uppercase).intersection(password)) > 0
  34. number_ok = len({str(x) for x in range(0, 10)}.intersection(password)) > 0
  35. result = "Password ok" if (len(password) > 7) + lowercase_ok + uppercase_ok + number_ok == 4 else "Password not ok"
  36. print(result)
  37.  
  38.  
  39. #### ITERATION 3 aka VALLIONXD ####
  40.  
  41. password = "dfshtjd4aDa"
  42. lowercase_ok = any(c.islower() for c in password)
  43. uppercase_ok = any(c.isupper() for c in password)
  44. number_ok = any(c.isdigit() for c in password)
  45. result = "Password ok" if (len(password) > 7) + lowercase_ok + uppercase_ok + number_ok == 4 else "Password not ok"
  46. print(result)
  47.  
  48.  
  49. #### ITERATION 4 QUITE UGLY ####
  50.  
  51. password = "dfshtjdaGa"
  52. pwd = [ord(c) for c in password]
  53. print("Password ok" if (len(pwd) > 7) and (47 < min(pwd) < 58) and (96 < max(pwd) < 123)
  54.         and any(c.isupper() for c in password) else "Password not ok")
  55.  
Advertisement
Comments
  • VallionXD
    134 days
    # Python 0.28 KB | 1 0
    1. ###################### EVEN SHORTER ONE #################################
    2.  
    3. import string; print('Password ok' if len('dfshtj54aA') > 7 and any(c.islower() for c in 'dfshtj54aA') and any(c.isupper() for c in 'dfshtj54aA') and any(c.isdigit() for c in 'dfshtj54aA') else 'Password not ok')
  • ifigazsi
    134 days (edited)
    # text 0.15 KB | 0 0
    1. Yes, *any* is a very good idea. Readability counts so i would prefer the first one with any. :)
    2.  
    3. lowercase_ok = any(c.islower() for c in password)
    4. etc.
Add Comment
Please, Sign In to add comment
Advertisement