Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Finished password checker! solution in Clear category for Acceptable Password VI by MarHar
# Taken from mission Acceptable Password V
# Taken from mission Acceptable Password IV
# Taken from mission Acceptable Password III
# Taken from mission Acceptable Password II
# Taken from mission Acceptable Password I
def is_acceptable_password(password: str) -> bool:
# debugging line (prints password)
print ("\n" + password)
# initialize variables to keep track of which conditions have been verified
number = False
letter = False
three_chars = False # number of unique characters must be at least 3
# if the word "password" is included, return False
# convert to lowercase to make it easy to check
if "password" in password.lower():
return False
# list of unique characters present
uni_chars = []
# make sure there are at least 3 different characters present
# go through each character/index pair in password
for i,c in enumerate(password):
# if this is the first character, add it to the list
if i == 0:
uni_chars.append(c)
# if this is the last character, just check if it's new or not
elif i == len(password)-1 and c not in uni_chars:
uni_chars.append(c)
# if this character is the same as a previous one, skip it
elif c in uni_chars:
continue
# for each index greater than i
for j in range (i+1, len(password)):
# if any of the other letters does not match c, or it's the last letter
if j == len(password)-1 or password[j] != c:
# add to count and break inner for loop
uni_chars.append(password[j])
break
# if number of unique characters reaches 3, break outer for loop
if len(uni_chars) == 3:
three_chars = True
break
# if for loop finishes without finding 3 unique characters, three_chars will remain False
# debugging line (prints unique character list)
#print ("uni_chars: " + str(uni_chars))
# if password is longer than 9, go ahead and return True if it has three unique characters
if three_chars and len(password)>9:
return True
# if password is longer than 6
if len(password)>6:
# go through each character
for i in password:
# if one of them is a number, make a note of this
if i.isdecimal():
number = True
# if one of them is a number, make a note of this
if i.isalpha():
letter = True
# debugging lines (prints which conditions are satisfied)
print ("number\t\t" + str(number) + "\nletter\t\t"+ str(letter) + "\nthree_chars\t" + str(three_chars))
print("overall\t\t" + str(number and letter and three_chars))
# returns False unless all conditions are True
# note that number and letter return False if the password is too short regardless of content
return (number and letter and three_chars)
# code to test function
print (is_acceptable_password("newpassword"))
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
assert is_acceptable_password("aaaaaa1") == False
assert is_acceptable_password("aaaaaabbbbb") == False
assert is_acceptable_password("aaaaaabb1") == True
assert is_acceptable_password("abc1") == False
assert is_acceptable_password("abbcc12") == True
assert is_acceptable_password("aaaaaaabbaaaaaaaab") == False
print("The mission is done! Click 'Check Solution' to earn rewards!")
Nov. 8, 2022