0% found this document useful (0 votes)
9 views4 pages

Assignment 7 Utkarsh

The document describes a Python assignment with two questions. Question 1 asks to create a function that takes a list as input and returns the product of all numeric values within the list and its nested structures. Question 2 asks to write a program to encrypt a message by reversing the alphabet and replacing spaces with '$'.

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)
9 views4 pages

Assignment 7 Utkarsh

The document describes a Python assignment with two questions. Question 1 asks to create a function that takes a list as input and returns the product of all numeric values within the list and its nested structures. Question 2 asks to write a program to encrypt a message by reversing the alphabet and replacing spaces with '$'.

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/ 4

Assignment 7: Python Task 1 - Utkarsh Gaikwad

Assignment pdf link

Question 1

Question 1: Create a function which will take a list as an argument and


return the product of all the numbers after creating a flat list.

Use the below-given list as an argument for your function.

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.

Answer : I have used below approach to solve this problem


1. Create a blank list l for initialization
2. if values are int and float then append the element
3. if values are list extend use l.extend to extend the list
4. if values are tuple and set , typecast into list and extend the list
5. if values are dictionary , append dict.keys and check dict.values into the list apply for loop for dictionary as
well
6. Now we have the list with all values from this we need to filter int and float from this list again for this we
can use filter function and lambda function to filter numeric values
7. Use reduce function to find product of all numeric values added

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

# Using for loop to iterate through


for i in lst:
# If type of element integer or float then append into list
if type(i)==int or type(i)==float:
l.append(i)

# If type of element list then extend the list l


elif type(i)==list:
l.extend(i)

# 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)

# Checking types of values and performing similar operations


if type(value)==int or type(value)==float:
l.append(value)
elif type(value)==list:
l.extend(value)
elif type(value)==tuple or type(value)==set:
l.extend(list(value))
# For block for dictionary completed

# Main For block is completed

# Print orignal list


print('Orignal List given : ',lst)
print('*****************************')

# Print list before filtering numeric values inside


print('\n Flat List before filtering numeric values inside nested structures : ',l)
print('*****************************')

# Apply Filter function and lambda to filter numeric values only


filter_l = list(filter(lambda x: type(x)==int or type(x)==float,l))
print('\n Flat List after filtering numeric values only : ',filter_l)
print('*****************************')

# Apply Reduce function to get product from the filtered list


product = reduce(lambda x,y : x*y, filter_l)

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.

Input Sentence: I want to become a Data Scientist.

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.

Answer : Below is my approach


1. Use string module ascii lowercase and create a dictionary encrypted_dict = {'a':'z','b':'y',... ,'z':'a'}
2. add encrypted_dict[' ']= Dollar Sign ( $)
3. from string module import punctuations and use for loop to add these into encrypted_dict
4. Add numbers and keep them same in the dictionary
5. Initialize a blank string encrypt_out
6. Use for loop and add encrypted with encrypted_dict[i]

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

# Step 1 : Creating dictionary {'a':'z','b':'y',...,'z':a}


ascii_alph = string.ascii_lowercase
encrypted_dict = dict(zip(ascii_alph, ascii_alph[::-1]))

# Step 2 : Add space key as $ in dictionary


encrypted_dict[' ']='$'

# Step 3 : Add punctuation marks as it is


for i in string.punctuation:
encrypted_dict[i]=i

# Step 4 : Adding Numbers and keeping them same:


for num in range(0,10):
encrypted_dict[str(num)]=str(num)

# Dictionary creation is completed

# Step 5 : Initialize blank string encrypt_out


encrypt_out = ''

# Step 6 : Apply for loop to input and use encrypted_dict


# Use lowercase for case unification
for char in strg.lower():
encrypt_out = encrypt_out + encrypted_dict[char]

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.'

You might also like