Drill

ProblemsPython › validation

Score a password

mediumvalidationPython

The sign-up form shows a strength meter with five segments, one for each rule the password satisfies.

password_score(password: string) → int

Solve it in the editor →

Where you start

def password_score(password: str) -> int:
    

Worked examples

CallResult
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

The same problem in another language

More validation problems in Python