In: Computer Science
Task 1:
Some websites impose certain rules for passwords. Write a program that ask the user to enter a password and checks whether a string
(Use input validation to make sure the user enters the correct password and then print it.)
is a valid password. Suppose the password rules are as follows:
• A password must have list a least 10 characters.
• A password must consist of only letters and digits.
• A password must contain at least two digits.
• A password must have at least one uppercase letter.
• A password must have one of the following characters: #, $, &, @, &, %
Example of valid passwords:
Col11egeOfDupag3$
CIS2531Course@cod
Pyth0n&1CODclass
Example of invalid passwords:
myid#COD
CIS2531CollegeOfDupage
Abc123
Task 2:
Reads a string from the user containing a date in the form 03/12/2020 and the program will
display the date in the form March 12, 2020.
Please use comments to explain your work so I can understand it, Thanks
Task 1: Making use of regex will help us to achieve the required result.
reg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d.*\\d)(?=.*[@$%#&])[A-Za-z\d@$#%&]{10,}$"
^ represents start of string
?=.*[a-z] represents the positive lookahead with zero or more occurrence of a - z
?=.*[A-Z] represents the positive lookahead with zero or more occurrence of A - Z i.e at least one uppercase letter
?=.*\\d.*\\d represents the positive lookahead with atleast 2 digits
?=.*[@$%#&] represents the positive lookahead with zero or more occurrence of the special characters @,$,%,#,&
[A-Za-z\d@$#%&]{10,} represents the password should contain at least 10 characters of these
$ represents the end of the string.
def validate_password(password):
#this regex will validate the password meeting the requirements
#^ denotes the start, $ denotes the end, ?= positive lookahead, * zero or more occurences,\\d\\d the digits for atleast 2 times and {10,} password atleast 10 characters
reg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d.*\\d)(?=.*[@$%#&])[A-Za-z\d@$#%&]{10,}$"
pattern = re.compile(reg) #compile the regex
match = re.match(pattern, password) #if there is a match, it returns matched re object
# validating conditions
if match:
return True #return True if match
else:
return False #else return False
#password=input("Please enter password")
#print(validate_password(password))
print(validate_password("Col11egeOfDupag3$")) #validate the password
Output: True
Task 2: Reads a string from the user containing a date in the form 03/12/2020 and the program will
display the date in the form March 12, 2020.
date_string="12/3/2020" #input string
date_string=date_string.split("/") #split the string into list making use of delimiter '/'
dt = datetime(year=int(date_string[2]), month=int(date_string[1]), day=int(date_string[0])) #convert string to datetime object. since the list is of type str, convert them into int
dt = datetime.strftime(dt,'%B %d, %Y') #convert the datetime object to this format. %B gives us full month name, %d gives day and %Y gives year
print(dt)
Output:
March 12, 2020
I am also attaching the output and code screenshots.
Output and code screenshot:
#Please don't forget to upvote if you find the solution
helpful. Feel free to ask doubts if any, in the comments section.
Thank you.