Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Answers Annotated for Password IV solution in Clear category for Acceptable Password IV by BootzenKatzen
# Taken from mission Acceptable Password III
# 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) > 9: # This is all I added this time 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 tried to modify their solution from password III to make it fit the conditions this time:
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
if len(password) > 9: # if the password is > 9 we don't need any other conditions
return True # so we return true
else: # otherwise
return all([cond1, cond2, cond3]) # The all function will only return True if everything in the list is True
# Here's how they solved it:
# It's pretty much the same as the one above in that they just added an if statement
# But they kept just 1 return statement instead
def is_acceptable_password3(password: str) -> bool:
cond1 = len(password) > 6
cond2 = any(char.isdigit() for char in password)
cond3 = any(char.isalpha() for char in password)
if len(password) > 9: # cond4
cond2 = cond3 = True # This is a clever way of solving this
# instead of a separate return statement they just make
return all([cond1, cond2, cond3]) # the other two conditions come back true
# So our return statement stays the same
# Bonus solution 1:
def is_acceptable_password4(x: str) -> bool:
return (len(x)>6 and not x.isdigit() and not x.isalpha()) or len(x)>9
#Breaking this down from left to right because that's how my brain works
# len(x) > 9 is pretty simple. If the password is > 9 characters we return true
# The or lets us return true if one of the two statements is true, so even if the password isn't > 9 characters
# We can check if it meets our other password conditions.
# Our other password conditions are all contained in () to mark them as a set of conditions
# That go on the other side of the or statement. This keeps it from reading it as
# a choice between not x.isalpha() or len(x) > 9
# It's a choice between the length being longer than 9 or all the other conditions put together
# so x.isalpha() is checking if our whole password is letters if there is even one digit, that returns False
# and fullfills our "not" condition, so reports true for that condition
# x.isdigit() does the same for numbers. If there is even one letter, it will return False
# and fulfill our "not" condition and return True for that condition
# and len(x)>6 is pretty straightforward
# So if our password is either:
# > 6 and not completely letters or numbers OR >9
# We will return True,
# Otherwise we return False.
#Bonus solution 2:
def is_acceptable_password5(x: str) -> bool:
return any((len(x) > 9, 0 < sum(ch.isdigit() for ch in x) < len(x) > 6))
# pretty much all the logic for the conditions is the same for this one
# the only difference is that instead of using OR we're using ANY
# we've used any on some of the previous solutions
# basically if any of the conditions inside the parenthesis return True,
# The whole statement returns True
# and our conditions inside the parentheses are the same as the or statement in the previous solution
# either len(password) > 9 OR ( all the other conditions as a group in parenthesis)
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("notshort") == 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
print("The mission is done! Click 'Check Solution' to earn rewards!")
May 11, 2023
Comments: