Recursion · 18 / 18 Exercise Hard

Ways to climb stairs

Objective

A staircase has n steps and you can climb 1, 2 or 3 steps at a time. In how many different ways can you climb it? It is a recursion problem with memoization, because the version without memory repeats a huge number of calculations.

Instructions

  • Read n (between 0 and 35).
  • Define ways(n): if n is negative there are no ways (0); if it is 0 there is one (1); otherwise it is the sum of ways(n - 1), ways(n - 2) and ways(n - 3).
  • Add memoization (with @lru_cache or with a dictionary) so that it computes fast.
  • Show Ways: w.
Input
4
Output
Ways: 7

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise