#==================================================
# Lecture 22: Thinking Recursively
#--------------------------------------------------
# Every function from the slides, in slide order.

#--------------------------------------------------
# Slides 8-9: Add the Digits (n // 10 makes the job smaller)

def digit_sum(n):
    if n < 10:              # W: one digit
        return n
    rest = n // 10          # I: drop the last digit
    last = n % 10
    return digit_sum(rest) + last   # S, then E: add the last digit

print(digit_sum(472))   # 13
print(digit_sum(47))    # 11

#--------------------------------------------------
# Slides 10-12: Reverse a string (slide 11: small examples)

def reverse(s):
    if len(s) <= 1:                 # W: "" or one letter
        return s
    return reverse(s[1:]) + s[0]    # I, S, E

print(reverse("hello"))   # olleh
print(reverse("cat"))     # tac
for word in ["o", "lo", "llo", "ello", "hello"]:
    print(word, reverse(word))   # o o, lo ol, llo oll, ello olle, hello olleh

#--------------------------------------------------
# Slides 14-16: Palindrome, without s[::-1] (Lecture 9 used the slice)

def is_palindrome(s):
    if len(s) <= 1:
        return True
    if s[0] != s[-1]:
        return False
    return is_palindrome(s[1:-1])

print(is_palindrome("racecar"))   # True
print(is_palindrome("hello"))     # False
print(is_palindrome("abba"))      # True

#--------------------------------------------------
# Slide 17: Read & Explain!

def mystery(s):
    if s == "":
        return 0
    rest = mystery(s[1:])
    if s[0] in "aeiou":
        return 1 + rest
    return rest

print(mystery("banana"))   # 3: it counts the vowels

#--------------------------------------------------
# Slides 18-19: Find Max

def find_max(L):
    if len(L) == 1:
        return L[0]
    best_of_rest = find_max(L[1:])
    if L[0] > best_of_rest:
        return L[0]
    return best_of_rest

print(find_max([3, 7, 2, 9, 1]))   # 9
print(find_max([3, 7, 2]))         # 7

#--------------------------------------------------
# Slide 20: Many Ways to Make a Job Smaller (sum_to is Lecture 21's)

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

print(sum_to(5), sum_to(4))                                # 15 10
print(digit_sum(472), digit_sum(47))                       # 13 11
print(reverse("cat"), reverse("at"))                       # tac ta
print(is_palindrome("abba"), is_palindrome("bb"))          # True True
print(find_max([3, 7, 2]), find_max([7, 2]))               # 7 7

#--------------------------------------------------
# Slides 22-24: The Towers of Brahma

def hanoi(n, start, spare, goal):
    if n == 0:                          # no disks: nothing to do
        return
    hanoi(n - 1, start, goal, spare)    # top n-1 out of the way
    print("Move disk", n, "from", start, "to", goal)
    hanoi(n - 1, spare, start, goal)    # top n-1 back on top

hanoi(3, "A", "B", "C")
# Move disk 1 from A to C
# Move disk 2 from A to B
# Move disk 1 from C to B
# Move disk 3 from A to C
# Move disk 1 from B to A
# Move disk 2 from B to C
# Move disk 1 from A to C

#--------------------------------------------------
# Slides 25-27: moves(n) with WISE (You Try), and when the world ends

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

for n in [1, 2, 3, 4, 5, 6, 10, 20]:
    print(n, moves(n))
# 1 1, 2 3, 3 7, 4 15, 5 31, 6 63, 10 1023, 20 1048575

print(moves(64))                 # 18446744073709551615
seconds_per_year = 365.25 * 24 * 60 * 60
years = moves(64) / seconds_per_year
print(round(years / 1e9))        # 585 (billion years, one move a second)
print(round(years / 13.8e9))     # 42  (ages of the universe)

#--------------------------------------------------
# Slides 29-30: Lists Inside Lists

def nested_sum(L):
    if L == []:
        return 0
    first = L[0]
    rest = nested_sum(L[1:])
    if type(first) == list:
        return nested_sum(first) + rest
    return first + rest

print(nested_sum([1, [2, 3], [4, [5, 6]]]))   # 21
print(nested_sum([1, [2, [3]]]))              # 6

#--------------------------------------------------
# Slides 33-34: You Try, count the digits

def count_digits(n):
    if n < 10:
        return 1
    rest = n // 10
    return count_digits(rest) + 1

print(count_digits(4096))   # 4
print(count_digits(7))      # 1

#--------------------------------------------------
# Slides 35-36: You Try, stutter

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

print(stutter("cat"))   # ccaatt

#--------------------------------------------------
# Slides 37-38: You Try, is the list sorted?

def is_sorted(L):
    if len(L) <= 1:
        return True
    if L[0] > L[1]:
        return False
    return is_sorted(L[1:])

print(is_sorted([2, 5, 5, 9]))   # True
print(is_sorted([2, 7, 4, 9]))   # False

#--------------------------------------------------
# Slide 44: Exit Ticket #22

def total(L):
    if L == []:
        return 0
    return L[0] + total(L[1:])

# print(moves(5))           # 31
# print(total([4, 5, 6]))   # 15
