Problem overview
A password can pass every length and character rule and still be a bad choice, because attackers try well-known passwords first. This version rejects passwords found in a small list of common ones. The program starts with a set common holding "password", "123456", "qwerty" and "letmein", and a password to check, here "qwerty".
It prints the password, whether it is common, and a verdict. For qwerty:
Password: qwerty
Common: True
Weak: reject
For Tr0ub4dor, which is not in the set:
Password: Tr0ub4dor
Common: False
Strong: accept
The Common line uses True or False with a capital first letter, so the program picks that text itself.
Approach: ask a set whether it holds the value
The question is a membership test: is this exact text one of the known ones? A Set is built for that. It stores each value once and answers "do you contain this?" quickly, however many values it holds. Scanning a list from the start would also work for four entries, but it would get slower as the list of common passwords grew to thousands. With a set, the code stays the same and the lookup stays fast.
The result of the lookup is a boolean, which the program stores in a variable and uses twice: once to print the Common line and once to decide the verdict. In this version, being common settles the verdict by itself, whatever else is true of the password.
Step by step
- Print the password. The first line is
"Password: "joined topasswordwith+. - Do the lookup. Create
isCommonfromcommon.contains(password), which istruewhen the set holds that exact text. - Print the
Commonline. Choose"True"or"False"with anifexpression, because Kotlin prints aBooleanin lowercase. - Write the verdict. An
ifonisCommonprintsWeak: reject. - Add the
else. It printsStrong: acceptfor every password the set did not contain.
The solution in Kotlin
val common = setOf("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")
}
Kotlin details worth a closer look:
setOf. It builds a read-onlySet<String>from its arguments. Duplicates are dropped, and the set is only ever read in this lesson, so read-only is the right choice. UsemutableSetOfif you need to add entries later.containsandin.common.contains(password)can also be writtenpassword in common, which many Kotlin programmers prefer for readability. Both give the same answer.- Exact matches only. Strings are compared by content, using
equals, and case counts.QWERTYis notqwerty, so the set does not contain it, and the program printsCommon: FalseandStrong: acceptfor it. - Booleans in text.
println("Common: " + isCommon)would show lowercasetrue. Theif (isCommon) "True" else "False"expression gives the exact text needed; the parentheses make it one operand of+. - The word "Strong" is relative. In this version
Strong: acceptmeans only "not on the common list". The program does not check length or character classes.
Complexity
A lookup in a hash-based set takes constant time on average, so the check costs the same however long the list is, apart from hashing the password itself. The set takes space proportional to the number of stored passwords.
Edge cases and common mistakes
- Case differences. A common password in capitals slips through, as the test below shows. Real checkers often lowercase the password before the lookup; this lesson matches exactly.
- Printing before looking up. The order of the lines is fixed: password, common flag, verdict.
- Printing the flag raw. The output needs a capital letter, which a plain
Booleancan't give. - Mixing up the branches. A common password must be rejected: the
ifbranch is the rejecting one.
Testing
Try QWERTY in capitals. The set holds only the lowercase form, so the program prints:
Password: QWERTY
Common: False
Strong: accept
This is the gap an exact lookup leaves, and it is worth knowing before relying on such a list.
