Start learning free

LESSON 200 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 in Scala: A 0 to 100 Score

Practice Read the full explanation

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

val password = "Secret!99"

Build:

Length: 9
Score: 100

Rules:

  • Start with score = 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Print "Length: " followed by the length, then print "Score: " followed by the score.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Use the same c.isUpper, c.isLower, c.isDigit, c.isLetterOrDigit checks, then accumulate into var score.

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")

STEP 1 OF 18
    Ctrl + Enter to check

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in Scala, explained

    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:

    RulePoints
    Length at least 825
    Has an uppercase letter20
    Has a lowercase letter20
    Has a digit20
    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

    1. Store the length as val length = password.length.
    2. Create the four flags, hasUpper, hasLower, hasDigit and hasSymbol, as vars set to false.
    3. Loop over the characters with for (c <- password).
    4. Set the flags inside the loop with four separate tests: c.isUpper, c.isLower, c.isDigit and !c.isLetterOrDigit.
    5. Create score as a var set to 0, after the loop.
    6. Score the length: if length >= 8, add 25 with score += 25.
    7. Score the uppercase letter, the lowercase letter and the digit: one if each, adding 20.
    8. Score the symbol: if hasSymbol, add 15.
    9. Print both lines after the points are added, using interpolated strings: s"Length: $length" and s"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 if without an else. 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. Writing hasUpper == true works but only adds noise.
    • String interpolation. s"Score: $score" inserts the value of score into the text. A plain "Score: " + score gives the same result.
    • += on a var. score += 25 means score = score + 25. It needs score to be a var, because a val cannot 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: 0 and Score: 0.
    • Uppercase only. ABCDEFGH is long enough and has capitals, so it earns 25 + 20 and the output is Score: 45.
    • Lowercase only. abcdefgh also scores 45, from the length and the lowercase letters.
    • Using else if for 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 println lines go after the last if, so that score is 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

    PasswordLength lineScore
    Secret!99Length: 9100
    abc123Length: 640
    ABCDEFGHLength: 845
    abcdefghLength: 845
    (empty)Length: 00
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free