Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pascal’s triangle is an infinite two-dimensional pattern of numbers whose first

ID: 3760645 • Letter: P

Question

Pascal’s triangle is an infinite two-dimensional pattern of numbers whose first five lines are illustrated in Figure 10.16. The first line, line 0, contains just 1. All other lines start and end with a 1 too. The other numbers in those lines are obtained using this rule: The number at position i is the sum of the numbers in position i 1 and i in the previous line. In python, Implement recursive function pascalLine() that takes a nonnegative integer n as input and returns a list containing the sequence of numbers appearing in the nth line of Pascal’s triangle.

>>> pascalLine(0)

[1]

>>> pascalLine(2)

[1, 2, 1]

>>> pascalLine(3)

[1, 3, 3, 1]

>>> pascalLine(4)

[1, 4, 6, 4, 1]

Explanation / Answer

row = []
def generatePascal(n,k,x):
if k!=n+1:
row.append(x)
x = x*(n-k)/(k+1)
generatePascal(n,k+1,x)

n = int(raw_input('Input the nth row of pascal series : '))
generatePascal(n,0,1)
print row