Chapter 3 · Data

Recursion

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 20 · Concept

A function can call itself

In this lesson you write functions that call themselves, and watch their frames pile up.

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

A function that calls itself is recursion. Each call has a smaller n. The base case, n == 0, stops it: return alone ends the call at once.

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


countdown(3)
print("Go")
def sum_digits(n):
    # the base case first, then a smaller call
    return 0


print(sum_digits(7), sum_digits(123))
print(sum_digits(2026), sum_digits(99999))
import sys

sys.setrecursionlimit(50)


def countdown(n):
    countdown(n - 1)


countdown(3)
import sys

sys.setrecursionlimit(50)


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


countdown(3)
print("Go")
def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)


def fib_loop(n):
    a, b = 0, 1
    for i in range(n):
        a, b = b, a + b
    return a


print([fib(n) for n in range(10)])
print(fib_loop(9), fib_loop(80))
def nested_sum(items):
    total = 0
    for x in items:
        if isinstance(x, list):
            total += nested_sum(x)
        else:
            total += x
    return total


print(isinstance(5, list))
print(isinstance([5], list))
print(nested_sum([1, [2, 3], [4, [5]]]))
def flatten(items):
    # return one list of all the numbers in items
    return []


print(flatten([1, [2, 3], [4, [5]]]))
print(flatten([[[7]], 8]))
def power(b, e):
    return 0


def is_palindrome(s):
    return False


print(power(2, 10), power(3, 4))
print(is_palindrome("level"), is_palindrome("abca"))
print(is_palindrome("noon"), is_palindrome(""))
memo = {}


def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)


print(fib(15))
Terminal
Step 1 of 9A countdown

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)