Problem overview
Some passwords are weak no matter how they look, because everyone tries them first. This lesson keeps a small collection of well-known passwords, common, and checks a candidate against it. The program prints three lines: the password, whether it is common, and a verdict.
The collection is a HashSet<string> holding "password", "123456", "qwerty" and "letmein". For the password "qwerty" the output is:
Password: qwerty
Common: True
Weak: reject
For "Tr0ub4dor", which is not in the set, the lines are Password: Tr0ub4dor, Common: False and Strong: accept.
Approach: ask a set whether it holds a value
The question is "is this exact text one of the known ones?". A HashSet answers it directly with Contains. A set stores each value once and finds a value by its hash code instead of walking through all of them, so a lookup takes about the same time whether the set holds four passwords or four million. A List<string> would also have a Contains method, but it compares against the items one after another, so the work grows with the size of the list.
The answer is a bool, which the program stores once and uses twice: in the second output line and in the verdict. The verdict is a plain if/else on that variable. A common password is rejected, whatever else is true of it.
Step by step
- Print the password line. The label
Password:is joined topasswordwith+. - Look it up.
common.Contains(password)returnstruewhen the set holds that exact text. Store it in aboolnamedisCommon. - Print the second line. The ternary
isCommon ? "True" : "False"picks the wording, and parentheses keep it from being swallowed by the+. - Write the verdict. An
if (isCommon)printsWeak: reject, and theelseprintsStrong: accept.
The solution in C#
HashSet<string> common = new HashSet<string> { "password", "123456", "qwerty", "letmein" };
string password = "qwerty";
Console.WriteLine("Password: " + password);
bool isCommon = common.Contains(password);
Console.WriteLine("Common: " + (isCommon ? "True" : "False"));
if (isCommon) {
Console.WriteLine("Weak: reject");
} else {
Console.WriteLine("Strong: accept");
}
The C# details worth a closer look:
- The generic type.
HashSet<string>is a set whose items are strings. It lives in theSystem.Collections.Genericnamespace, likeList<T>andDictionary<K, V>. - The braces after the constructor.
new HashSet<string> { "password", ... }is a collection initializer: C# calls the set'sAddmethod once for each item. Repeating a value is harmless, because a set keeps one copy.Addreturnsfalsewhen the value was already there. - The set has no order and no indexing. There is no
common[0]. A set is for membership questions, andContainsis the main one. - Text equality. A
HashSet<string>compares strings by their content, so a different string object with the same characters is found. - A
boolprints asTruein C#."Common: " + isCommonwould print the same words as the lesson's ternary. The ternary makes the text explicit.
Matching is exact
By default the set compares strings exactly, including case and spaces. With this set, "QWERTY", "Qwerty" and "qwerty " (with a trailing space) are all not found, so each of them would be accepted. To make the lookup ignore case, build the set with a comparer: new HashSet<string>(common, StringComparer.OrdinalIgnoreCase). With that set, Contains("QWERTY") is true. The lesson uses the default comparison, so only an exact match is rejected.
Edge cases and common mistakes
- Not in the set.
"Tr0ub4dor"givesCommon: FalseandStrong: accept. The wordStronghere only means "not on the list". The earlier length and character rules aren't part of this lesson's verdict. - Different case.
"QWERTY"is not found, as described above. - The empty string.
""is not in the set, so it is accepted.Containsnever fails on it. - A
nullpassword.Contains(null)is allowed on aHashSet<string>and returnsfalse, because the set does not holdnull. - Forgetting to store the result. Calling
common.Contains(password)twice works, but a namedboolis clearer and does the lookup once. - Reversing the verdict.
Weak: rejectbelongs to thetruebranch. A password found in the set is the bad one.
Testing
"qwerty"givesPassword: qwerty,Common: True,Weak: reject."Tr0ub4dor"givesPassword: Tr0ub4dor,Common: False,Strong: accept."letmein"givesCommon: TrueandWeak: reject."QWERTY"givesCommon: FalseandStrong: accept.
