Created
November 5, 2016 16:50
-
-
Save nicholaskajoh/51e13786be3b1f7cf98eb75e7bc3e042 to your computer and use it in GitHub Desktop.
Generate a Pascal triangle of n rows.
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
# 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