← Back to challenges

Strong Password Checker

PythonHardlogicnumbershigher_order_functionsfunctional_programming

Instructions

A password is considered strong if all the following conditions are met:

  1. It has at least 8 characters and at most 20 characters.
  2. It contains at least one lowercase letter, one uppercase letter and one digit.
  3. It must NOT contain three repeating characters in a row (e.g. "...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met).

Write a function that takes a string txt and return the MINIMUM change required to make it a strong password. If it's already strong, return 0.

Examples

password_checker("Mypass!") ➞ 1
# 7 characters total, need to add one more digit for a strong password.
# 1 minimum change.

password_checker("mypass1!") ➞ 1
# 8 characters total, need to add an uppercase letter.
# 1 minimum change.

password_checker("ABCDEFGHIJKLMNOPQRSTU") ➞ 3
# 21 characters total, only uppercase letters, need to delete one
# character and replace two characters, 1 with a digit, 1 with a
# lowercase letter.
# 3 minimum changes.

password_checker("Mypaaaass!1") ➞ 1
# Contains more than 3 repeating characters in a row - "aaaa", need
# to replace an "a" with a different character (e.g. "a3aa" or in some
# cases add a character in the middle "aa2aa".
# 1 minimum change.

password_checker("000aaaBBBccccDDDDeeeee") ➞ 6
# 22 characters, and 6 sets of repeating characters.
# 2 characters need to be removed, and one change is needed in each set.
# removing one 0 and one a, and replacing one of the middle characters
# in each of the four BBB,cccc,DDDD,eeeee blocks does the trick
# 6 minimum changes.

Notes

  • Insertion, deletion or replacement of any one character is considered one change.
  • Spaces will be ignored for this challange.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.