Problem overview
Version 3 asks four questions about the password instead of one: does it contain an uppercase letter, a lowercase letter, a digit and a symbol? A symbol is any character that is neither a letter nor a digit. The starting code sets password = "Secret!99". The receipt in receipt shows the length, one line per flag in the order Upper, Lower, Digit, Symbol, and a verdict. The verdict is now stricter: Strong needs a length of at least 8 and all four flags true.
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
Approach: one pass, four independent tests
The previous version used one flag, has_digit. This one uses four flags named has_upper, has_lower, has_digit and has_symbol, all starting at False. A single loop over the characters tests each character against all four kinds.
The tests are four separate if statements, not an if/elif chain. Each character falls into one kind at most, but the program still tests every kind for it, and the tests don't interfere. A character that matches no test, such as a space, leaves every flag as it was.
Step by step
- Create
lengthandlines. Then appendLength:followed bystr(length). - Create the four flags as
False:has_upper,has_lower,has_digitandhas_symbol. - Loop over the characters with
for c in password. - Test for uppercase.
c.isupper()setshas_upper. - Test for lowercase.
c.islower()setshas_lower. - Test for a digit.
c.isdigit()setshas_digit. - Test for a symbol.
not c.isalnum()setshas_symbol.isalnum()is true for letters and digits, so thenotleaves everything else. - Append the four flag lines after the loop, in the order Upper, Lower, Digit, Symbol, each with
str()on the flag. - Write the verdict. One
ifjoins five conditions withand: the length and the four flags. TheelseappendsWeak. - Create
receipt."\n".join(lines).
Watching the four flags change
Here is the loop on the short password "Aa1!", with the four flags printed after each character, in the order has_upper, has_lower, has_digit, has_symbol:
'A' True False False False
'a' True True False False
'1' True True True False
'!' True True True True
Each character switches on exactly one flag, and no later character switches one off.
The solution in Python
password = "Secret!99"
length = len(password)
lines = []
lines.append("Length: " + str(length))
has_upper = False
has_lower = False
has_digit = False
has_symbol = False
for c in password:
if c.isupper():
has_upper = True
if c.islower():
has_lower = True
if c.isdigit():
has_digit = True
if not c.isalnum():
has_symbol = True
lines.append("Upper: " + str(has_upper))
lines.append("Lower: " + str(has_lower))
lines.append("Digit: " + str(has_digit))
lines.append("Symbol: " + str(has_symbol))
if length >= 8 and has_upper and has_lower and has_digit and has_symbol:
lines.append("Strong")
else:
lines.append("Weak")
receipt = "\n".join(lines)
Details worth knowing:
- What each method returns for one character. For
"S",isupper()is true andislower()is false. For"5",isdigit()is true andisalnum()is true, so it isn't a symbol. For"!", all four ofisupper(),islower(),isdigit()andisalnum()are false, sonot c.isalnum()is true. - Why four
ifstatements. Withelif, a character would stop at the first test that matched. That happens to work for these tests, but four independentifstatements keep each test self-contained, and they stay correct if the tests ever overlap. - A space is a symbol. It isn't alphanumeric, so "Secret 99" gets
Symbol: Trueand isStrong. - The long condition. A flag is already a boolean, so
and has_upperneeds no== True. - Booleans become text with
str().str(True)is"True".
Complexity
Time: O(n), where n is the number of characters. The loop makes one pass, and each character gets a fixed number of constant-time tests. The four tests per character don't change that, because four is a constant.
Space: O(1) extra for the flags or counters and the few output lines, whatever the password's length.
A stricter verdict
Version 2 needed two conditions for Strong: the length and a digit. This version needs five, so the verdict line is the longest in the program. Every added requirement can only turn a Strong into a Weak, never the other way. A password that passed before, like "Secret99", fails now because it has no symbol.
Why four separate variables
Four named flags read better than one clever value, and each can be reported on its own line. The verdict also reads almost like the rule: the length is at least 8, and the password has an uppercase letter, and a lowercase letter, and a digit, and a symbol. When a password is weak, the four flag lines show which kinds are missing, and the length line shows whether it is long enough.
Edge cases and common mistakes
- Missing a symbol. "Secret99" has length 8 and three flags true, but
Symbol: Falsemakes itWeak. - Missing a digit. "Secret!!!" has
Digit: Falseand isWeak. - Everything but the length. "Aa1!" has all four flags true, but it has only 4 characters, so it is
Weak. - All lowercase. "secret99" gives
Upper: False,Lower: True,Digit: True,Symbol: FalseandWeak. - Treating
not c.isalpha()as "symbol". Digits aren't letters either, so every digit would count as a symbol. - Forgetting one flag in the verdict. With only four conditions, a password could be missing the omitted kind and still be
Strong. - Using
orin the verdict. Withor, a single true flag would be enough forStrong. - Swapping the line order. The lines have to appear as Upper, Lower, Digit, Symbol.
Testing
"Secret!99"gives fourTruelines andStrong."secret99"givesUpper: False,Lower: True,Digit: True,Symbol: FalseandWeak."Secret99"givesSymbol: FalseandWeak."Aa1!"gives fourTruelines,Length: 4andWeak."Secret 99"gives fourTruelines andStrong.
