Problem overview
This version turns the earlier yes-or-no checks into one number. A password earns points for each good property, and the program prints its length and its score. You start with let password = "Secret!99". The points are:
- 25 for a length of at least 8;
- 20 for an uppercase letter;
- 20 for a lowercase letter;
- 20 for a digit;
- 15 for a symbol, which is any character that is not a letter and not a digit.
The five amounts add up to 100. For "Secret!99" the output is:
Length: 9
Score: 100
For "abc123" the output is Length: 6 and Score: 40: it has lowercase letters and digits, but no capital, no symbol and not enough characters.
Approach: find the facts, then add up the points
The solution has two stages. The first collects facts about the password: its length, and four flags saying whether an uppercase letter, a lowercase letter, a digit and a symbol occur. A single loop over the characters sets the flags, as in the earlier versions of the checker. The second stage turns facts into points. It starts the score at 0 and runs five independent if statements, each adding its own amount when its fact is true.
The score rules are separate ifs, not an if/else if chain, because the rules aren't alternatives. A password can earn points from every rule, so no rule may skip the others. Keeping the two stages apart also makes the scoring easy to change: another weight is a one-number edit in one place, and the loop stays as it is.
The score is a measure, not a verdict: this version prints the number and leaves it to the reader to decide what a good score is. The input is a password that is already fixed in the code, so the program has no branch for a wrong kind of input. Whatever text it gets, the answer is a number from 0 to 100.
Step by step
- Store the length.
let length = password.countis used twice, for the score rule and for the printed line. - Create
hasUpper,hasLower,hasDigitandhasSymbolasvarflags starting atfalse, one step each. A flag only ever changes from false to true. - Loop over the characters.
for character in passwordruns once per character, 9 times for"Secret!99". - Raise the flags. In four separate
ifs,isUppercasesetshasUpper,isLowercasesetshasLower,isNumbersetshasDigit, and!character.isLetter && !character.isNumbersetshasSymbol. - Create
scoreas avarset to 0, after the loop. - Score the length. If
length >= 8,score += 25. - Score the four classes. One
ifper flag: 20 forhasUpper, 20 forhasLower, 20 forhasDigitand 15 forhasSymbol. - Print the results. The
Length:line first and theScore:line second, after the score is final.
The solution in Swift
let password = "Secret!99"
let length = password.count
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
}
}
var score = 0
if length >= 8 {
score += 25
}
if hasUpper {
score += 20
}
if hasLower {
score += 20
}
if hasDigit {
score += 20
}
if hasSymbol {
score += 15
}
print("Length: \(length)")
print("Score: \(score)")
The Swift details worth knowing:
- A
Boolis a complete condition.if hasUpper {needs no comparison withtrue. The flags are declared asvarbecause the loop assigns them, andscorebecause it is raised with+=. - Inferred types.
var score = 0is anIntandvar hasUpper = falseis aBool. No annotations are needed. - The properties follow Unicode.
isUppercase,isLowercase,isLetterandisNumberareBoolproperties ofCharacter, and they cover accented letters as well as the English alphabet. - Length counts characters.
countgives the number of characters, so an accented letter counts as one. - The maximum is 100 by construction. The five amounts sum to 100, so no check against going over is needed.
Edge cases and common mistakes
- Long but one-dimensional. Length alone is worth only 25 points. A password of 8 or more characters with a single class of character can't reach even half of the 100.
- The empty password. No flag is raised and the length is 0, so the output is
Length: 0andScore: 0. - Only one class, long enough.
"ABCDEFGH"scores 45: 25 for the length and 20 for the uppercase letters. - Adding a symbol.
"abcdefgh!"scores 60: 25 for the length, 20 for lowercase and 15 for the symbol. - Using
else iffor the rules. After the first matching rule the others would be skipped, and the score could never pass 25. - Counting a class once per character. Adding 20 inside the loop for every uppercase letter would give each capital its own 20 points. The flags make each class worth its points once, however many characters belong to it.
- Printing before the score is complete. The two
printlines come after all theifs. Earlier, they would show a partial score.
Testing
Change password in the editor. These results come from the reference solution:
"abc123"printsLength: 6andScore: 40.""printsLength: 0andScore: 0."ABCDEFGH"printsLength: 8andScore: 45."abcdefgh!"printsLength: 9andScore: 60.
