Problem overview
This version turns the password rules into a score. Starting from 0, the program adds points for each property the password has:
- 25 points for a length of 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 any character that is not a letter or a digit.
The five amounts add up to 100. The program prints the length, then the score. For Secret!99:
Length: 9
Score: 100
For abc123 the output is Length: 6 and Score: 40: the lowercase letters give 20 and the digits give 20, and the length is under 8.
Approach: collect the facts, then add up the points
The work splits into two stages. The first stage is a single loop over the password that sets four flags: hasUpper, hasLower, hasDigit and hasSymbol. The second stage turns facts into points: one if per rule, each adding its own amount to score.
Keeping the stages apart makes each easy to follow. The loop knows nothing about points, and the scoring knows nothing about characters. The rules use independent if statements, never else if, because a password can earn several awards at once.
The weights are chosen so that a password with every property reaches exactly 100, and no more. That is why no extra code is needed to cap the score: each rule can fire at most once, so the highest possible total is 25 + 20 + 20 + 20 + 15. Each property is counted once however many characters of that kind there are. A password with ten digits earns the same 20 points as one with a single digit.
The earlier versions printed each fact and a verdict. This one reduces everything to one number, which is easy to compare between passwords but hides which rule was missed. The Length: line is printed to give some context for the score.
Step by step
- Store the length.
length := len(password)gives the number of bytes in the string, which is the number of characters for ASCII text. It is used for the score and for the first output line. - Create four flags, all
false:hasUpper,hasLower,hasDigit,hasSymbol. - Loop with
for _, c := range password. The blank identifier discards the position, andcis each character. - Set the flags. Three range tests handle
'A'to'Z','a'to'z'and'0'to'9'. The fourth test is true whencis in none of the three ranges. - Create
scoreand set it to 0, before any rule is checked. - Award the length points.
if length >= 8runsscore += 25. The operator+=adds to the variable in place. - Award the class points. One
ifeach forhasUpper,hasLowerandhasDigitadds 20, andhasSymboladds 15. - Print the result. The
Length:line and theScore:line each join a label to a number converted withstrconv.Itoa, because Go does not join anintto a string with+.
The solution in Go
password := "Secret!99"
length := len(password)
hasUpper := false
hasLower := false
hasDigit := false
hasSymbol := false
for _, c := range password {
if c >= 'A' && c <= 'Z' {
hasUpper = true
}
if c >= 'a' && c <= 'z' {
hasLower = true
}
if c >= '0' && c <= '9' {
hasDigit = true
}
if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
hasSymbol = true
}
}
score := 0
if length >= 8 {
score += 25
}
if hasUpper {
score += 20
}
if hasLower {
score += 20
}
if hasDigit {
score += 20
}
if hasSymbol {
score += 15
}
fmt.Println("Length: " + strconv.Itoa(length))
fmt.Println("Score: " + strconv.Itoa(score))
Go details worth knowing here:
lencounts bytes. For ASCII passwords that equals the number of characters. For a string with accented or non-Latin letters,lenis larger, which affects the length rule. The string"héllo"has 5 characters andlengives 6.rangeyields runes.cis arune, so the character literals'A','z'and'9'compare with it directly. A character outside the ASCII ranges, such asé, passes the "none of the three" test and counts as a symbol.- Compound assignment.
score += 25meansscore = score + 25. - Conditions on booleans.
if hasUpperneeds no== true. - Order of statements. The scoring
ifstatements must come after the loop. Before it, the flags would still befalse.
Edge cases and common mistakes
- Using
else iffor the rules. Only the first matching rule would score.Secret!99would get 25 instead of 100. - An empty password. The loop does not run, no rule matches, and the output is
Length: 0andScore: 0. - Short but varied.
Aa1!has all four classes but only 4 characters: 20 + 20 + 20 + 15 is 75, and the length adds nothing. - Long but one class.
abcdefghscores 45: 25 for the length and 20 for lowercase letters.!!!!!!!!scores 40: 25 plus 15. - Printing before scoring. The output lines come after the points are added, so they show the final values.
- Misreading the points. The class points differ: a symbol is worth 15 and the others 20. Using 20 for all of them would give 105 as the maximum, past the 100 the lesson aims for.
Testing
Secret!99printsLength: 9andScore: 100.abc123printsLength: 6andScore: 40.abcdefghprintsLength: 8andScore: 45.!!!!!!!!printsLength: 8andScore: 40.Aa1!printsLength: 4andScore: 75.- An empty string prints
Length: 0andScore: 0.
