Pascal's triangle was a challenge on a forum I frequent today and this is what I came up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | def pascals_triangle(n):
'''
Pascal's triangle:
for x in pascals_triangle(5):
print('{0:^16}'.format(x))
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
'''
x=[[1]]
for i in range(n-1):
x.append([sum(i) for i in zip([0]+x[-1],x[-1]+[0])])
return x
|
Even better:
The second doesn't even look triangular :-)
maybe you also like
for sure it works just for ´n<6´ since it would cut the ´10´ to ´1, 0´ etc.