Suppose we have a string s, representing a password, we have to check the password criteria. There are few rules, that we have to follow −
- Password length will be At least 8 characters and at most 20 characters long.
- Password contains at least one digit
- Password contains at least one lowercase character and one uppercase character
- Password contains at least one special character like !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
- Password does not contain any other character like tabs or new lines.
So, if the input is like "@bCd12#4", then the output will be True.
To solve this, we will follow these steps −
- a:= 0, b:= 0, c:= 0, d:= 0
- if size of password < 8 or size of password > 20, then
- return False
- for each character i in password, do
- if i is uppercase letter, then
- a := a + 1
- otherwise when i is lowercase letter, then
- b := b + 1
- otherwise when i in these set of special characters '"!"#^modAND\'() *+,- ./:;<=>?@[\\]XOR_`{OR}~"', then
- c := c + 1
- otherwise when i is a digit, then
- d := d + 1
- if i is uppercase letter, then
- if a>=1 and b>=1 and c>=1 and d>=1 and a+b+c+d is same as size of the password , then
- return True
- otherwise,
- return False
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, password): a=0 b=0 c=0 d=0 if len(password)<8 or len(password)>20: return False for i in password: if i.isupper(): a+=1 elif i.islower(): b+=1 elif i in '"!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~"': c+=1 elif i.isdigit(): d+=1 if a>=1 and b>=1 and c>=1 and d>=1 and a+b+c+d==len(password): return True else: return False s = "@bCd12#4" ob = Solution() print(ob.solve(s))
Input
"@bCd12#4"
Output
True