MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / climb_stairs

Function climb_stairs

dynamic_programming/climbing_stairs.py:4–36  ·  view source on GitHub ↗

LeetCdoe No.70: Climbing Stairs Distinct ways to climb a number_of_steps staircase where each time you can either climb 1 or 2 steps. Args: number_of_steps: number of steps on the staircase Returns: Distinct ways to climb a number_of_steps staircase Raises

(number_of_steps: int)

Source from the content-addressed store, hash-verified

2
3
4def climb_stairs(number_of_steps: int) -> int:
5 """
6 LeetCdoe No.70: Climbing Stairs
7 Distinct ways to climb a number_of_steps staircase where each time you can either
8 climb 1 or 2 steps.
9
10 Args:
11 number_of_steps: number of steps on the staircase
12
13 Returns:
14 Distinct ways to climb a number_of_steps staircase
15
16 Raises:
17 AssertionError: number_of_steps not positive integer
18
19 >>> climb_stairs(3)
20 3
21 >>> climb_stairs(1)
22 1
23 >>> climb_stairs(-7) # doctest: +ELLIPSIS
24 Traceback (most recent call last):
25 ...
26 AssertionError: number_of_steps needs to be positive integer, your input -7
27 """
28 assert isinstance(number_of_steps, int) and number_of_steps > 0, (
29 f"number_of_steps needs to be positive integer, your input {number_of_steps}"
30 )
31 if number_of_steps == 1:
32 return 1
33 previous, current = 1, 1
34 for _ in range(number_of_steps - 1):
35 current, previous = current + previous, current
36 return current
37
38
39if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected