-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce.py
More file actions
47 lines (32 loc) · 692 Bytes
/
Copy pathreduce.py
File metadata and controls
47 lines (32 loc) · 692 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#gives a single value
from functools import reduce
def add(a,b):
return a+b
nums= [1,2,3,4,5,6]
result= reduce(add,nums)
print(result)
def multiply(a,b):
return a*b
nums= [1,2,3,4,5,6]
result= reduce(multiply, nums)
print(result)
def join_words(a,b):
return a+ " " + b
words= ["hello", "world", "from", "python"]
result= reduce(join_words, words)
print(result)
#max number
def get_max(a,b):
if a>b:
return a
return b
numbers= [25,8,35,90,25,3,-2]
result= reduce(get_max, numbers)
print(result)
def get_longest(a,b):
if len(a)>len(b):
return a
return b
words= ["hi", "yo", "man", "ok"]
result= reduce(get_longest, words)
print(result)