Question

In: Computer Science

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

python3

(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.

These functions could then
be called from the passwordChecker function. However, implementation details are
left to you.

reference code:

# 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()

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.

Solutions

Expert Solution

Please give the thumbs up, if it is helpful for you!!. Let me know if you have any doubts.

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 7 01:32:35 2018
@author:
"""
def checklen(astring, le):
'''
(str) -> Boolean
Returns True if length of astring is at least le characters long, else False
'''
return len(astring) >= le


def is_nonalnum(astring):

'''
(str) -> Boolean
Returns True if astring contains at least one non-alphanumeric character;
returns False otherwise.
'''
if len(astring) == 0:
return False
else:
return not(astring.isalnum())

def is_noEe(astring):

'''
(str) -> Boolean
Returns True if astring does NOT contain characters 'E' or 'e';
returns False otherwise.'''
  
lowere = 'e' in astring
uppere = 'E' in astring
return not(lowere or uppere)

def is_uc_alpha(astring):

'''
(str) -> Boolean
return True if any char in s is an uppercase letter,
otherwise return False'''
for c in astring:
if c.isupper():
return True
return False

def is_2numbers(astring):

'''
(str) -> Boolean
returns True if astring has at least two numbers, otherwise return False
'''
digits_ctr = 0
for c in astring:
if c.isdigit():
digits_ctr += 1
return digits_ctr >= 2

def is_special_char(astring):

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

for c in astring:
if c in special:
return True
return False

def frequent(psw):
'''
(str) -> Boolean
returns True if string psw contain in the list
['password', '12345', 'qwerty', 'letmein', 'trustno1','000000', 'passw0rd']
otherwise returns False'''
  
lst=['password', '12345', 'qwerty', 'letmein', 'trustno1','000000', 'passw0rd']
if psw in lst:
return False
else:
return True
  
def passwordChecker(string):
pass1=checklen(string,5)
pass2=is_nonalnum(string)
pass3=is_noEe(string)
pass4=is_uc_alpha(string)
pass5=is_2numbers(string)
pass6=is_special_char(string)
pass7=frequent(string)
  
if pass1==False:
return ("Password must be at least 5 characters long")
if pass2==False:
return ("Passwords must include at least one non-alphanumeric character.")
if pass3==False:
return ("Passwords may not contain the characters E or e")
if pass4==False:
return ("Passwords must contain at least one upper case letter")
if pass5==False:
return ("Passwords must contain at least two numbers")
if pass6==False:
return ("password must contains at least special character from [!@#$%^&]")
if pass7==False:
return ('''A password may not be a frequently used password: 'password', '12345',
'qwerty', 'letmein', 'trustno1', '000000', 'passw0rd''')
  
if pass1==True and pass2==True and pass3==True and pass4==True and pass5==True \
and pass6==True and pass7==True:
return ("\npassword meets the criteria.!!")
else:
return ("\npassword meets the criteria.!!")
  
def main():
string=input('Enter the password: ')
res=passwordChecker(string)
print(res)
  
main()

Output:


Related Solutions

Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
Write A function with an input parameter which is a string arithmetic statement which contains only...
Write A function with an input parameter which is a string arithmetic statement which contains only alphabet variables and binary operations including +, -, *, / and % and checks whether the statement is a valid arithmetic statement or not. If the statement is valid, the function returns true, otherwise returns false. • The statement might contain parenthesis as well. For instance: • a+b*a+c/c%y • (a+b)*(a/d-(a/b)) • You can make this assumption that the variable names contain only one alphabet...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
(Python3) Write a function friend_besties() that calculates the "besties" (i.e. degree-one friends) of a given individual...
(Python3) Write a function friend_besties() that calculates the "besties" (i.e. degree-one friends) of a given individual in a social network. The function takes two arguments: individual, an individual in the social network, in the form of a string ID. bestie_dict, a dictionary of sets of friends of each individual in the social network (as per the first question of the Project) The function should return a sorted list, made up of all "degree-one" friends for the individual. In the instance...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT