Problem overview
The checker gets a second rule. A strong password must now have at least 8 characters and also contain at least one digit. The program starts with val password = "Secret99" and prints three lines: the length, whether a digit was found, and the verdict, Strong or Weak.
For Secret99 the output is:
Length: 8
Has digit: True
Strong
The flag line must read True or False with a capital letter, as in the example. For abcdefgh the output is Length: 8, Has digit: False and Weak.
Approach: a flag set by a loop
The length is one lookup, but "contains a digit" needs a look at every character. The standard pattern is a boolean flag. It starts as false, meaning no digit has been seen yet, and a loop over the characters turns it to true when it meets a digit. After the loop, the flag answers the question for the whole string.
The flag is only ever switched one way. Once a digit has been found, later characters must not switch it back, so the loop has no else branch: it either sets the flag or does nothing.
The verdict combines both rules with "and": the password is Strong only if the length is at least 8 and the flag is true. Everything else, whether too short, digit-free or both, is Weak.
Step by step
- Store the length in
val length. - Print the first line,
Length:followed by the number. - Create the flag.
var hasDigit = falseis avar, because it changes when a digit is found. - Loop over the characters with
for (c <- password). The arrow<-reads as "taken from", and each pass givescthe next character. - Test each character with
c.isDigitand sethasDigittotrueinside theif. - After the loop, print the flag line. The flag is turned into
TrueorFalsewith anifthat gives a value. - Write the verdict
ifwithlength >= 8 && hasDigit, printingStrong. - Add the
elsebranch, which printsWeak.
The solution in Scala
val password = "Secret99"
val length = password.length
println("Length: " + length)
var hasDigit = false
for (c <- password) {
if (c.isDigit) {
hasDigit = true
}
}
println("Has digit: " + (if (hasDigit) "True" else "False"))
if (length >= 8 && hasDigit) {
println("Strong")
} else {
println("Weak")
}
Details worth knowing:
- A string is a sequence of characters.
for (c <- password)loops over aStringdirectly, andcis aChar. No index is needed. c.isDigitistruefor decimal digits. It also accepts digits from other scripts, so the Arabic-Indic digit three,'\u0663', counts as a digit too. For the passwords in this lesson that makes no difference.- Booleans print in lowercase.
println("Has digit: " + hasDigit)would printHas digit: true. The lesson needs a capital, so it picks the text itself. ifis an expression.(if (hasDigit) "True" else "False")has a value: the first string when the flag is true and the second when it is false. The parentheses keep the wholeif/elsetogether as one operand of the+.&&is "and". It is true only when both sides are, and it skips the right side when the left is already false.
Following the loop
For Secret99 the flag stays false while the loop passes S, e, c, r, e and t, because none of them is a digit. At the first 9 the condition is true and the flag becomes true. The second 9 sets it to true again, which changes nothing. The loop always visits all eight characters, even after the flag is set, because this version does not stop early.
A shorter way to ask the same question
Scala strings also have an exists method that takes a test and answers whether any character passes it. "Secret99".exists(_.isDigit) gives true and "abcdefgh".exists(_.isDigit) gives false. It does the same job as the loop and the flag in one expression. This lesson builds the flag by hand because the loop-and-flag pattern is the general tool: it works for any test you can write on one character.
Complexity
The loop makes one pass over the n characters and does constant work on each, so the time is O(n). The program stores a few values, whatever the password length, so the extra space is O(1). Asking for the length is constant time, so the length check adds nothing to the cost.
Edge cases and common mistakes
- Digits only.
12345678has 8 characters and a digit, so it isStrongunder these two rules. Under this lesson's rules, only the length and the digit matter. - Long but no digit.
abcdefghprintsHas digit: FalseandWeak. - A digit but short.
short1printsLength: 6,Has digit: TrueandWeak. The flag line reports the digit, and the verdict still fails on length. - Resetting the flag. Adding an
else { hasDigit = false }inside the loop makes the flag reflect only the last character. Leave the flag alone when the character is not a digit. - Using
valfor the flag. The compiler rejects the assignment in the loop, since avalcannot be reassigned. - Printing the flag inside the loop. That prints one line per character instead of one line in total.
Testing
| Password | Output |
|---|---|
Secret99 | Length: 8, Has digit: True, Strong |
abcdefgh | Length: 8, Has digit: False, Weak |
12345678 | Length: 8, Has digit: True, Strong |
short1 | Length: 6, Has digit: True, Weak |
