Lab4
Lab4
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))
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)
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.
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:
In [ ]:
def double_re(word):
word = str(word)
if word[0:2] == 're':
return word[0:2]+word
else:
return word
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))
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)))
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]
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
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",
]
print(word_counts)