DEV Community

ABUL HASAN A
ABUL HASAN A

Posted on

TASK 9

1) [10, 501, 22, 37, 100, 999, 87, 351]
2)# Sample list containing a mix of integers and strings
my_list = [10, 'hello', 5, 'world', 8]

Lambda function to check if an element is an integer or string

check_type = lambda x: isinstance(x, (int, str))

Apply the lambda function to each element of the list

result = list(map(check_type, my_list))

Print the result

print(result)
3)from functools import reduce

Define a lambda function to generate Fibonacci series

fib_series = lambda n: reduce(lambda x, _: x + [x[-1] + x[-2]], range(n - 2), [0, 1])

Generate Fibonacci series of 50 elements

result = fib_series(50)

Print the result

print(result)
4) a) import re

def validate_email(email):
"""
Validate an email address using a regular expression.

Args:
email (str): The email address to be validated.

Returns:
bool: True if the email address is valid, False otherwise.
"""

Regular expression for email validation

regex = r'^[\w.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$'

Match the email address against the regular expression

if re.match(regex, email):
return True
else:
return False

Enter fullscreen mode Exit fullscreen mode




Example usage:

email1 = "example@email.com"
email2 = "invalid_email.com"

print(validate_email(email1)) # Output: True
print(validate_email(email2)) # Output: False
b) import re

def validate_usa_mobile_number(number):
"""
Validate a mobile number of the USA using a regular expression.

Args:
number (str): The mobile number to be validated.

Returns:
bool: True if the mobile number is valid, False otherwise.
"""

Regular expression for USA mobile number validation

regex = r'^+?1?\s*[-]?\s*(?[2-9]{1}[0-9]{2})?[-]?\s*[2-9]{1}[0-9]{2}[-]?\s*[0-9]{4}$'

Match the mobile number against the regular expression

if re.match(regex, number):
return True
else:
return False

Enter fullscreen mode Exit fullscreen mode




Example usage:

number1 = "+1 123-456-7890"
number2 = "123-456-7890"
number3 = "1234567890"
number4 = "+1 (123) 456-7890"
number5 = "123-456-789" # Invalid number

print(validate_usa_mobile_number(number1)) # Output: True
print(validate_usa_mobile_number(number2)) # Output: True
print(validate_usa_mobile_number(number3)) # Output: True
print(validate_usa_mobile_number(number4)) # Output: True
print(validate_usa_mobile_number(number5)) # Output: False
c) import re

def validate_usa_telephone_number(number):
"""
Validate a telephone number of the USA using a regular expression.

Args:
number (str): The telephone number to be validated.

Returns:
bool: True if the telephone number is valid, False otherwise.
"""

Regular expression for USA telephone number validation

regex = r'^+?1?\s*[-]?\s*(?[2-9]{1}[0-9]{2})?[-]?\s*[2-9]{1}[0-9]{2}[-]?\s*[0-9]{4}$'

Match the telephone number against the regular expression

if re.match(regex, number):
return True
else:
return False

Enter fullscreen mode Exit fullscreen mode




Example usage:

number1 = "+1 123-456-7890"
number2 = "123-456-7890"
number3 = "1234567890"
number4 = "+1 (123) 456-7890"
number5 = "123-456-789" # Invalid number

print(validate_usa_telephone_number(number1)) # Output: True
print(validate_usa_telephone_number(number2)) # Output: True
print(validate_usa_telephone_number(number3)) # Output: True
print(validate_usa_telephone_number(number4)) # Output: True
print(validate_usa_telephone_number(number5)) # Output: False
d) import re

def validate_password(password):
"""
Validate a password consisting of 16 characters with at least one upper case letter,
one lower case letter, one special character, and one number.

Args:
password (str): The password to be validated.

Returns:
bool: True if the password is valid, False otherwise.
"""

Regular expression for password validation

regex = r'^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[@$!%?&])[A-Za-z\d@$!%?&]{16}$'

Match the password against the regular expression

if re.match(regex, password):
return True
else:
return False

Enter fullscreen mode Exit fullscreen mode




Example usage:

password1 = "Password@1234"
password2 = "Weakpassword123"
password3 = "StrongPass!5678"

print(validate_password(password1)) # Output: False
print(validate_password(password2)) # Output: False
print(validate_password(password3)) # Output: True

Top comments (0)