-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path342.py
More file actions
51 lines (34 loc) · 1.16 KB
/
342.py
File metadata and controls
51 lines (34 loc) · 1.16 KB
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
"""
Problem:
reduce (also known as fold) is a function that takes in an array, a combining function,
and an initial value and builds up a result by calling the combining function on each
element of the array, left to right. For example, we can write sum() in terms of
reduce:
def add(a, b):
return a + b
def sum(lst):
return reduce(lst, add, 0)
This should call add on the initial value with the first element of the array, and then
the result of that with the second element of the array, and so on until we reach the
end, when we return the sum of the array.
Implement your own version of reduce.
"""
from typing import Any, Callable, Iterable
def reduce(iterable: Iterable, func: Callable, initial_value: int) -> int:
value = initial_value
for item in iterable:
value = func(value, item)
return value
def add(a: int, b: int) -> int:
return a + b
def sum(lst: Iterable) -> int:
return reduce(lst, add, 0)
if __name__ == "__main__":
print(sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
[for the reduce function only (considering the iterable doesn't contain a nested
iterable)]
"""