Last active
March 1, 2017 01:29
-
-
Save pmallory/150ab4eb9233f686322f to your computer and use it in GitHub Desktop.
Stairs problem: dynamic programming solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import * | |
def count_ways(n, max_step_size=2): | |
table = [Decimal(1), Decimal(1), Decimal(2)] | |
assert len(table) >= max_step_size | |
for i in xrange(3,n+1): | |
table.append(sum(table[i-max_step_size:i])) | |
return table[n] | |
def probability(n): | |
return count_ways(n, 2) / count_ways(n, 3) | |
print probability(2000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment