PY
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
# can't use optional argument for 'initial'
# (iterable may itself contain the default
# value, even None may be in the list)
# we can't omit 'initial' either,
# and we can't put it as a positional arg
# (user may choose to omit it)
# so..
# *args
def reduce(*args):
if len(args)==2:
func,iterable=args
return reduce(
func,
iterable[1:],
iterable[0]
)
func,iterable,initial=args
# base case:
if len(iterable)==1:
return func(initial,iterable[0])
# recursion:
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run