Problem overview
The checker now wants variety. A password is strong when it has at least 8 characters and contains an uppercase letter, a lowercase letter, a digit and a symbol. A symbol is any character that is not a letter and not a digit. You start with let password = "Secret!99", and the program prints the length, one line per class and a verdict:
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
For "secret99" the output is Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak. The booleans print as True and False, with a capital letter.
Approach: four flags, one pass
The previous version used one flag for digits. This one scales the idea up: hasUpper, hasLower, hasDigit and hasSymbol, all starting at false. A single loop over the characters can raise all four. For every character, the program asks four questions, one if for each.
The four tests are separate if statements, not an if/else if chain. A chain stops at the first test that matches, and the others are never asked. Separate ifs each test the character on their own, so adding a fifth class later means adding one more block and touching nothing else.
The classes aren't a partition of all characters. A letter from a script without cases, such as "中", is a letter, but it is neither uppercase nor lowercase: it raises none of the first two flags and is not a symbol either. Four independent tests handle that case without any special code.
Flags are enough here because the rule asks only whether each class appears, not how often. Once a flag is true it stays true, and the loop could not do better by remembering more. The length is a separate rule: it comes from count and doesn't need the loop at all.
Step by step
- Store the length in
lengthand printLength:with it. - Create the first two flags,
hasUpperandhasLower, asvarvalues starting atfalse. - Create the other two,
hasDigitandhasSymbol, in the same way. - Start the loop:
for character in passwordhands you one character at a time. One pass checks all four classes. - Uppercase test.
character.isUppercasesetshasUpper. - Lowercase test.
character.isLowercasesetshasLower, in its ownifafter the first. - Digit test.
character.isNumbersetshasDigit. - Symbol test. A symbol is neither a letter nor a digit, so the condition is
!character.isLetter && !character.isNumber. The!in front of each part flips true and false. - Print the first two flags after the loop,
UpperandLower, usingflag ? "True" : "False"inside the interpolation. - Print the other two,
DigitandSymbol, keeping the order: Upper, Lower, Digit, Symbol. - Write the verdict.
Strongneeds five conditions joined with&&: the length and the four flags. - Add the
else: a password that misses any one of the five getsWeak.
The solution in Swift
let password = "Secret!99"
let length = password.count
print("Length: \(length)")
var hasUpper = false
var hasLower = false
var hasDigit = false
var hasSymbol = false
for character in password {
if character.isUppercase {
hasUpper = true
}
if character.isLowercase {
hasLower = true
}
if character.isNumber {
hasDigit = true
}
if !character.isLetter && !character.isNumber {
hasSymbol = true
}
}
print("Upper: \(hasUpper ? "True" : "False")")
print("Lower: \(hasLower ? "True" : "False")")
print("Digit: \(hasDigit ? "True" : "False")")
print("Symbol: \(hasSymbol ? "True" : "False")")
if length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol {
print("Strong")
} else {
print("Weak")
}
Swift details in this solution:
- The properties live on
Character.isUppercase,isLowercase,isLetterandisNumberare allBoolproperties, with no parentheses and no imports. They follow Unicode, so"É"counts as uppercase and"é"as lowercase, not only the English alphabet. - Symbols are defined by exclusion. There is no
isSymbolin the lesson's code: a symbol is what is left after letters and digits. A space therefore counts as a symbol. With"Secret 99"the lesson's code printsSymbol: TrueandStrong. - The ternary is repeated four times. Swift prints a
Boolastrueorfalse, so each line picks its own text to get the capital letter. &&chains. Five conditions joined with&&are all required, and the evaluation stops at the first false one.- An underscore is a symbol. It is neither a letter nor a number in Unicode terms, so
_setshasSymbol.
Edge cases and common mistakes
- Missing a class.
"SECRET!99"has no lowercase letter: it printsLower: FalseandWeak."Secret!!!"has no digit and isWeaktoo. - Forgetting one of the five conditions. Leaving out
length >= 8would call a short password such as"Ab1!"strong. - Using
else ifbetween the tests. The chain would skip every test after the first match, which couples the tests to each other. Separateifs remove that risk. - Writing the symbol test as
||.!isLetter || !isNumberis true for ordinary characters such asa(not a number) and9(not a letter), so it would flag almost every character as a symbol. The two negations must be joined with&&. - Printing the flags inside the loop. Each flag line should appear once, after the loop has seen the whole password.
- Changing the order of the lines. The output is Upper, Lower, Digit, Symbol, in that order.
Testing
Change password in the editor. These results come from the reference solution:
"secret99"printsLength: 8,Upper: False,Lower: True,Digit: True,Symbol: FalseandWeak."SECRET!99"printsLength: 9,Upper: True,Lower: False,Digit: True,Symbol: TrueandWeak."Secret!!!"printsLength: 9,Upper: True,Lower: True,Digit: False,Symbol: TrueandWeak."Secret 99"printsLength: 9, all four flags asTrueandStrong.
