Problem overview
The checker now asks for variety. A password is Strong only when it is at least 8 characters long and contains an uppercase letter, a lowercase letter, a digit and a symbol. A symbol is any character that is neither a letter nor a digit. The program starts with val password = "Secret!99" and prints the length, one line for each of the four flags, in the order Upper, Lower, Digit, Symbol, and the verdict.
For Secret!99 the output is:
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
For secret99 there is no capital and no symbol, so the output is Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
Approach: one pass, four flags
The previous version tracked one flag, hasDigit. This one scales the same idea to four: hasUpper, hasLower, hasDigit and hasSymbol, all starting at false. A single loop over the characters tests each character against all four classes, and each test can only switch its own flag to true.
The four tests are separate if statements, not an if / else if chain. A character belongs to at most one class, so a chain would also give correct answers, but separate tests make each rule independent and readable, and each one can be changed without affecting the others.
The symbol test is defined by exclusion. Rather than listing symbols, the code asks whether the character is not a letter and not a digit. c.isLetterOrDigit is true for both kinds, so negating it with ! leaves the symbols, including the space.
Step by step
- Store the length in
val length. - Print the length line, here with an interpolated string,
s"Length: $length". - Create
hasUpperandhasLowerasvars set tofalse. - Create
hasDigitandhasSymbolthe same way. - Start the loop
for (c <- password), with its closing brace written right away. - First test: if
c.isUpper, sethasUppertotrue. - Second test: if
c.isLower, sethasLowertotrue. - Third test: if
c.isDigit, sethasDigittotrue. - Fourth test: if
!c.isLetterOrDigit, sethasSymboltotrue. - After the loop, print the four flag lines, in order, each turned into
TrueorFalsewith anifthat gives a value. - Write the verdict. Join five conditions with
&&: the length and the four flags. PrintStrongif all hold, andWeakin theelse.
The solution in Scala
val password = "Secret!99"
val length = password.length
println(s"Length: $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
}
}
println("Upper: " + (if (hasUpper) "True" else "False"))
println("Lower: " + (if (hasLower) "True" else "False"))
println("Digit: " + (if (hasDigit) "True" else "False"))
println("Symbol: " + (if (hasSymbol) "True" else "False"))
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
println("Strong")
} else {
println("Weak")
}
Scala details in this solution:
- String interpolation. The
sbefore the quotes ins"Length: $length"turns on interpolation, so$lengthis replaced by the variable's value. The other lines use+. Both forms give the same text, and a literal dollar sign in ansstring is written$$. isUpperandisLowerare methods onChar. Letters from other alphabets count too, so an accented lowercase letter such aséis lowercase.!c.isLetterOrDigituses!to flip aBoolean. It applies to the result of the call, so!c.isLetterOrDigitreads as c is not a letter or digit.- Booleans print in lowercase. Printing a flag directly would give
true, so every flag line picks its text withif (flag) "True" else "False", in parentheses. - Repeated shape. The four flag lines differ only in the label and the flag. When code repeats like this it is correct but long, and a small helper function could remove the repetition. The lesson keeps it explicit so that each line can be checked on its own.
Following the loop
For Secret!99 the flags change in the order the characters arrive. The first character, S, sets hasUpper. The next, e, sets hasLower. The ! is neither a letter nor a digit, so it sets hasSymbol, and the first 9 sets hasDigit. After that, every flag is already true and the remaining characters change nothing. Since each character reaches all four tests, it does not matter in which order the classes show up in the password.
Complexity
The loop visits each of the n characters once and does four constant-time tests per character, so the time is O(n). The program keeps a length and four booleans, so the extra space is O(1).
Edge cases and common mistakes
- The space is a symbol.
Secret 99has a space and no punctuation. The space is neither a letter nor a digit, soSymbol: Trueand the password isStrong. - Missing exactly one class.
Secret99has 8 characters, a capital, small letters and digits, but no symbol. The output hasSymbol: FalseandWeak. - Using
else ifbetween the tests. It works for this data, but then a later test is skipped whenever an earlier one matches, which makes changes to the rules riskier. - Treating a symbol as "not a letter". A digit is not a letter either, so the symbol test needs to exclude digits too. Testing only
!c.isLetterwould mark every digit as a symbol. - Forgetting the length. A password of seven characters with all four classes, such as
Aa1!xyz, has all four flags but is stillWeak. - Wrong order of lines. The flag lines must be Upper, Lower, Digit and Symbol, as listed.
Testing
| Password | Flags (Upper, Lower, Digit, Symbol) | Verdict |
|---|---|---|
Secret!99 | True, True, True, True | Strong |
secret99 | False, True, True, False | Weak |
Secret 99 | True, True, True, True | Strong |
Secret99 | True, True, True, False | Weak |
