0% found this document useful (0 votes)
7 views

Lab4

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

Lab4

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

Exercise 1

Write a function to compute the below formualation with the use of map function
x2 + y2
− r2
then, print out the values of the equation at these below values of x, y and z
x = [1,2,3,4,5,6]
y = [2,3,4,5,6,7]
r = [1,1,3,3,4,5]

In [ ]:

x = [1,2,3,4,5,6]
y = [2,3,4,5,6,7]
r = [1,1,3,3,4,5]

In [ ]:
values= map(lambda x,y,r:x*x+y*y-r*r,x,y,r)
print(list(values))

[4, 12, 16, 32, 45, 60]

In [ ]:
def print_values(function,x,y,r):
for i in range(len(x)):
result=(function(x[i],y[i],r[i]))
print('The results of',x[i],'^2 +',y[i],'^2 -',r[i],'^2 =',result)

print_values(lambda x,y,r: x*x+y*y-r*r,x,y,r)

The results of 1 ^2 + 2 ^2 - 1 ^2 = 4
The results of 2 ^2 + 3 ^2 - 1 ^2 = 12
The results of 3 ^2 + 4 ^2 - 3 ^2 = 16
The results of 4 ^2 + 5 ^2 - 3 ^2 = 32
The results of 5 ^2 + 6 ^2 - 4 ^2 = 45
The results of 6 ^2 + 7 ^2 - 5 ^2 = 60

Exercise 2
Write a function that take input as a list of words and return to a list of integers representing the lengths of the
correponding words.

Write it in three different ways:

1. using a for-loop,
2. using function map() , and
3. using list comprehensions.

In [ ]:
#using a for-loop
def word_lengths_for_loop(words):
lengths = []
for word in words:
lengths.append(len(word))
return lengths

list1= ['meme','meomeo']
word_lengths_for_loop(list1)
Out[ ]:
[4, 6]

In [ ]:
#using function map()
def word_lengths_map(words):
return list(map(len, words))

list2=['hahaaa', 'ahihi','haizzzzzzz']
word_lengths_map(list2)
Out[ ]:
[6, 5, 10]

In [ ]:
#using list comprehensions
def word_lengths_list_comprehension(words):
return [len(word) for word in words]

list3=['ga','meo','huou']
word_lengths_map(list3)
Out[ ]:
[2, 3, 4]

Exercise 3
Implement a program to make a list whose elements are double string of a string re (i.e., rere ), where re is
the first two characters of approriate elements in this list:

['real', 'rock', 'realize', 'rocket', 'red', 'eraser' , '7', 8 , 9, 10] .

In [ ]:
def double_re(word):
word = str(word)
if word[0:2] == 're':
return word[0:2]+word
else:
return word

input_list = ['real', 'rock', 'realize', 'rocket', 'red', 'eraser', '7', 8, 9, 10]

result_list = list(map(double_re,input_list))
print( result_list)

['rereal', 'rock', 'rerealize', 'rocket', 'rered', 'eraser', '7', '8', '9', '10']

Exercise 4
Write a function max_in_list() that takes a list of numbers and returns the largest one using reduce() in
lib functools

In [ ]:
from functools import reduce

def max_in_list(lis):
return reduce(lambda a, b: a if a > b else b, lis)

lis=[3,24,5,47,689,7876,542]
print("The maximum element of the list is : ", max_in_list(lis))

The maximum element of the list is : 7876

Exercise 5
Using Python map to write a Python program to triple all numbers of a given list of integers.

In [ ]:
data=[2,3,4,5,6]
print(list(map(lambda num: num*3, data)))

[6, 9, 12, 15, 18]

Exercise 6
Implement parallel processing using map() to apply a function across multiple elements of an iterable
concurrently, and then use reduce() to aggregate the results.

In [2]:
import concurrent.futures
import functools
import operator

def square(n):
return n * n

numbers = [1, 2, 3, 4, 5]

with concurrent.futures.ProcessPoolExecutor() as executor:


results = executor.map(square, numbers)

total = functools.reduce(operator.add, list(results))

print(total)

55

Exercise 7
Implement a MapReduce-style function to process data distributed across multiple nodes or workers.

In [3]:
from multiprocessing import Pool
from collections import Counter
import operator

# The "map" function


def count_words(document):
return Counter(document.split())

# The "reduce" function


def combine_counts(dict1, dict2):
return dict1 + dict2

if __name__ == "__main__":
documents = [
"the quick brown fox jumped over the lazy dog",
"dog fox brown quick the",
"lazy dog jumped over the quick brown fox",
]

# Create a pool of workers


with Pool() as pool:
# Use the workers to count words in each document
count_lists = pool.map(count_words, documents)
# Use the workers to combine the counts
word_counts = functools.reduce(combine_counts, count_lists)

print(word_counts)

Counter({'the': 4, 'quick': 3, 'brown': 3, 'fox': 3, 'dog': 3, 'jumped': 2, 'over': 2, 'l


azy': 2})

You might also like