PythiloLesson 27 of 32 · progress kept in this browser

Remember answers with memoization

Counting the ways to climb n stairs taking one or two steps repeats the same subproblems many times. Store each answer the first time it is computed and the exponential tree collapses to a straight line.

from functools import cache

@cache
def ways(n):
    if n <= 1:
        return 1
    return ways(n - 1) + ways(n - 2)
Your task

Write climb_ways(n) returning the number of ways to climb n stairs with steps of 1 or 2. climb_ways(0) is 1. It must handle n = 5000 quickly.