Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Probably too much annotation solution in Clear category for Acceptable Password V by BootzenKatzen
# Taken from mission Acceptable Password IV
# 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 "password" in password.lower(): # .lower() will make any capitals lowercase so we can find "password"
pass # no matter what case it is. Pass will move to "return" keeping key False
elif len(password) > 9: # if the length is > 9 we only need 1 condition
key = True
elif 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
#I had modified a previous version of their solution before, and I'm gonna do it again for this one:
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
cond4 = "password" not in password.lower() # pretty much the same explanation as above
if len(password) > 9: # if the password is > 9 we only need one condition
return cond4 # so we check if "password" is in our password
else: # otherwise
return all([cond1, cond2, cond3, cond4])
# The all function will only return True if everything in the list is True
# so if the password is <9 long, it will check all the conditions
# This is their solution:
def is_acceptable_password3(password: str) -> bool:
cond1 = len(password) > 6 # This is pretty similar to the solution above
cond2 = any(char.isdigit() for char in password)
cond3 = any(char.isalpha() for char in password)
if len(password) > 9: # cond4 # This is the only difference. Instead of having if/else
cond2 = cond3 = True # They just make the other conditions true if the password >9
cond5 = "password" not in password.lower() # So essentially those 2 conditions are effectively "ignored"
# When the length is > 9
return all([cond1, cond2, cond3, cond5]) # Checking if all the conditions are true
# including our newly added "password" not in password.lower()
# Bonus solution 1:
# This is actually fairly similar to my first solution
def is_acceptable_password4(password: str) -> bool:
# They're checking through the conditions and immediately
# returning False if they're not True
if not len(password) > 6: # If the password is not at least 6 characters long
return False # immediately returns false
if not len(password) > 9: # if the password is not > 9 we check the next two conditions
# Otherwise we skip to the next "if" about "password" in password
if not any(x.isdigit() for x in password): # if there are no digits in the password
return False # imediately return false
if not any(x.isalpha() for x in password): # if there are no letters in password
return False # return false
if 'password' in password.lower(): # if "password" is in our password at all (even len > 9)
return False # return false
# if none of those conditions triggered a false
return True # return True
#Bonus Solution 2:
import re # Oh boy. Regular expressions. My favorite. *Heavy sarcasm*
def is_acceptable_password5(password: str) -> bool:
return bool(
(len(password) > 6 and re.search('\d', password) and re.search('[a-z]', password)
or len(password) > 9)
and (password.lower().find('password')) == -1)
# Oh. Actually this one's not to bad with the regex
# so bool() will just return True or False for whatever is in the parenthesis.
# First in the parentheses is the big batch of conditions in one set () OR password > 9
# If one of those is true, either it meets all the conditions in the () or it's >9 in length
# It will return true
# The len(password) functions are pretty easy to understand
# so our first new thing is re.search('\d', password)
# re is regular expressions, which comes with a number of funcitons including search
# which works like search(what you're looking for, where you're looking)
# so we're looking for '\d' in the password and in regex, \d is any digit 0-9
# so this is how we're checking for at least one number
# so re.search('[a-z]', password) is looking for any lowercase letter
# (I kind of think this is an oversight because what if their password is "200CATS" it would trigger False
# despite it having >6 characters, letters and numbers)
# Anyway, so you're looking for ((all 3 conditions) or len>9) as one group AND
# (password.lower().find('password')) == -1)
# if it finds "password" in the lowercase password, it would return the index where it found it
# which would be anything >= 0
# if it can't find it, it returns -1
# so if it does return -1 we get True for that condition
# So either we get all 3 conditions and no "password" in our password
# or >9 and no "password" in our password
# and if one of those two sets of conditions has true for both parts
# bool() will return True for our password
print("Example:")
print(is_acceptable_password("short"))
# These "asserts" are used for self-checking
assert is_acceptable_password("short") == False
assert is_acceptable_password("short54") == True
assert is_acceptable_password("muchlonger") == True
assert is_acceptable_password("ashort") == False
assert is_acceptable_password("muchlonger5") == True
assert is_acceptable_password("sh5") == False
assert is_acceptable_password("1234567") == False
assert is_acceptable_password("12345678910") == True
assert is_acceptable_password("password12345") == False
assert is_acceptable_password("PASSWORD12345") == False
assert is_acceptable_password("pass1234word") == True
print("The mission is done! Click 'Check Solution' to earn rewards!")
May 12, 2023
Comments: