Problem overview
Version 4 stops asking whether a kind of character is present and counts how many characters of each kind there are. The starting code sets password = "Hello!World9". The program uses four integer counters, uppercase, lowercase, digits and symbols, and stores four lines in receipt. A symbol is any character that is neither a letter nor a digit. There is no verdict line in this version.
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
The password has 12 characters, and 2 + 8 + 1 + 1 is 12, so every character has been counted once.
Approach: counters instead of flags
A flag remembers "at least one". A counter remembers "how many", so the assignment = True becomes += 1. Each counter starts at 0. The loop visits every character, and each of four if statements adds 1 to the counter that matches.
After the loop, the program builds one line per counter in a fixed order and joins the lines.
Step by step
- Create four counters at 0:
uppercase,lowercase,digitsandsymbols. - Loop over the characters with
for c in password. For this password, the loop body runs 12 times. - Count uppercase letters. When
c.isupper()is true,uppercase += 1. - Count lowercase letters. When
c.islower()is true,lowercase += 1. - Count digits. When
c.isdigit()is true,digits += 1. - Count symbols. When
not c.isalnum()is true,symbols += 1. A space counts too. - Create the empty list
linesbelow the loop. - Append the four lines in the order Uppercase, Lowercase, Digits, Symbols. Each is a label followed by the counter turned into text with
str(). - Create
receipt."\n".join(lines).
Watching the counters change
Here is the loop on the password "Ab1!x", with the four counters printed after each character, in the order uppercase, lowercase, digits, symbols:
'A' 1 0 0 0
'b' 1 1 0 0
'1' 1 1 1 0
'!' 1 1 1 1
'x' 1 2 1 1
Each character raises exactly one counter. That gives a useful self-check: the four counters add up to len(password), here 5. The sum equals the length for ordinary passwords, though a letter without a case, such as one in "日本語", is counted by none of the four.
The solution in Python
password = "Hello!World9"
uppercase = 0
lowercase = 0
digits = 0
symbols = 0
for c in password:
if c.isupper():
uppercase += 1
if c.islower():
lowercase += 1
if c.isdigit():
digits += 1
if not c.isalnum():
symbols += 1
lines = []
lines.append("Uppercase: " + str(uppercase))
lines.append("Lowercase: " + str(lowercase))
lines.append("Digits: " + str(digits))
lines.append("Symbols: " + str(symbols))
receipt = "\n".join(lines)
Details worth knowing:
+= 1. It is shorthand foruppercase = uppercase + 1. It works only on a variable that already has a value, which is why the counters are created before the loop.- Each test is its own
if. A character is examined by all four tests, and at most one of them fires for it. - Labels differ from version 3. The lines are
DigitsandSymbolsin the plural, andUppercaseandLowercasein full, so copy the label text carefully. - Letters from other alphabets. The methods work on Unicode text. In "ÉÀé1" the counts are 2 uppercase, 1 lowercase, 1 digit and 0 symbols. Characters that are letters without a case, such as "日本語", are alphanumeric but neither upper nor lower, so they go in none of the four counters.
- Counters are plain integers. They need
str()to be joined with a label, like every number in these lessons.
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. Building the four output lines is constant work.
Space: O(1) extra for the flags or counters and the few output lines, whatever the password's length.
From flags to counts
The structure is the same as in the flag version: variables created before the loop, one pass, tests inside the loop, output after it. Only the update changes. A flag records that something happened, and a counter records how many times. Counting loses nothing, because a count above 0 answers the question "is there at least one?" as well. The reverse isn't true: a flag can't tell 1 from 8.
Edge cases and common mistakes
- Spaces. "Pass word!2" has 1 uppercase letter, 7 lowercase letters, 1 digit and 2 symbols: the space and the exclamation mark.
- The empty password. The loop doesn't run, and all four lines show 0.
- Starting a counter at 1. Every count would be one too high.
- Creating counters inside the loop. They would reset to 0 on every character.
- Writing the symbol test as
not c.isalpha(). Digits aren't letters, so every digit would also be counted as a symbol. The test must benot c.isalnum(). - Counting with
len.len(password)is the total, not the count of one kind. Each counter has to be raised one character at a time, inside its own test. - Appending lines inside the loop. The program would produce four lines for every character.
Testing
"Hello!World9"gives 2, 8, 1 and 1."Pass word!2"gives 1, 7, 1 and 2.""gives 0, 0, 0 and 0."ÉÀé1"gives 2, 1, 1 and 0.
