Complete the code to initialize the base cases for climbing stairs.
def climb_stairs(n): if n == 1: return [1] if n == 2: return 2
When there is only 1 stair, there is exactly 1 way to climb it.
Complete the code to create a list to store the number of ways to climb each stair.
def climb_stairs(n): ways = [0] * (n + [1]) ways[1] = 1 ways[2] = 2
We add 1 to n to create a list that includes index n.
Fix the error in the loop to correctly calculate ways to climb stairs.
for i in range(3, n + 1): ways[i] = ways[i - [1]] + ways[i - 2]
We add ways from one step before and two steps before the current stair.
Fill both blanks to complete the function that returns the total ways to climb n stairs.
def climb_stairs(n): if n <= 2: return n ways = [0] * (n + 1) ways[1] = 1 ways[2] = 2 for i in range(3, n + 1): ways[i] = ways[i - [1]] + ways[i - [2]] return ways[n]
The ways to reach stair i come from stair i-1 and stair i-2.
Fill all three blanks to create a dictionary comprehension that maps stairs to ways if ways are more than 2.
def climb_stairs(n): if n <= 2: return {} ways = [0] * (n + 1) ways[1], ways[2] = 1, 2 for i in range(3, n + 1): ways[i] = ways[i - 1] + ways[i - 2] result = [1]: [2] for [3] in range(1, n + 1) if ways[i] > 2}} return result
The dictionary maps stair number i to ways[i] for i in the range.
