Skip to content

Instantly share code, notes, and snippets.

@mhingston
Last active November 13, 2015 19:31
Show Gist options
  • Save mhingston/4ae4192082791d012842 to your computer and use it in GitHub Desktop.
Save mhingston/4ae4192082791d012842 to your computer and use it in GitHub Desktop.
def pascalsTriangle(n):
triangle = [[1]]
for i in xrange(1, n):
row = []
for j in xrange(len(triangle[i-1]) + 1):
if j-1 >= 0 and j < len(triangle[i-1]):
row.append(triangle[i-1][j-1] + triangle[i-1][j])
elif j < len(triangle[i-1]):
row.append(triangle[i-1][j])
else:
row.append(triangle[i-1][j-1])
triangle.append(row)
return triangle
print pascalsTriangle(6) # print rows 0-5 of Pascal's triangle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment