Python Please include comments explaining and screenshot of
Python! Please include comments explaining and screenshot of the output, thank you!
Find the minimum of a list of numerical values by using reduce
Find the intersection of N given lists by using map and reduce
(you can use set to solve this problem)
example :[ [1, 2, 3, 4, 5], [2, 3, 4, 5,6], [3, 4, 5,6,7] -> [3,4,5]
Solution
=========Code=============
#lambda function saying that if you input two numbers return the smalller no
 f = lambda a,b: a if (a < b) else b
 #the way reduce works here is it keeps inputting two no at a time to reduce to final no
 #so for eg. here first lambda is run on 47 and 11 and it is reduced to 11
 #then 11 and 42 giving 11
 #then 11 and 102 giving 11
 #then 11 and 13 giving 11
 print reduce(f, [47,11,42,102,13])
#create three sets, reason for using sets here is you would not end up putting same element twice in your result
 set_list = [set([1, 2, 3, 4, 5]), set([2, 3, 4, 5,6]), set([3, 4, 5,6,7])]
 #it is operating on above set_list and takes one input from bot the sets and adds if it is present in both
 print reduce(lambda s1, s2: s1 & s2, set_list)
========Output=========
11
set([3, 4, 5])

