Check a Password Against a Supplied Policy
Problem statement
Return whether password satisfies the policy supplied by minimumLength, requireLowercase, requireUppercase and requireDigit.
For this exercise, assume the password contains only printable ASCII characters, with codes 32 through 126. Its length must be at least minimumLength. Each enabled class flag also requires at least one matching character: lowercase a through z, uppercase A through Z, or digit 0 through 9. Disabled class flags impose no requirement.
For this exercise, assume other printable characters, including spaces and punctuation, are allowed and count toward length; they satisfy none of those three classes. Return true exactly when every enabled requirement passes. An empty password passes only when the minimum is zero and all class flags are false. Do not modify the input.
Function
checkPassword(password: String, minimumLength: int, requireLowercase: boolean, requireUppercase: boolean, requireDigit: boolean) → booleanExamples
Example 1
password = "aA0"minimumLength = 3requireLowercase = truerequireUppercase = truerequireDigit = truereturn = trueAuthored example: The length is three and the password contains a lowercase letter, an uppercase letter and a digit.
Example 2
password = "hello"minimumLength = 5requireLowercase = truerequireUppercase = truerequireDigit = falsereturn = falseAuthored example: The length and lowercase check pass, but the enabled uppercase requirement fails.
Example 3
password = ""minimumLength = 0requireLowercase = falserequireUppercase = falserequireDigit = falsereturn = trueAuthored example: An empty string meets the zero minimum and has no enabled class checks.
Constraints
- For this exercise, assume
0 <= password.length <= 500. - For this exercise, assume
0 <= minimumLength <= 500. - Every password character is printable ASCII, from code
32through126. - The three class parameters are Boolean flags.