Problem overview
Some passwords are weak whatever they contain, because everyone tries them first. This version rejects those. You start with a set of well-known passwords, common, and the password to check:
let common: Set<String> = ["password", "123456", "qwerty", "letmein"]
let password = "qwerty"
The program prints the password, then whether it is in the set, then a verdict. A common password gets Weak: reject and any other gets Strong: accept:
Password: qwerty
Common: True
Weak: reject
For "Tr0ub4dor" the output is Password: Tr0ub4dor, Common: False and Strong: accept. Nothing in this version looks at length or character classes: the only question is whether the exact text is in the list.
Approach: a set as a lookup table
Checking membership in a plain array means comparing the password with every element until one matches, and the time grows with the size of the list. A real list of common passwords can hold millions of entries. A set is built for the question "is this value in here?": it answers in constant time on average, however many values it holds. That makes it the right container whenever you only need to ask whether something is present and don't care about order or positions.
The program asks the question once, stores the answer as a Bool and uses it twice: to print the Common line and to choose the verdict.
Step by step
- Print the password.
print("Password: \(password)")shows which password is being checked. - Look it up.
let isCommon = common.contains(password). Thecontainsmethod of a set returnstruewhen the set holds that text. - Print the
Commonline. Swift prints aBoolin lowercase, and the output needs a capital letter, soisCommon ? "True" : "False"inside the interpolation picks the text. - Write the
ifonisCommon. A common password is rejected whatever else is true of it, so its body printsWeak: reject. - Add the
else. The}that closes theifalso opens theelse, and its body printsStrong: accept.
The solution in Swift
let common: Set<String> = ["password", "123456", "qwerty", "letmein"]
let password = "qwerty"
print("Password: \(password)")
let isCommon = common.contains(password)
print("Common: \(isCommon ? "True" : "False")")
if isCommon {
print("Weak: reject")
} else {
print("Strong: accept")
}
Swift details here:
- The type annotation matters. A bare array literal in
["password", ...]would make anArray. Writing: Set<String>tells Swift to build a set from the same literal. Without the annotation,commonwould be an array, and an array'scontainswould still compile and work, just with a linear search. containsreturns aBool. It gives a plaintrueorfalse, and so needs no optional handling. That differs from a dictionary lookup, which returns an optional.- A
Boolcan be used directly as a condition.if isCommon {is enough. Writingif isCommon == trueis legal and means the same, but the shorter form is idiomatic. - The ternary inside the interpolation.
\(isCommon ? "True" : "False")places a conditional expression inside a string literal. The inner quotes are allowed in Swift 5. - Sets are unordered. Printing a whole set can list the values in any order, and the order can change between runs. This program never prints the set, only asks whether a value is in it.
Edge cases and common mistakes
- Case matters.
"Qwerty"isn't in the set, so it printsCommon: FalseandStrong: accept. Matching ignoring case would take a further step, such as lowercasing the password before the lookup, which this version doesn't ask for. - The empty string. It isn't in the set, so the output is
Password:, thenCommon: FalseandStrong: accept. This version doesn't check the length. - Not being common doesn't mean strong. The verdict word
Strong: acceptonly says that the password is not on the list. The checks from earlier versions are separate rules. - Looping to search. A
forloop over the set that compares each element does the same job, with more code and a slower search for large sets.containssays it in one call. - Printing the
Boolas it is."Common: \(isCommon)"printstruein lowercase, which doesn't match the required text. - Typing the set as an array. If a learner leaves out the
Set<String>annotation, the code still compiles and prints the same text for small inputs, so the mistake only costs speed.
Testing
Change password in the editor. The outputs below are those of the reference solution:
"Tr0ub4dor"printsPassword: Tr0ub4dor,Common: FalseandStrong: accept."Qwerty"printsPassword: Qwerty,Common: FalseandStrong: accept.""printsPassword:,Common: FalseandStrong: accept.
