Start learning free

LESSON 199 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in Scala: Common Passwords with a Set

Practice Read the full explanation

Use a dictionary (or set) as a fast lookup table to reject well-known weak passwords.

You start with:

val common = Set("password", "123456", "qwerty", "letmein")
val password = "qwerty"

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Print "Password: " followed by the password.
  • Look the password up in the set common and store the result as a boolean.
  • Print "Common: " followed by the boolean. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If it is common, print "Weak: reject"; otherwise print "Strong: accept".

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

Use a Set[String] built with Set(...) and call .contains(password).

val common = Set("password", "123456", "qwerty", "letmein")
val password = "qwerty"

println("Password: " + password)
val isCommon = common.contains(password)
println("Common: " + (if (isCommon) "True" else "False"))
if (isCommon) {
    println("Weak: reject")
} else {
    println("Strong: accept")
}

STEP 1 OF 5
    Ctrl + Enter to check

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in Scala, explained

    Problem overview

    This version stops checking a password's contents and asks a different question: is it on a list of well-known weak passwords? The program starts with val common = Set("password", "123456", "qwerty", "letmein") and val password = "qwerty". It prints the password, whether it is common, and a verdict: Weak: reject for a common password and Strong: accept for any other.

    For qwerty the output is:

    Password: qwerty
    Common: True
    Weak: reject

    For Tr0ub4dor it is Password: Tr0ub4dor, Common: False and Strong: accept.

    Approach: ask a set

    A Set holds each value at most once and answers one question well: is this value in it? The list of common passwords is a natural set. Its contains method returns a Boolean, and the default immutable Set is backed by a hash structure for larger sizes, so the lookup stays fast however many passwords the set holds. An alternative is to loop through a list and compare each entry, which gets slower as the list grows.

    The result of the lookup is stored in a val, isCommon, and used twice: once for the Common: line and once for the verdict. Asking the set once keeps the two lines consistent by construction.

    Step by step

    1. Print the password line with println, joining Password: and the password with +.
    2. Look it up. val isCommon = common.contains(password) is true when the set holds that exact text.
    3. Print the Common: line. The boolean is turned into True or False with if (isCommon) "True" else "False", because Scala prints a boolean in lowercase and the output needs a capital.
    4. Write the verdict. An if on isCommon prints Weak: reject.
    5. Add the else branch, which prints Strong: accept.

    The solution in Scala

            val common = Set("password", "123456", "qwerty", "letmein")
            val password = "qwerty"
    
            println("Password: " + password)
            val isCommon = common.contains(password)
            println("Common: " + (if (isCommon) "True" else "False"))
            if (isCommon) {
                println("Weak: reject")
            } else {
                println("Strong: accept")
            }

    Language details:

    • Set("a", "b") builds an immutable Set[String]. Duplicates in the arguments are dropped, and the type is inferred from the elements.
    • contains compares by value, so a string typed by the user matches the entry even though it is a different object. Matching is exact, character for character.
    • A boolean needs its own text. println("Common: " + isCommon) would print Common: true. The if inside the parentheses is an expression that evaluates to one of two strings, so it can be joined with + directly.
    • Naming. isCommon reads like a yes-or-no question, which suits a boolean. It makes the verdict if (isCommon) read almost like English.

    Complexity

    With a set of m entries, one lookup takes about constant time on average, O(1), for sets of any size, and for the tiny sets Scala builds with up to four elements it is a direct comparison. The alternative of scanning a list is O(m) per check. Hashing the password costs time proportional to its length, so a lookup is really O(L) for a password of L characters. Space is O(m) for the set.

    Edge cases and common mistakes

    • Case matters. Qwerty is not in the set, because the entry is lowercase. The output is Common: False and Strong: accept. Lowercasing the password before the lookup would treat the two the same, but this lesson tests the text as typed.
    • Wrong verdict text. The two verdicts are Weak: reject and Strong: accept, with the colon and the second word. They are not the plain Weak and Strong of earlier versions.
    • Using a List of passwords. List.contains gives the same answer, but it scans the list. The set states the intent, membership, and stays fast as it grows.
    • Not common does not mean strong. This check looks only at the list. A password such as a is not in the set, so the program prints Strong: accept for it.
    • Printing the boolean directly. That gives true or false in lowercase, which does not match the required output.

    Testing

    PasswordCommon lineVerdict
    qwertyCommon: TrueWeak: reject
    Tr0ub4dorCommon: FalseStrong: accept
    QwertyCommon: FalseStrong: accept
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free