|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | + |
1 | 4 | def climb_stairs(n: int) -> int:
|
2 |
| - """ |
3 |
| - LeetCdoe No.70: Climbing Stairs |
4 |
| - Distinct ways to climb a n step staircase where |
5 |
| - each time you can either climb 1 or 2 steps. |
6 |
| -
|
7 |
| - Args: |
8 |
| - n: number of steps of staircase |
9 |
| -
|
10 |
| - Returns: |
11 |
| - Distinct ways to climb a n step staircase |
12 |
| -
|
13 |
| - Raises: |
14 |
| - AssertionError: n not positive integer |
15 |
| -
|
16 |
| - >>> climb_stairs(3) |
17 |
| - 3 |
18 |
| - >>> climb_stairs(1) |
19 |
| - 1 |
20 |
| - """ |
21 |
| - assert isinstance(n,int) and n > 0, "n needs to be positive integer, your input {0}".format(0) |
22 |
| - if n == 1: return 1 |
23 |
| - dp = [0]*(n+1) |
24 |
| - dp[0], dp[1] = (1, 1) |
25 |
| - for i in range(2,n+1): |
26 |
| - dp[i] = dp[i-1] + dp[i-2] |
27 |
| - return dp[n] |
| 5 | + """ |
| 6 | + LeetCdoe No.70: Climbing Stairs |
| 7 | + Distinct ways to climb a n step staircase where |
| 8 | + each time you can either climb 1 or 2 steps. |
| 9 | +
|
| 10 | + Args: |
| 11 | + n: number of steps of staircase |
| 12 | +
|
| 13 | + Returns: |
| 14 | + Distinct ways to climb a n step staircase |
| 15 | +
|
| 16 | + Raises: |
| 17 | + AssertionError: n 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: n needs to be positive integer, your input -7 |
| 27 | + """ |
| 28 | + fmt = "n needs to be positive integer, your input {}" |
| 29 | + assert isinstance(n, int) and n > 0, fmt.format(n) |
| 30 | + if n == 1: |
| 31 | + return 1 |
| 32 | + dp = [0] * (n + 1) |
| 33 | + dp[0], dp[1] = (1, 1) |
| 34 | + for i in range(2, n + 1): |
| 35 | + dp[i] = dp[i - 1] + dp[i - 2] |
| 36 | + return dp[n] |
| 37 | + |
| 38 | + |
| 39 | +if __name__ == "__main__": |
| 40 | + import doctest |
| 41 | + |
| 42 | + doctest.testmod() |
0 commit comments