Problem overview
This version turns the earlier checks into one number. The program starts with val password = "Secret!99" and awards points for the length and for each class of character:
- 25 points if the length is at least 8;
- 20 points for an uppercase letter;
- 20 points for a lowercase letter;
- 20 points for a digit;
- 15 points for a symbol, meaning a character that is neither a letter nor a digit.
The five amounts add up to exactly 100. The program prints the length and the score:
Length: 9
Score: 100
For abc123, which is 6 characters long and has lowercase letters and digits only, the score is 40:
Length: 6
Score: 40
Approach: find the facts, then add the points
The work splits into two phases. In the first, one loop over the characters sets four boolean flags, as in the earlier version with character classes. In the second, a series of independent if statements adds points to a score variable, one rule per statement.
Each rule must be its own if, not part of an else if chain. A chain stops at the first match, so a password with an uppercase letter would never be checked for the other classes. The rules are not alternatives; they are additive, and every rule that applies contributes its points.
Keeping the two phases apart makes the scoring easy to read and to change. To adjust what a digit is worth, you change one number in one line, without touching the loop.
Here is how the second example gets its 40. abc123 has 6 characters, so the length rule adds nothing. It has no uppercase letter and no symbol, so those rules add nothing either. It has lowercase letters, worth 20, and digits, worth 20. The total is 40. The program's output for it, shown above, is the same.
Step by step
- Store
lengthfrompassword.length. It is used for the length rule and for the first output line. - Create the four flags
hasUpper,hasLower,hasDigitandhasSymbol, each avarstarting atfalse. - Loop over the characters with
for (c in password). - Set the flags. Four
ifstatements testc.isUpperCase(),c.isLowerCase(),c.isDigit()and!c.isLetterOrDigit(). Each sets its own flag totrue, and none ever sets one back. - Create
scoreas avarat 0. - Add the length points. If
length >= 8,score += 25. - Add the class points. One
ifeach forhasUpper,hasLowerandhasDigitadds 20, and one forhasSymboladds 15. - Print the two lines, the length first, then the score.
The solution in Kotlin
val password = "Secret!99"
val length = password.length
var hasUpper = false
var hasLower = false
var hasDigit = false
var hasSymbol = false
for (c in password) {
if (c.isUpperCase()) {
hasUpper = true
}
if (c.isLowerCase()) {
hasLower = true
}
if (c.isDigit()) {
hasDigit = true
}
if (!c.isLetterOrDigit()) {
hasSymbol = true
}
}
var score = 0
if (length >= 8) {
score += 25
}
if (hasUpper) {
score += 20
}
if (hasLower) {
score += 20
}
if (hasDigit) {
score += 20
}
if (hasSymbol) {
score += 15
}
println("Length: $length")
println("Score: $score")
Kotlin details worth a closer look:
+=.score += 25meansscore = score + 25. It needs avar.- Testing a boolean directly.
if (hasUpper)is enough; writingif (hasUpper == true)says the same thing in more words. - Templates.
"Length: $length"inserts the value of the variable. A template reads up to the end of the name, so"Score: $score"printsScore: 100and no stray characters come along. - Flags and counters are different tools. The flags only record that a class is present. Scoring needs just that, not how many characters there were, so a password with ten digits earns the same 20 points as one with a single digit.
Intarithmetic. The largest possible score is 100, so there is no overflow concern, and the sum is exact.
A score is a different kind of answer from the earlier verdicts. A verdict is yes or no, while a score can say how far a password is from the top. That also means the program must define what the numbers mean: here the maximum, 100, is reached only when every rule passes.
Complexity
The loop visits each character once, and the scoring does a fixed amount of work. Time is proportional to the password length, and the space is a few variables.
Edge cases and common mistakes
else ifinstead of separateifs. The score would stop growing after the first rule that matched.- Counting a class twice. Adding points inside the character loop would award 20 points for every uppercase letter, not once. The flags exist to avoid that.
- Printing the score before it is final. The output comes after all the rules have run.
- Short but varied passwords. A password can lose the 25 length points and still score from its classes.
Testing
The empty password has no length points and no classes, so it scores 0:
Length: 0
Score: 0
The 4-character password Aa1! has all four classes but fails the length rule, so it gets 20 + 20 + 20 + 15:
Length: 4
Score: 75
These two cases show the extremes of the scale and the effect of the length rule on its own.
