Skip to content

Instantly share code, notes, and snippets.

@joshz
Last active August 29, 2015 14:11
Show Gist options
  • Save joshz/9b82df5eca5c86ff1693 to your computer and use it in GitHub Desktop.
Save joshz/9b82df5eca5c86ff1693 to your computer and use it in GitHub Desktop.
pascal's trimangle, no recursion, no combinations
def triangle(r):
def row(n, previous=None):
if n == 1:
return n+1, [1]
else:
rown = []
rown.append(1)
for i in xrange(n):
if i + 1 < n-1:
rown.append(previous[i] + previous[i+1])
rown.append(1)
return n+1, rown
rowd = []
all_rows = []
n = 1
while n <= r:
n, rowd = row(n, rowd)
all_rows.append(rowd)
return all_rows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment