def cascade(n): """Draw a cascading tree. >>> cascade(486) 486 48 4 48 486 >>> cascade(48) 48 4 48 >>> cascade(4) 4 """ if n < 10: print(n) else: print(n) cascade(n // 10) print(n) def cascade_alt(n): """Draw a cascading tree. Alternate short implementation. >>> cascade_alt(486) 486 48 4 48 486 >>> cascade_alt(48) 48 4 48 >>> cascade_alt(4) 4 """ print(n) if n >= 10: cascade_alt(n // 10) print(n) def fib(n): """Return the nth Fibonacci number. >>> fib(0) 0 >>> fib(1) 1 >>> fib(6) 8 >>> fib(30) 832040 """ if n == 0: return 0 elif n == 1: return 1 else: return fib(n - 2) + fib(n - 1) def broken_fib(n): """Broken version of fib which is missing a base case and throws a RecursionError. >>> broken_fib(5) Traceback (most recent call last): ... RecursionError: maximum recursion depth exceeded in comparison """ if n == 0: return 0 else: return broken_fib(n - 2) + broken_fib(n - 1) def fib_iter(n): """Return the nth Fibonacci number. Iterative. >>> fib_iter(0) 0 >>> fib_iter(1) 1 >>> fib_iter(6) 8 >>> fib_iter(30) 832040 """ i = 0 curr, next = 0, 1 while i < n: curr, next = next, curr + next i += 1 return curr def count_part(n, m): """Count the number of ways to give out n pieces of chocolate if nobody can have more than m pieces. Both n and m are positive. >>> count_part(6, 4) 9 """ if n == 0: return 1 if n < 0: return 0 elif m == 0: return 0 else: with_m = count_part(n - m, m) wo_m = count_part(n, m - 1) return with_m + wo_m