forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimbing_stairs.py
42 lines (33 loc) · 1.09 KB
/
climbing_stairs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
defclimb_stairs(number_of_steps: int) ->int:
"""
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:
AssertionError: number_of_steps not positive integer
>>> climb_stairs(3)
3
>>> climb_stairs(1)
1
>>> climb_stairs(-7) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
AssertionError: number_of_steps needs to be positive integer, your input -7
"""
assertisinstance(number_of_steps, int) andnumber_of_steps>0, (
f"number_of_steps needs to be positive integer, your input {number_of_steps}"
)
ifnumber_of_steps==1:
return1
previous, current=1, 1
for_inrange(number_of_steps-1):
current, previous=current+previous, current
returncurrent
if__name__=="__main__":
importdoctest
doctest.testmod()