Problems › Python › validation
Score a password
The sign-up form shows a strength meter with five segments, one for each rule the password satisfies.
- One point each for: being at least 8 characters, containing a lowercase letter, containing an uppercase letter, containing a digit, and containing anything else.
- The score is therefore between 0 and 5.
password_score(password: string) → int
Where you start
def password_score(password: str) -> int:
Worked examples
| Call | Result |
|---|---|
password_score("abc") | 1 |
password_score("abcdefgh") | 2 |
password_score("Abcdefg1") | 4 |
password_score("Abcdefg1!") | 5 |
Hint
Five independent booleans, added up. No early returns.
Reference solution in Python
def password_score(password: str) -> int:
lower = any('a' <= c <= 'z' for c in password)
upper = any('A' <= c <= 'Z' for c in password)
digit = any('0' <= c <= '9' for c in password)
other = any(not ('a' <= c <= 'z' or 'A' <= c <= 'Z' or '0' <= c <= '9') for c in password)
return (1 if len(password) >= 8 else 0) + lower + upper + digit + other