Skip to content

Instantly share code, notes, and snippets.

@travishsu
Created November 15, 2022 22:51
Show Gist options
  • Save travishsu/7150be0983da972edca105bc0b740212 to your computer and use it in GitHub Desktop.
Save travishsu/7150be0983da972edca105bc0b740212 to your computer and use it in GitHub Desktop.
Pascal's Triangle
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