0% found this document useful (0 votes)
18 views6 pages

Assignment 8 Utkarsh

The document discusses solving coding problems related to validating passwords and performing operations on lists using Python functions like lambda, filter, map and list comprehensions. It provides sample code to count characters in a password, validate the password rules, check if a string starts with a letter, check if a string is numeric, sort a list of tuples by quantity, find squares and cube roots of numbers, filter even/odd numbers from a list, and separate positive and negative numbers from a list.

Uploaded by

bubunkumar84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Assignment 8 Utkarsh

The document discusses solving coding problems related to validating passwords and performing operations on lists using Python functions like lambda, filter, map and list comprehensions. It provides sample code to count characters in a password, validate the password rules, check if a string starts with a letter, check if a string is numeric, sort a list of tuples by quantity, find squares and cube roots of numbers, filter even/odd numbers from a list, and separate positive and negative numbers from a list.

Uploaded by

bubunkumar84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 8: Python Task 2 - Utkarsh Gaikwad

Assignment pdf link

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.

Answer : I'm breaking this code down in multiple functions :

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

# Iterate through each character of password


for char in password:
# Check wether character is alphabet first
if char.isalpha()==True:
# Check wether character is upper case
if char.isupper()==True:
upper_count = upper_count + 1
else:
lower_count = lower_count + 1

# Check if character is numeric


elif char.isnumeric()==True:
number_count = number_count + 1

# Is character is neither alphabet and nor numeric it is special character


else:
symbol_count = symbol_count + 1

return upper_count, lower_count, number_count, symbol_count

In [2]:

# Testing above counting funtion


count_characters('UtkarsH12345,^(#)_12345')
Out[2]:
(2, 5, 10, 6)

In [3]:
# Create a final function which validates the password
def validate_password(password):

"""
This function validates the password based on rules provided
"""

# Call count_character function to calculate counts of various characters


upper_count, lower_count, number_count, symbol_count = count_characters(password)

# Create a boolean to check all conditions


isValid = (upper_count>=2) and (lower_count>=2) and (number_count>=1) and (symbol_co
unt>=3) and(len(password)==10)

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"

if sum(1 for c in password if c.isupper()) < 2 or \


sum(1 for c in password if c.islower()) < 2 or \
sum(1 for c in password if c.isdigit()) == 0 or \
len(re.findall("[!@#$%^&*()_+-=,.<>/?;':[\]{} |~]", password)) < 3:
return "Invalid Password"

return "Valid 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

Question 2: Solve the below questions using atleast 1 of the following


:
1. Lambda Function
2. Filter Function
3. Map Function
4. Comprehension function
4. Comprehension function

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

Check if string is numeric

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]:

[('orange', 80), ('mango', 99), ('grapes', 1000)]

Find squares of numbers from 1 to 10

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]

Find cube root of numbers from 1 to 10

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]

Check if given number is even

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

Filter odd numbers from given list : [1,2,3,4,5,6,7,8,9,10]

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]:

# Using list comprehension


# Please note that 0 is neither positive nor negative according to mathematical definitio
n
# Used sorted function to sort in ascending order
lst = [1,2,3,4,5,6,-1,-2,-3,-4,-5,0]
positive_list = sorted([i for i in lst if i>0])
negative_list = sorted([i for i in lst if i<0])
print(positive_list)
print(negative_list)

[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

You might also like