1
2
3
def cascade(n): if n < 10:
print(n) else:
print(n)
cascade(n//10)
print(n)
cascade(123)
4
def cascade(n): if n < 10:
print(n) else:
print(n)
cascade(n//10)
print(n)
cascade(123)
4
def cascade(n): if n < 10:
print(n) else:
print(n)
cascade(n//10)
print(n)
cascade(123)
Global frame
cascade → func cascade(n)[parent=Global]
f1: cascade[parent=Global] n 123
Return value None
f2: cascade[parent=Global] n 12
Return value None
f3: cascade[parent=Global] n1
Return value None
Print output:
123 12 1 12 123
5
def cascade(n): if n < 10:
print(n) else:
print(n)
cascade(n//10)
print(n)
def cascade(n): print(n)
if n >= 10: cascade(n//10)
print(n)
6
1 12 123 12 1
7
def inverse_cascade(n): grow(n)
print(n)
shrink(n)
grow = lambda n: f_then_g( shrink = lambda n: f_then_g(
)
)
8
def inverse_cascade(n): grow(n)
print(n)
shrink(n)
def f_then_g(f, g, n): if n:
f(n) g(n)
grow = lambda n: f_then_g( shrink = lambda n: f_then_g(
)
)
8
def inverse_cascade(n): grow(n)
print(n)
shrink(n)
def f_then_g(f, g, n): if n:
f(n) g(n)
grow = lambda n: f_then_g(grow, print, n//10) shrink = lambda n: f_then_g( )
8
def inverse_cascade(n): grow(n)
print(n)
shrink(n)
def f_then_g(f, g, n): if n:
f(n) g(n)
grow = lambda n: f_then_g(grow, print, n//10) shrink = lambda n: f_then_g(print, shrink, n//10)
8
9
10
def virfib(n):
“””Compute the nth Virahanka-Fibonacci number, for N >= 1. >>> virfib(2)
1
>>> virfib(6)
8
“””
11
def virfib(n):
“””Compute the nth Virahanka-Fibonacci number, for N >= 1. >>> virfib(2)
1
>>> virfib(6)
8
“””
if n == 0:
return 0 elif n == 1: return 1
else:
return virfib(n-2) + virfib(n-1)
11
< Prev > Next
12
⇑⇑
⇑⇑⇑⇑
⇑⇑⇑⇑⇑⇑
⇑⇑
13
14
n mn
m
count_partitions(6, 4)
15
n mn
count_partitions(6, 4)
m
16
n mn
m
count_partitions(6, 4)
16
n mn
count_partitions(6, 4)
m
16
n mn
count_partitions(6, 4)
m
16
n mn
m
count_partitions(6, 4) count_partitions(2, 4)
count_partitions(6, 3)
17
n mn
m
count_partitions(6, 4)
count_partitions(2, 4)
count_partitions(n-m, m)
count_partitions(6, 3)
17
n mn
m
count_partitions(6, 4)
count_partitions(2, 4)
count_partitions(n-m, m)
count_partitions(6, 3)
count_partitions(n, m-1)
17
n mn
m
count_partitions(6, 4)
m
count_partitions(2, 4)
count_partitions(n-m, m)
m
count_partitions(6, 3)
count_partitions(n, m-1)
def count_partitions(n, m): “””
>>> count_partitions(6, 4)
9 “””
18
n mn
m
count_partitions(6, 4)
m
count_partitions(2, 4)
count_partitions(n-m, m)
m
count_partitions(6, 3)
count_partitions(n, m-1)
def count_partitions(n, m): “””
>>> count_partitions(6, 4)
9 “””
else:
with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m
18
n mn
m
count_partitions(6, 4)
m
count_partitions(2, 4)
count_partitions(n-m, m)
m
count_partitions(6, 3)
count_partitions(n, m-1)
def count_partitions(n, m): “””
>>> count_partitions(6, 4)
9
“””
if n == 0:
return 1 elif n < 0:
return 0 elif m == 0: return 0
else:
with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m
18
< Prev > Next
19