Check for the standard password in Python using Sets
In this post also we will check whether the password is in standard format or not in Python. But unlike the previous post, this time we will use sets and their in-built functions. This will prove to be very handy for those who are unaware of sets and the in-built functions we have for them.
This will also prove to be very handy for the aspirants of competitive programming as sets prove to be a great asset in cp.
Introduction to Sets
Let’s quickly know sets in a brief, so that we can start with our task. Set is one of the four collection data-types in Python. It is unordered and unindexed, unlike lists and tuples, and cannot store duplicate data in a set. Sets are written with curly brackets.
Basic Set Operations:
Converting set to other collections and vice-versa in Python
We will understand it with the help of a few examples;
>>> a= [ 1,4,2,2,4,5,6,8,43,2,1] >>> b= set(a) >>> b {1, 2, 4, 5, 6, 8, 43} >>> c = list(b) >>> print(c) [1, 2, 4, 5, 6, 8, 43] >>> string = "abcdcb" >>> set_string = set(string) >>> print(set_string) {'a', 'd', 'c', 'b'}
Python: Check whether the password is in standard format or not
A password will be standard only if;
- The length of the password lies in the range of 8 to 16 characters.
- Consists of at least one special character [[email protected]#$%^&*()-+].
- Consists of at least one lowercase alphabet and one uppercase alphabet.
We will start with defining 4 sets, one for each.
upper_set = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ") lower_set = set("abcdefghijklmnopqrstuvwxyz") digit_set = {"0123456789"} symbol_set = set("[email protected]#$%^&*()-+")
We have converted the strings into sets such that the elements of the set are the characters present in the string. Now we will take input and convert it into the set.
password = input() pass_set = set(password)
Now we have the password as the set of characters and we have already defined the rest of the sets for the operation.
To check that the password entered satisfies all the conditions we may use a nested if-conditional statement and a flag to store the status of the password.
flag = 0 if bool(upper_set.intersection(pass_set)): if bool(lower_set.intersection(pass_set)): if bool(digit_set.intersection(pass_set)): if bool(symbol_set.intersection(pass_set)): flag = 1
In the end, we will check the flag for validation.
upper_set = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ") lower_set = set("abcdefghijklmnopqrstuvwxyz") digit_set = {"1234567890"} symbol_set = set("[email protected]#$%^&*()-+") password = input() pass_set = set(password) flag = 0 if bool(upper_set.intersection(pass_set)): if bool(lower_set.intersection(pass_set)): if bool(digit_set.intersection(pass_set)): if bool(symbol_set.intersection(pass_set)): flag = 1 if flag: print("Valid") else: print("Invalid")
OUTPUT
123asdFG! Valid 123asA! Invalid [email protected]#asd2 Valid [email protected] Valid [email protected] Invalid hello [email protected] Invalid
Also, see
Leave a Reply