Skip to content

Instantly share code, notes, and snippets.

@pmallory
Last active March 1, 2017 01:29
Show Gist options
  • Save pmallory/150ab4eb9233f686322f to your computer and use it in GitHub Desktop.
Save pmallory/150ab4eb9233f686322f to your computer and use it in GitHub Desktop.
Stairs problem: dynamic programming solution
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