Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions Challenges/MyShinyChain/Solutions/PeterBPythonSolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#
# Our PM ask us to create a function for password validation
# Some of the validation rules are the following:
# - Password cannot be less than 8 characters
# - Password cannot be greater than 20 characters
# - Password must have, at least, one capital letter
# - Password must have, at least, one number
# - Password must have, at least, one special character from this list !@#$%^&*
# The following is our first solution
#

def nomax_is_password_valid(password):
if 8 <= len(password):
# Checking for capital letter
one_capital = False
for letter in password:
if letter.isupper():
one_capital = True
break

if one_capital == False:
return False

# Checking for numbers
one_number = False
for letter in password:
if letter.isdigit():
one_number = True
break

if one_number == False:
return False

# Checking for special characters
one_special_character = False
special_characters = "!@#$%^&*"
for letter in password:
for special_character in special_characters:
if letter == special_character:
one_special_character = True
break

if one_special_character == True:
break

if one_special_character == False:
return False

return True
return False


def is_password_valid(password, maxlengthenabled = True):
if not maxlengthenabled:
return nomax_is_password_valid(password)
elif 8 <= len(password) <= 20:
# Checking for capital letter
one_capital = False
for letter in password:
if letter.isupper():
one_capital = True
break

if one_capital == False:
return False

# Checking for numbers
one_number = False
for letter in password:
if letter.isdigit():
one_number = True
break

if one_number == False:
return False

# Checking for special characters
one_special_character = False
special_characters = "!@#$%^&*"
for letter in password:
for special_character in special_characters:
if letter == special_character:
one_special_character = True
break

if one_special_character == True:
break

if one_special_character == False:
return False

return True
return False


if __name__ == '__main__':
print(is_password_valid('Abcdefa4!'))
print(is_password_valid('Abcdefa4'))
print(is_password_valid('Abcdefa4abcdefghijklmnopqrstuvwxyz'))
print(is_password_valid('Abcdefaa!'))
print(is_password_valid('abcdefa4!'))
print(is_password_valid('Abcdefa4a'))
# unittest.main()


#
# The PM now is asking us to reuse the password validator function
# in other part of the solution, but without applying max length restriction
# additionally, we will need to include another rule that must apply only
# for this call, but that functionality will be provided later.
#
# HINT: There is a design pattern called "chain of responsibility" that could
# fit perfectly for this case. Try to apply it.
#