MA0301/exercise8/scripts/pascal.py

39 lines
771 B
Python

from math import comb
def pascal(n, left, right):
i = 1
result = [[left, right]]
while i < n:
prev_row = result[-1]
result.append(
[left ** (i+1)]
+ [comb(i+1, j+1) * (left ** (len(prev_row)-j-1)) * (right ** (j+1)) for j in range(len(prev_row)-1)]
+ [right ** (i+1)])
i += 1
return result
def pretty_print_pascal(triangle):
for i, row in enumerate(triangle):
print(f'{i+1}.\t{row}')
def pretty_print_row(row):
for i, coefficient in enumerate(row):
print(f'{coefficient}\tx^{len(row)-i-1}\ty^{i}')
if __name__ == "__main__":
print('a. ')
pretty_print_row(pascal(12, 1, 1)[-1])
print()
print('b. ')
pretty_print_row(pascal(12, 1, 2)[-1])
print()
print('c. ')
pretty_print_row(pascal(12, 2, -3)[-1])