2019-07-16 05:26:28 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
2022-10-30 09:59:10 +00:00
|
|
|
def climb_stairs(number_of_steps: int) -> int:
|
2019-07-16 05:26:28 +00:00
|
|
|
"""
|
|
|
|
LeetCdoe No.70: Climbing Stairs
|
2022-10-30 09:59:10 +00:00
|
|
|
Distinct ways to climb a number_of_steps staircase where each time you can either
|
|
|
|
climb 1 or 2 steps.
|
2019-07-16 05:26:28 +00:00
|
|
|
|
|
|
|
Args:
|
2022-10-30 09:59:10 +00:00
|
|
|
number_of_steps: number of steps on the staircase
|
2019-07-16 05:26:28 +00:00
|
|
|
|
|
|
|
Returns:
|
2022-10-30 09:59:10 +00:00
|
|
|
Distinct ways to climb a number_of_steps staircase
|
2019-07-16 05:26:28 +00:00
|
|
|
|
|
|
|
Raises:
|
2022-10-30 09:59:10 +00:00
|
|
|
AssertionError: number_of_steps not positive integer
|
2019-07-16 05:26:28 +00:00
|
|
|
|
|
|
|
>>> climb_stairs(3)
|
|
|
|
3
|
|
|
|
>>> climb_stairs(1)
|
|
|
|
1
|
|
|
|
>>> climb_stairs(-7) # doctest: +ELLIPSIS
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2022-10-30 09:59:10 +00:00
|
|
|
AssertionError: number_of_steps needs to be positive integer, your input -7
|
2019-07-16 05:26:28 +00:00
|
|
|
"""
|
2021-02-23 05:53:49 +00:00
|
|
|
assert (
|
2022-10-30 09:59:10 +00:00
|
|
|
isinstance(number_of_steps, int) and number_of_steps > 0
|
|
|
|
), f"number_of_steps needs to be positive integer, your input {number_of_steps}"
|
|
|
|
if number_of_steps == 1:
|
2019-07-16 05:26:28 +00:00
|
|
|
return 1
|
2022-10-30 09:59:10 +00:00
|
|
|
previous, current = 1, 1
|
|
|
|
for _ in range(number_of_steps - 1):
|
|
|
|
current, previous = current + previous, current
|
|
|
|
return current
|
2019-07-16 05:26:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|