Problem overview
The last check of the series turns the rules into one number. The program starts with val password = "Secret!99", scores it between 0 and 100, and prints two lines: the length and the score. Each rule adds points when it holds:
| Rule | Points |
|---|---|
| Length at least 8 | 25 |
| Has an uppercase letter | 20 |
| Has a lowercase letter | 20 |
| Has a digit | 20 |
| Has a symbol (not a letter and not a digit) | 15 |
The points add up to 100 when every rule holds. For Secret!99 the output is:
Length: 9
Score: 100
For abc123 the output is Length: 6 and Score: 40: only the lowercase letters and the digits count, 20 each.
Approach: collect the facts, then add up the points
The program has two phases. The first is the single pass over the characters from the earlier flag lesson: four booleans record which classes appear, hasUpper, hasLower, hasDigit and hasSymbol. The second phase turns the facts into points: a score that starts at 0, with one if per rule that adds that rule's points.
Keeping the phases separate makes the scoring easy to read and change. The loop only answers "what is in this password?", and the rules only answer "what is it worth?". Changing a weight touches one line of the second phase and nothing in the loop.
Each rule is its own if, never an else if. The rules are independent, and a password can earn several of them, so none of them may cancel another.
Step by step
- Store the length as
val length = password.length. - Create the four flags,
hasUpper,hasLower,hasDigitandhasSymbol, asvars set tofalse. - Loop over the characters with
for (c <- password). - Set the flags inside the loop with four separate tests:
c.isUpper,c.isLower,c.isDigitand!c.isLetterOrDigit. - Create
scoreas avarset to 0, after the loop. - Score the length: if
length >= 8, add 25 withscore += 25. - Score the uppercase letter, the lowercase letter and the digit: one
ifeach, adding 20. - Score the symbol: if
hasSymbol, add 15. - Print both lines after the points are added, using interpolated strings:
s"Length: $length"ands"Score: $score".
The solution in Scala
val password = "Secret!99"
val length = password.length
var hasUpper = false
var hasLower = false
var hasDigit = false
var hasSymbol = false
for (c <- password) {
if (c.isUpper) {
hasUpper = true
}
if (c.isLower) {
hasLower = true
}
if (c.isDigit) {
hasDigit = true
}
if (!c.isLetterOrDigit) {
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
}
println(s"Length: $length")
println(s"Score: $score")
Details:
- The
ifwithout anelse.if (hasUpper) { score += 20 }does nothing when the flag is false. That is the right shape for a bonus: either it is added or nothing happens. - Testing a boolean directly.
if (hasUpper)is enough. WritinghasUpper == trueworks but only adds noise. - String interpolation.
s"Score: $score"inserts the value ofscoreinto the text. A plain"Score: " + scoregives the same result. +=on avar.score += 25meansscore = score + 25. It needsscoreto be avar, because avalcannot be reassigned.- The weights. 25 + 20 + 20 + 20 + 15 is 100, so the best possible score is exactly 100 and the worst is 0.
Following the score
For abc123 the loop sets hasLower on a and hasDigit on 1; the other two flags stay false. The length is 6, so the first rule fails. Then the checks run in order: length no, uppercase no, lowercase yes (20), digit yes (20), symbol no. The score is 20 + 20 = 40, as printed.
For Secret!99 every rule passes: 25 for the length of 9, then 20, 20 and 20 for the three letter and digit classes, then 15 for the !. That is 100.
Two passwords with the same score can fail different rules. ABCDEFGH and abcdefgh both score 45, but one misses the lowercase letters and the other the capitals. The score summarises the password, and it does not say which rule failed.
Complexity
The loop is one pass over the n characters, so the time is O(n). The scoring after it is five constant-time tests. The extra space is the four flags and the score, which is O(1).
Edge cases and common mistakes
- Empty password. No flag is set and the length is 0, so the output is
Length: 0andScore: 0. - Uppercase only.
ABCDEFGHis long enough and has capitals, so it earns 25 + 20 and the output isScore: 45. - Lowercase only.
abcdefghalso scores 45, from the length and the lowercase letters. - Using
else iffor the rules. The first rule that holds would stop the rest from being tested, and the score would stop growing after one bonus. - Adding points inside the loop. The loop sees many characters of the same class. If the points were added there, a password with three capitals would earn the uppercase bonus three times. The flags exist so each rule counts once.
- Printing before scoring. The two
printlnlines go after the lastif, so thatscoreis final.
Changing the rules
Because the weights are literal numbers in five separate lines, a rule change is a local edit. To make a symbol worth more, change the number in the last if and adjust another weight so that the total stays at 100. To add a rule, such as a bonus for 12 characters or more, add one more if after the existing ones. The loop and the flags do not need to change unless the new rule needs a new kind of fact about the characters.
Testing
| Password | Length line | Score |
|---|---|---|
Secret!99 | Length: 9 | 100 |
abc123 | Length: 6 | 40 |
ABCDEFGH | Length: 8 | 45 |
abcdefgh | Length: 8 | 45 |
| (empty) | Length: 0 | 0 |
