Assignment 8 Utkarsh
Assignment 8 Utkarsh
Question 1
Question 1: You are writing code for a company. The requirement of the
company is that you create a python function that will check wether
password entered by user is correct or not. The function should take
password as input and return the string "Valid Password" if password
follows below given password guidelines else it should return "Invalid
Password"
Note :
1. The Password should contain atleast 2 uppercase letters and 2 lowercase letters.
2. The Password should contain atleast a number and 3 special characters.
3. The length of password should be 10 characters long.
Approach 1:
1. Define a function to count upper case letters and lower case letters , numbers and special characters
2. Create a final function call the counting function inside final function and other conditions to give final
results
In [1]:
# This function counts upper and lowercase alphabets in function
def count_characters(password):
"""
This function counts upper and lowercase in string and
returns the number of upper and lowercase characters
"""
# Intitalize count of each character type to zero
upper_count = 0
lower_count = 0
number_count = 0
symbol_count = 0
In [2]:
In [3]:
# Create a final function which validates the password
def validate_password(password):
"""
This function validates the password based on rules provided
"""
if isValid==True:
return "Valid Password"
else:
return "Invalid Password"
In [4]:
# Test Case 1
p1 = 'P$W@Skil!1'
validate_password(p1)
Out[4]:
'Valid Password'
In [5]:
# Test Case 2
p2 = 'Utkarsh1231200#'
validate_password(p2)
Out[5]:
'Invalid Password'
In [6]:
# Test case 3
p3 = 'UtkArS3!@$'
validate_password(p3)
Out[6]:
'Valid Password'
Approach 2 : Using regex library and invert the conditions to return invalid password
In [7]:
import re
def validate_password_alt(password):
"""
Approach 2 function uses regular expressions library
and use inverted conditions to return invalid password
if any one condition fails password is invalid
"""
if len(password) != 10:
return "Invalid Password"
In [8]:
# Test Case 1 , Approach 2
p1 = 'P$W@Skil!1'
validate_password_alt(p1)
Out[8]:
'Valid Password'
In [9]:
# Test Case 2, Approach 2
p2 = 'Utkarsh1231200#'
validate_password_alt(p2)
Out[9]:
'Invalid Password'
In [10]:
# Test Case 3, Approach 2
# Test case 3
p3 = 'UtkArS3!@{'
validate_password_alt(p3)
Out[10]:
'Valid Password'
Question 2
Answers :
Check if String starts with a particular letter
In [11]:
# Checking if string starts with letter H using lambda function
# Used lower function for case unification
string_starts_with_h = lambda s : s[0].lower()=='h'
# Examples
print(string_starts_with_h('Heart'))
print(string_starts_with_h('harting'))
print(string_starts_with_h('Utkarsh'))
True
True
False
In [12]:
# Checking wether string is numeric using 2 lambda functions
is_numeric = lambda q: q.replace('.','',1).isdigit()
is_numeric_including_negative = lambda r: is_numeric(r[1:]) if r[0]=='-' else is_numeric
(r)
print(is_numeric_including_negative('-123.34569'))
print(is_numeric_including_negative('asf1234'))
print(is_numeric_including_negative('1234-'))
print(is_numeric_including_negative('95'))
print(is_numeric_including_negative('-13'))
print(is_numeric_including_negative('123.123.34'))
True
False
False
True
True
False
Sort a list of tuples having fruit name and their quantity : [('mango',99),('orange',80),('grapes',1000)]
In [13]:
# using lambda function and inbuilt function sorted
fruits = [('mango',99),('orange',80),('grapes',1000)]
sorted_fruits = sorted(fruits,key = lambda x:x[1])
sorted_fruits
Out[13]:
In [14]:
# Using list comprehension
sqr = [i*i for i in range(1,11)]
sqr
Out[14]:
Out[14]:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In [15]:
# Using Map and Lambda function
cube_roots = map(lambda x : x**(1/3), range(1,11))
list(cube_roots)
Out[15]:
[1.0,
1.2599210498948732,
1.4422495703074083,
1.5874010519681994,
1.7099759466766968,
1.8171205928321397,
1.912931182772389,
2.0,
2.080083823051904,
2.154434690031884]
In [16]:
# Using Lambda function
is_even = lambda x : x%2==0
print(is_even(1))
print(is_even(2))
print(is_even(3))
print(is_even(4))
print(is_even(5))
print(is_even(7))
False
True
False
True
False
False
In [17]:
# Using filter and lambda function
lst = [1,2,3,4,5,6,7,8,9,10]
list(filter(lambda x: x%2 != 0,lst))
Out[17]:
[1, 3, 5, 7, 9]
Sort a list of integers into positive and negative integers list : [1,2,3,4,5,6,-1,-2,-3,-4,-5,0]
In [18]:
[1, 2, 3, 4, 5, 6]
[-5, -4, -3, -2, -1]
Note: 0 is not Shown in above example because it is neither positive , nor negative according to Mathematical
Definition