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
- Print the password line with
println, joiningPassword:and the password with+. - Look it up.
val isCommon = common.contains(password)istruewhen the set holds that exact text. - Print the
Common:line. The boolean is turned intoTrueorFalsewithif (isCommon) "True" else "False", because Scala prints a boolean in lowercase and the output needs a capital. - Write the verdict. An
ifonisCommonprintsWeak: reject. - Add the
elsebranch, which printsStrong: 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 immutableSet[String]. Duplicates in the arguments are dropped, and the type is inferred from the elements.containscompares 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 printCommon: true. Theifinside the parentheses is an expression that evaluates to one of two strings, so it can be joined with+directly. - Naming.
isCommonreads like a yes-or-no question, which suits a boolean. It makes the verdictif (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.
Qwertyis not in the set, because the entry is lowercase. The output isCommon: FalseandStrong: 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: rejectandStrong: accept, with the colon and the second word. They are not the plainWeakandStrongof earlier versions. - Using a
Listof passwords.List.containsgives 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
ais not in the set, so the program printsStrong: acceptfor it. - Printing the boolean directly. That gives
trueorfalsein lowercase, which does not match the required output.
Testing
| Password | Common line | Verdict |
|---|---|---|
qwerty | Common: True | Weak: reject |
Tr0ub4dor | Common: False | Strong: accept |
Qwerty | Common: False | Strong: accept |
