Skip to content

Commit 4380fde

Browse files
committed
completed 8.1
1 parent 6e78a38 commit 4380fde

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
""" 8.1 Triple Step: A child is running up a staircase with n steps an can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs. """
2+
3+
4+
def count_ways(n):
5+
if n < 0:
6+
return 0
7+
elif n == 0:
8+
return 1
9+
else:
10+
return count_ways(n-1) + count_ways(n-2) + count_ways(n-3)
11+
12+
13+
def count_ways_memo(n, memo={}):
14+
if n < 0:
15+
return 0
16+
elif n == 0:
17+
return 1
18+
else:
19+
memo[n] = count_ways_memo(n-1, memo) + count_ways_memo(n-2, memo) + count_ways_memo(n-3, memo)
20+
return memo[n]

0 commit comments

Comments
 (0)