Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Annotated for Understanding "Acceptable Password III" solution in Clear category for Acceptable Password III by BootzenKatzen
# My solution (I definitely did this the weird way):
def is_acceptable_password(password: str) -> bool:
key = False # Creating a variable to store our bool in - false by default
if len(password) > 6: #checking the length of the password
try: # using try/except to check if the whole password is a number
int(password) # if we can turn the password into an integer, it will skip the except
# and the key will stay false
except: # if we can't turn it into an integer we go to our except
for i in range(len(password)): # we iterate through each character of the password
if password[i].isdigit(): # and if the current character is a digit (if there is at least one digit)
key = True # our bool becomes true
return key # returns our bool
#Their solution:
def is_acceptable_password2(password: str) -> bool:
cond1 = len(password) > 6 # if the password is > 6 characters this will return as True
cond2 = any(map(str.isdigit, password)) # map is used to apply a function to all iterables and store it
cond3 = any(map(str.isalpha, password)) # so we're mapping isdigit or isalpha to every character in password
# so for "short" isdigit we'd get (false, false, false, false, false)
# but isalpha would give us (True, True, True, True True)
# any checks the mapped list for ANY True values
# if there is even one true, cond2 or cond3 returns as True
return all([cond1, cond2, cond3]) # The all function will only return True if everything in the list is True
# Bonus solution 1:
is_acceptable_password3 = lambda p: 0 < sum(c.isdigit() for c in p) < len(p) > 6
# Okay, let's break this down from right to left (because that's how my brain does it)
# First off, our first condition is right there at the end len(p) > 6
# We want our password to be longer than 6 characters, pretty straightforward
# Next we have c.isdigit() for c in p, which is iterating through the password
# and checking if the character is a digit, because we want at least one
# sum is counting all the times isdigit comes back True, because in python True = 1 and False = 0
# so now we have our count of digits so we have 0 < sum of digits < len(p) >6
# So we want the count of digits to be greater than 0 but less than the length of the password (not all digits)
# so if all of these conditions are True, it will return True, otherwise we get False
# Bonus solution 2:
# Let's say you needed varying levels of security, like a passcode for a door, an easy password for a shared computer
# and then a password for your personal account
# You can use your code for the first security level, and add to it for your second level, and 2nd to 3rd
def is_acceptable_passwordI(x: str) -> bool: # So our passcode we only need it to be longer than 6
return len(x) > 6 # So we check if it's longer than 6
def is_acceptable_passwordII(x: str) -> bool: # Our shared computer needs > 6 AND have a number in it
return is_acceptable_passwordI(x) and any([c in '0123456789' for c in x])
# so we use is_acceptable_passwordI to check if it's > 6 and add to it
# and tells it we need both statements to be true
# any is looking for at least one True value
# and c in '0123456789' for c in x is iterating through the password looking for a number
def is_acceptable_passwordIII(x: str) -> bool:
return is_acceptable_passwordII(x) and not all([c in '0123456789' for c in x])
# our personal account needs all 3 conditions
# so we use is_acceptable_passwordII to check for the first two conditions
# then use and to add another condition
# all is going to check for all True values, and return False if even one is False
# so we're checking to see if all the characters are numbers
# if it returns True, then the "not" before it will return false becasue we do NOT want all numbers
# if it returns False then they're not all numbers, and it fulfills the condition as True
print("Example:")
print(is_acceptable_password("short"))
# These "asserts" are used for self-checking
assert is_acceptable_password("short") == False
assert is_acceptable_password("muchlonger") == False
assert is_acceptable_password("ashort") == False
assert is_acceptable_password("muchlonger5") == True
assert is_acceptable_password("sh5") == False
assert is_acceptable_password("1234567") == False
print("The mission is done! Click 'Check Solution' to earn rewards!")
May 5, 2023
Comments: