Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(3a) Write a function, frequent, with one parameter, psw, a string. If psw is in

ID: 3723862 • Letter: #

Question

(3a) Write a function, frequent, with one parameter, psw, a string. If psw is in a list of frequently used passwords ['password', '12345', 'qwerty', 'letmein', 'trustno1', '000000', 'passw0rd'], frequent should return False; otherwise, return True. Be sure to include at least three good test cases in the docstring.

(3b) Password Protection SecuriCorp has recently been the victim of a number of security breaches. Internal analysis has determined that employees use simple passwords that are too easy to guess. You have been hired to write a password checking program. This program should contain a function passwordChecker which takes a password and returns one of the following security regulation codes:

Passwords must be at least 5 characters long

Passwords must contain at least one upper case letter

Passwords must contain at least two numbers

Passwords may not contain the characters "E" or "e"

Passwords must include at least one non-alphanumeric character.

A password may not be a frequently used password: 'password', '12345', 'qwerty', 'letmein', 'trustno1', '000000', 'passw0rd'

Consultants suggest writing a separate function to test a password against each of these conditions. (Hint: reuse code from Project_WK6_key. I have included the edited version of Project_WK6_key_Edited on Canvas) These functions could then be called from the passwordChecker function. However, implementation details are left to you. Write the code (passwordChecker function and auxiliary functions) and execute it for a sufficient number of test cases that SecuriCorp will be confident of their passwords.

PROJECT 6-

import doctest

# problem 1

# problem 1a

def checklen(astring, le):
'''
(str) -> Boolean

Returns True if length of astring is at
least le characters long, else False

>>> checklen('', 1)
False
>>> checklen('four', 5)
False
>>> checklen('check', 5)
True
>>> checklen('check6', 6)
True
'''
return len(astring) >= le


# problem 1b
# empty string needs to be treated
# as a separate condition

def is_nonalnum(astring):
'''
(str) -> Boolean

Returns True if astring contains at
least one non-alphanumeric character;
returns False otherwise.

>>> is_nonalnum('')
False
>>> is_nonalnum('abc123')
False
>>> is_nonalnum('#123')
True
'''
if len(astring) == 0:
return False
else:
return not(astring.isalnum())
# problem 1c
# if vs. elif - either is ok here
# return True must be outside of for-block!


def is_noEe(astring):
'''
(str) -> Boolean

Returns True if astring does NOT
contain characters 'E' or 'e';
returns False otherwise.

>>> is_noEe('')
True
>>> is_noEe('e')
False
>>> is_noEe('CHEM 101')
False
>>> is_noEe('abcd')
True
'''
if 'E' in astring:
return False
elif 'e' in astring:
return False
else:
return True
# prolbe 1c
# different algorithm/same solution
"""
def is_noEe(astring):
'''
(str) -> Boolean

Returns True if astring does NOT
contain characters 'E' or 'e';
returns False otherwise.

>>> is_noEe('')
True
>>> is_noEe('e')
False
>>> is_noEe('CHEM 101')
False
>>> is_noEe('abcd')
True
'''
lowere = 'e' in astring
uppere = 'E' in astring
return not(lowere or uppere)

"""
# problem 1d
def is_uc_alpha(astring):
'''
(str) -> Boolean

return True if any char in s is an
uppercase letter, otherwise return False

>>> is_uc_alpha('CIS122')
True
>>> is_uc_alpha('Ducks')
True
>>> is_uc_alpha('testing')
False
'''
for c in astring:
if c.isupper():
return True

return False

# problem 1e

def is_2numbers(astring):
'''
(str) -> Boolean
  
returns True if astring has at least two numbers,
otherwise return False

>>> is_2numbers('CIS122')
True
>>> is_2numbers('Ducks')
False
>>> is_2numbers('ABC-1')
False
'''

digits_ctr = 0
  
for c in astring:
if c.isdigit():
digits_ctr += 1

return digits_ctr >= 2

# problme 1f

def is_special_char(astring):
'''
(str) -> Boolean
  
returns True if string contains a
special character:!, @, #, $, %, ^, &
otherwise returns False

>>> is_special_char('CIS122')
False
>>> is_special_char('CIS-122')
False
>>> is_special_char('CIS122!')
True
'''
special = '!@#$%^&'
for c in astring:
if c in special:
return True

return False
doctest.testmod()

Explanation / Answer

from sys import exit

def check_upper(input):
uppers = 0
upper_list = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split()
for char in input:
if char in upper_list:
uppers += 1
if uppers > 0:
return True
else:
return False

def check_lower(input):
lowers = 0
lower_list = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split()
for char in input:
if char in lower_list:
lowers += 1
if lowers > 0:
return True
else:
return False

def check_number(input):
numbers = 0
number_list = "1 2 3 4 5 6 7 8 9 0".split()
for char in input:
if char in number_list:
numbers += 1
if numbers > 0:
return True
else:
return False

def check_special(input):
specials = 0
special_list = "! @ $ % ^ & * ( ) _ - + = { } [ ] | , . > < / ? ~ ` " ' : ;".split()
for char in input:
if char in special_list:
specials += 1
if specials > 0:
return True
else:
return False

def check_len(input):
if len(input) >= 8:
return True
else:
return False


def validate_password(input):
check_dict = {
'upper': check_upper(input),
'lower': check_lower(input),
'number': check_number(input),
'special': check_special(input),
'len' : check_len(input)
}
if check_upper(input) & check_lower(input) & check_number(input) & check_special(input) & check_len(input):
return True
else:
print "Invalid password! Review below and change your password accordingly!"
print
if check_dict['upper'] == False:
print "Password needs at least one upper-case character."
if check_dict['lower'] == False:
print "Password needs at least one lower-case character."
if check_dict['number'] == False:
print "Password needs at least one number."
if check_dict['special'] == False:
print "Password needs at least one special character."
if check_dict['len'] == False:
print "Password needs to be at least 8 characters in length."
print   

while True:
password = raw_input("Enter desired password: ")
print
if validate_password(password):
print "Password meets all requirements and may be used."
print
print "Exiting program..."
print
exit(0)