Advertisement
fkudinov

5. Acceptable Password / Вирішуємо задачі на Python CheckIO Українською

Aug 28th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | Source Code | 0 0
  1. # -------------   tasks/acceptable_password.py   ------------
  2.  
  3.  
  4. def has_digits(password: str) -> bool:
  5.     return all([password.isalnum(),
  6.                 not password.isalpha(),
  7.                 not password.isnumeric()])
  8.  
  9.  
  10. def is_acceptable_password(password: str) -> bool:
  11.  
  12.     misses_password_word = "password" not in password.lower()
  13.     is_long = len(password) > 8
  14.     is_short = len(password) >= 7 and not is_long
  15.     digits_check = has_digits(password)
  16.  
  17.     is_valid_short = all([is_short, digits_check, misses_password_word])
  18.     is_valid_long = all([is_long, misses_password_word])
  19.  
  20.     return any([is_valid_short, is_valid_long])
  21.  
  22.  
  23. # --------------     tests/test_acceptable_password.py -------------
  24.  
  25. import pytest
  26.  
  27. from tasks.acceptable_password import is_acceptable_password, has_digits
  28.  
  29.  
  30. @pytest.mark.parametrize(["data", "is_valid"], [
  31.     ["short", False],
  32.     ['short54', True],
  33.     ['muchlonger', True],
  34.     ['ashort', False],
  35.     ['muchlonger5', True],
  36.     ['sh5', False],
  37.     ['1234567', False],
  38.     ['12345678910', True],
  39.     ['password12345', False],
  40.     ['PASSWORD12345', False],
  41.     ['pass1234word', True]
  42. ])
  43. def test_acceptable_password(data, is_valid):
  44.     assert is_acceptable_password(data) == is_valid
  45.  
  46.  
  47. @pytest.mark.parametrize(["data", "is_valid"], [
  48.     ["short", False],
  49.     ['short54', True],
  50.     ['345', False],
  51. ])
  52. def test_has_digits(data, is_valid):
  53.     assert has_digits(data) == is_valid
  54.  
Tags: checkio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement