Last active
November 13, 2015 19:31
-
-
Save mhingston/4ae4192082791d012842 to your computer and use it in GitHub Desktop.
This file contains 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
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