mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-27 19:38:39 +00:00
Fix error message and format with python/black (#1025)
@SandersLin Your review please?
This commit is contained in:
parent
1e55bfd4da
commit
2fb3beeaf1
@ -1,27 +1,42 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
|
||||||
def climb_stairs(n: int) -> int:
|
def climb_stairs(n: int) -> int:
|
||||||
"""
|
"""
|
||||||
LeetCdoe No.70: Climbing Stairs
|
LeetCdoe No.70: Climbing Stairs
|
||||||
Distinct ways to climb a n step staircase where
|
Distinct ways to climb a n step staircase where
|
||||||
each time you can either climb 1 or 2 steps.
|
each time you can either climb 1 or 2 steps.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
n: number of steps of staircase
|
n: number of steps of staircase
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Distinct ways to climb a n step staircase
|
Distinct ways to climb a n step staircase
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
AssertionError: n not positive integer
|
AssertionError: n not positive integer
|
||||||
|
|
||||||
>>> climb_stairs(3)
|
>>> climb_stairs(3)
|
||||||
3
|
3
|
||||||
>>> climb_stairs(1)
|
>>> climb_stairs(1)
|
||||||
1
|
1
|
||||||
"""
|
>>> climb_stairs(-7) # doctest: +ELLIPSIS
|
||||||
assert isinstance(n,int) and n > 0, "n needs to be positive integer, your input {0}".format(0)
|
Traceback (most recent call last):
|
||||||
if n == 1: return 1
|
...
|
||||||
dp = [0]*(n+1)
|
AssertionError: n needs to be positive integer, your input -7
|
||||||
dp[0], dp[1] = (1, 1)
|
"""
|
||||||
for i in range(2,n+1):
|
fmt = "n needs to be positive integer, your input {}"
|
||||||
dp[i] = dp[i-1] + dp[i-2]
|
assert isinstance(n, int) and n > 0, fmt.format(n)
|
||||||
return dp[n]
|
if n == 1:
|
||||||
|
return 1
|
||||||
|
dp = [0] * (n + 1)
|
||||||
|
dp[0], dp[1] = (1, 1)
|
||||||
|
for i in range(2, n + 1):
|
||||||
|
dp[i] = dp[i - 1] + dp[i - 2]
|
||||||
|
return dp[n]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user