Skip to content

Instantly share code, notes, and snippets.

@danishi
Last active September 9, 2023 11:18
Show Gist options
  • Select an option

  • Save danishi/3986d116f060761992cdedd89c34f0a6 to your computer and use it in GitHub Desktop.

Select an option

Save danishi/3986d116f060761992cdedd89c34f0a6 to your computer and use it in GitHub Desktop.
Pascal's triangle
import copy
init_array = [1, 0, 0, 0, 0, 0, 0, 0, 0]
for i in range(1, 10):
copy_array = copy.copy(init_array)
if i != 1:
copy_array.insert(0, 1)
copy_array.pop(9)
for j in range(9):
init_array[j] = init_array[j] + copy_array[j]
buf = []
for k in range(i):
buf.append(str(copy_array[k]))
print(' '.join(buf))
@danishi
Copy link
Author

danishi commented Sep 9, 2023

$ python pascals_triangle.py
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1      
1 7 21 35 35 21 7 1   
1 8 28 56 70 56 28 8 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment