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): ifnis negative there are no ways (0); if it is 0 there is one (1); otherwise it is the sum ofways(n - 1),ways(n - 2)andways(n - 3). - Add memoization (with
@lru_cacheor with a dictionary) so that it computes fast. - Show
Ways: w.
4
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