I have created a Pascal's Triangle. Everything works how I want it to. I now want to change the color of specific numbers in the triangle. I would like to have the diagonals different colors, ie: The counting numbers would be blue and the triangular numbers would be orange. Is there a way that I can change the color of numbers in different lines, or even set up a sequence of colors (each number in a line is blue, red, orange, yellow.....then the following like would restart the sequence but at red)?
# Pascal's Triangle
def multiplicative(n, k):
result = 1
for i in range(1, k+1):
result = result * (n-(k-i))/i
return result
def pascal_level(n):
lists = []
ns = range(n)
for n in ns:
nlist = []
for k in range(n+1):
nlist.append(multiplicative(n, k))
nlist = ' '.join(map(str, nlist))
lists.append(nlist)
return lists
def center(strings):
maxlistlen = len(max(strings, key=len))
for i, s in enumerate(strings):
diff = maxlistlen - len(s)
pad = ' '*(diff/2)
yield str(i)+'-> '+pad+s
answer = int(raw_input("How many levels do you want? \n"))
ps = pascal_level(answer)
for r in center(ps):
print r
edit - I have a limited understanding of this so I will have to play around with the suggestions. Thanks for the help and I'll post an update when I finish. I've added a picture of something similar to what I'm trying to create.Example