Skip to content

Instantly share code, notes, and snippets.

@stamaniorec
Created June 15, 2016 12:23
Show Gist options
  • Select an option

  • Save stamaniorec/092be58e8d2166be01da5ffa893ecb37 to your computer and use it in GitHub Desktop.

Select an option

Save stamaniorec/092be58e8d2166be01da5ffa893ecb37 to your computer and use it in GitHub Desktop.
Pascal triangle
from __future__ import print_function
def sum_one_to_n(n):
return (n * (n + 1)) / 2
def get_index(row, col):
return sum_one_to_n(row) + col
def get_value(row, col, l):
if col < 0:
return 0
return l[get_index(row, col)]
n = 5
how_many_elements = sum_one_to_n(n)
l = [None] * how_many_elements
l[0] = 1
row = 1
col = 0
for _ in range(1, how_many_elements):
l[get_index(row, col)] = get_value(row-1, col-1, l) + get_value(row-1, col, l)
col += 1
if col > row:
row = col
col = 0
# printing works prettily for n < 6
dots = n - 1
index = 0
for i in range(0, n):
for _ in range(0, dots):
print('.', end='')
for k in range(0, i+1):
print(l[index], end='')
index += 1
if k >= i:
break
print('_', end='')
for _ in range(0, dots):
print('.', end='')
dots -= 1
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment