#==================================================
# Lecture 21: Recursion and the Call Stack
#--------------------------------------------------
# Every function from the slides, in slide order.
# The buggy ones are commented out: remove the # in
# front of a call to watch it fail.

import sys

#--------------------------------------------------
# Slide 12: First Example: Countdown

def countdown(n):
    if n <= 0:              # base case: stop
        print("Blastoff!")
        return
    print(n)                # recursive case:
    countdown(n - 1)        # the same job, one smaller

countdown(5)
# 5
# 4
# 3
# 2
# 1
# Blastoff!

#--------------------------------------------------
# Slides 15-18: Tracing sum_to (Going Down, Coming Back Up), Why n <= 0

def sum_to(n):
    if n <= 0:
        return 0
    return n + sum_to(n - 1)

print(sum_to(4))     # 10
print(sum_to(0))     # 0
print(sum_to(-5))    # 0   (n <= 0 catches odd inputs too)

#--------------------------------------------------
# Slide 19: You Try! (print AFTER the call)

def countup(n):
    if n == 0:
        return
    countup(n - 1)
    print(n)

countup(3)
# 1
# 2
# 3

#--------------------------------------------------
# Slides 22-23: No Base Case, The Pile Has a Limit

def infinite(n):
    print(n)
    infinite(n - 1)

print(sys.getrecursionlimit())     # 1000
# infinite(5)
# 5, 4, 3, ... -992, -993, then:
# RecursionError: maximum recursion depth exceeded

#--------------------------------------------------
# Slide 24: Bug Hunt! power  (fix: e - 1)

def power_buggy(b, e):
    if e == 0:
        return 1
    return b * power_buggy(b, e)

def power(b, e):
    if e == 0:
        return 1
    return b * power(b, e - 1)

# print(power_buggy(2, 3))    # RecursionError
print(power(2, 3))            # 8

#--------------------------------------------------
# Slide 25: Bug Hunt! count down in twos  (fix: n <= 0)

def twos_buggy(n):
    if n == 0:
        print("Blastoff!")
        return
    print(n)
    twos_buggy(n - 2)

def twos(n):
    if n <= 0:
        print("Blastoff!")
        return
    print(n)
    twos(n - 2)

# twos_buggy(7)     # 7 5 3 1 -1 -3 ... RecursionError
twos_buggy(6)       # 6 4 2 Blastoff!  (even numbers hide the bug)
twos(7)             # 7 5 3 1 Blastoff!

#--------------------------------------------------
# Slide 26: Bug Hunt! the missing return  (fix: return n + ...)

def sum_to_buggy(n):
    if n <= 0:
        return 0
    n + sum_to_buggy(n - 1)

# print(sum_to_buggy(3))
# TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
print(sum_to_buggy(1))   # None: the 1 + 0 is worked out, then thrown away

#--------------------------------------------------
# Slide 29: Factorial

def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)

print(factorial(5))   # 120
print(factorial(0))   # 1

#--------------------------------------------------
# Slide 30: Length of a String, Without len

def length(s):
    if s == "":
        return 0
    return 1 + length(s[1:])

print(length("hello"))   # 5
print(length(""))        # 0

#--------------------------------------------------
# Slide 31: You Try! multiply with only +

def multiply(a, b):
    if b == 0:
        return 0
    return a + multiply(a, b - 1)

print(multiply(3, 4))   # 12
print(multiply(7, 0))   # 0

#--------------------------------------------------
# Slide 34: You Try! echo (print before AND after the call)

def echo(n):
    if n == 0:
        print("base")
        return
    print("down", n)
    echo(n - 1)
    print("up", n)

echo(2)
# down 2
# down 1
# base
# up 1
# up 2
# At most 3 echo frames at once: n = 2, 1, 0

#--------------------------------------------------
# Slide 35: You Try! halvings  (fix: n <= 1)

def halvings_buggy(n):
    if n == 1:
        return 0
    return 1 + halvings_buggy(n // 2)

def halvings(n):
    if n <= 1:
        return 0
    return 1 + halvings(n // 2)

print(halvings_buggy(8))    # 3   (8 -> 4 -> 2 -> 1)
# print(halvings_buggy(0))  # RecursionError: 0 // 2 is 0, never 1
print(halvings(0))          # 0

#--------------------------------------------------
# Slide 36: You Try! countdown_list

def countdown_list(n):
    if n == 0:
        return []
    return [n] + countdown_list(n - 1)

print(countdown_list(3))   # [3, 2, 1]
print(countdown_list(0))   # []

#--------------------------------------------------
# Slide 42: Exit Ticket #21

def f(n):
    if n == 0:
        return 0
    return n * 2 + f(n - 1)

# print(f(3))   # 12
