Assignment 7 Utkarsh
Assignment 7 Utkarsh
Question 1
list1 = [1,2,3,4, [44,55,66, True], False, (34,56,78,89,34), {1,2,3,3,2,1}, {1:34, "key2": [55, 67, 78,
89], 4: (45,22, 61, 34)}, [56, 'data science'], 'Machine Learning']
Note: you must extract numeric keys and values of the dictionary also.
In [1]:
from functools import reduce
def flat_and_product(lst):
"""
This function takes a list extracts all numeric values from it including dictionary k
eys , values
and returns the product of numeric values as result
"""
l = [] #Initialize Blank List
# If type of element tuple or sets then typecast and extend the list l
elif type(i)==tuple or type(i)==set:
l.extend(list(i))
# If type of element is dictionary then extend keys and values into the list l it
erate through key and value and extend them
elif type(i)==dict:
for key, value in i.items():
l.append(key)
return product
In [2]:
# Using the function to get product
list1 = [1,2,3,4, [44,55,66, True], False, (34,56,78,89,34), {1,2,3,3,2,1}, {1:34, "key
2": [55, 67, 78, 89], 4: (45,22, 61, 34)}, [56, 'data science'], 'Machine Learning']
flat_and_product(list1)
Orignal List given : [1, 2, 3, 4, [44, 55, 66, True], False, (34, 56, 78, 89, 34), {1, 2
, 3}, {1: 34, 'key2': [55, 67, 78, 89], 4: (45, 22, 61, 34)}, [56, 'data science'], 'Mach
ine Learning']
*****************************
Flat List before filtering numeric values inside nested structures : [1, 2, 3, 4, 44, 55
, 66, True, 34, 56, 78, 89, 34, 1, 2, 3, 1, 34, 'key2', 55, 67, 78, 89, 4, 45, 22, 61, 34,
56, 'data science']
*****************************
Flat List after filtering numeric values only : [1, 2, 3, 4, 44, 55, 66, 34, 56, 78, 89,
34, 1, 2, 3, 1, 34, 55, 67, 78, 89, 4, 45, 22, 61, 34, 56]
*****************************
Out[2]:
4134711838987085478833841242112000
Question 2
Question 2 : Write a python program for encrypting a message sent to
you by your friend. The logic of encryption should be such that, for a the
output should be z. For b, the output should be y. For c, the output
should be x respectively. Also, the whitespace should be replaced with a
dollar sign. Keep the punctuation marks unchanged.
Encrypt the above input sentence using the program you just created.
Note: Convert the given input sentence into lowercase before encrypting. The final output
should be lowercase.
In [5]:
# Creating a function for providing encrypted output
import string
def encrypted_message(strg):
"""
This function returns encrypted string as mentioned in the question 2
"""
return encrypt_out
In [7]:
In [7]:
# Test Case
test = 'Hello there I am Utkarsh Gaikwad and my age is 28, this is a test !!?@'
encrypted_message(test)
Out[7]:
'svool$gsviv$r$zn$fgpzihs$tzrpdzw$zmw$nb$ztv$rh$28,$gsrh$rh$z$gvhg$!!?@'
In [8]:
# Final Output
inp = "I want to become a Data Scientist."
encrypted_message(inp)
Out[8]:
'r$dzmg$gl$yvxlnv$z$wzgz$hxrvmgrhg.'