Created
November 15, 2022 22:51
-
-
Save travishsu/7150be0983da972edca105bc0b740212 to your computer and use it in GitHub Desktop.
Pascal's Triangle
This file contains hidden or 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 pascal(n): | |
L = n + n - 1 | |
rows = [] | |
row = [1] | |
rows.append(row) | |
while len(rows) < n: | |
row_cat = [0] + row + [0] | |
row = [row_cat[i] + row_cat[i+1] for i in range(len(row_cat)-1)] | |
rows.append(row) | |
for row_idx, row in enumerate(rows): | |
print(" "*(n-row_idx), end="") | |
print(" ".join([str(r) for r in row])) | |
# pascal(6) | |
# 1 | |
# 1 1 | |
# 1 2 1 | |
# 1 3 3 1 | |
# 1 4 6 4 1 | |
# 1 5 10 10 5 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment