Skip to content

Instantly share code, notes, and snippets.

@nicholaskajoh
Created November 5, 2016 16:50
Show Gist options
  • Save nicholaskajoh/51e13786be3b1f7cf98eb75e7bc3e042 to your computer and use it in GitHub Desktop.
Save nicholaskajoh/51e13786be3b1f7cf98eb75e7bc3e042 to your computer and use it in GitHub Desktop.
Generate a Pascal triangle of n rows.
# pascal traingle generator - generates n rows of triangle
if __name__ == "__main__":
n = int(input("n: ")) # num rows
triangle = [] # pascal triangle
for x in range(0, n):
# no references for the 1's at the edges
# hard code them, generate the rest
if x == 0:
triangle.append([1])
elif x == 1:
triangle.append([1,1])
else:
row = []
row.append(1)
for y in range(0, x-1):
row.append(triangle[x-1][y] + triangle[x-1][y+1])
row.append(1)
triangle.append(row)
print(triangle[x])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment