Problem overview
Version 6 rates a password with a number. It checks five facts, and each true fact adds points to a score: a length of at least 8 gives 25, an uppercase letter 20, a lowercase letter 20, a digit 20 and a symbol 15. The five amounts add up to 100. The starting code sets password = "Secret!99", and the receipt in receipt has two lines, the length and the score.
Length: 9
Score: 100
For "abc123" only the lowercase letters and the digits count, so the lines are Length: 6 and Score: 40.
Approach: find the facts, then add up the points
The program works in two phases. First, a loop over the characters sets four flags: has_upper, has_lower, has_digit and has_symbol. Then, a series of separate if statements turns the facts into points.
The scoring statements are independent if statements, not an if/elif chain. Every rule that holds adds its own points, so a password can earn several of them. An elif chain would stop after the first rule that matched.
Step by step
- Create
length.len(password), used for the 25-point rule and for the first output line. - Create four flags at
False:has_upper,has_lower,has_digitandhas_symbol. - Loop over the characters.
c.isupper()setshas_upper,c.islower()setshas_lower,c.isdigit()setshas_digit, andnot c.isalnum()setshas_symbol. Each test is its ownif. - Create
scoreat 0. It is a total that nothing has added to yet. - Add the points. One
iffor each rule:length >= 8adds 25,has_upper20,has_lower20,has_digit20 andhas_symbol15, each withscore += .... - Create the empty list
lines. - Append the length line and the score line, in that order, each with
str()on the number. - Create
receipt."\n".join(lines).
Watching the points add up
For "abc123", which has no uppercase letter, no symbol and a length of 6, only two rules fire. Printing the score after each rule that adds points gives:
lower 20
digit 40
The final score is 40. The length rule, the uppercase rule and the symbol rule were all skipped, and each skipped rule adds nothing.
The solution in Python
password = "Secret!99"
length = len(password)
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
score = 0
if length >= 8:
score += 25
if has_upper:
score += 20
if has_lower:
score += 20
if has_digit:
score += 20
if has_symbol:
score += 15
lines = []
lines.append("Length: " + str(length))
lines.append("Score: " + str(score))
receipt = "\n".join(lines)
Details worth knowing:
- The weights are uneven. Length is worth the most, and the symbol is worth the least, 15. The five numbers were picked to sum to exactly 100, so a password that satisfies every rule scores 100.
score += 25. It meansscore = score + 25. It needsscoreto exist already, which is whyscore = 0comes first.- A flag works as a condition.
if has_upper:is enough, becausehas_upperis alreadyTrueorFalse. - The loop and the scoring are separate. The loop runs 9 times for "Secret!99" and only records facts. It doesn't touch
score. That keeps each phase easy to read and to test. - The scores are only sums of the rules. Not every number from 0 to 100 can occur, because the points come in fixed blocks.
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 five scoring statements run once each, however long the password is.
Space: O(1) extra for the flags or counters and the few output lines, whatever the password's length.
Reading the score
The score is a sum of independent contributions, so it can be explained rule by rule. A score of 45 means the length rule and one 20-point rule fired, and a score of 55 for a short password means all of uppercase, lowercase and symbol were present. Because each rule adds a fixed amount, one more rule would only mean one more if, with the weights rebalanced to keep the total at 100.
Edge cases and common mistakes
- Short passwords can still score. "Abc!" has 4 characters, so it gets no length points, but it earns 20 + 20 + 15 and scores 55.
- Long, one kind only. "abcdefgh" has 8 characters and lowercase letters, so it scores 25 + 20, which is 45. "ABCDEFGH" also scores 45.
- All rules at once. "Secret!99" earns 25 + 20 + 20 + 20 + 15, which is 100, the most a password can score.
- The empty password. Nothing is earned, and the lines are
Length: 0andScore: 0. - A space is a symbol. "Secret 99" scores 100, the same as "Secret!99".
- Using
eliffor the scoring. Only one rule could add points, and the score would never go past 25. - Setting
score = 25instead of adding. Each rule would overwrite the earlier points. The statements must use+=. - Putting
score = 0after the rules. It would erase the points just added.
Testing
"Secret!99"givesLength: 9andScore: 100."abc123"givesLength: 6andScore: 40."abcdefgh"givesLength: 8andScore: 45."Abc!"givesLength: 4andScore: 55.""givesLength: 0andScore: 0.
