-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathreduce_with_two.py
56 lines (32 loc) · 1.15 KB
/
reduce_with_two.py
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
48
49
50
51
52
53
54
55
56
"""
Examples of using `reduce()` with two arguments.
"""
from functools import reduce
# Sum elements in an iterable.
def add_two_values(x, y):
return x + y
print(reduce(add_two_values, [1, 2, 3, 4, 5]))
print(sum([1, 2, 3, 4, 5]))
# Concatenate strings.
print(reduce(add_two_values, ["cat", "dog", "hedgehog", "gecko"]))
print("".join(["cat", "dog", "hedgehog", "gecko"]))
# Calculate the factorial with `reduce()`.
def multiply(x, y):
return x * y
def factorial_with_reduce(n):
return reduce(multiply, range(1, n + 1))
print(factorial_with_reduce(4)) # 1 * 2 * 3 * 4
print(factorial_with_reduce(6)) # 1 * 2 * 3 * 4 * 5 * 6
# Calculate the factorial with `math.factorial()`.
from math import factorial # noqa E402
print(factorial(4))
print(factorial(6))
# Identify the maximum value.
print(max([23, 49, 6, 32]))
def greater(x, y):
return x if x > y else y
print(reduce(greater, [23, 49, 6, 32]))
# Use lambda expressions to complete the tasks.
print(reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]))
print(reduce(lambda x, y: x + y, ["cat", "dog", "hedgehog", "gecko"]))
print(reduce((lambda x, y: x if x > y else y), [23, 49, 6, 32]))