2. Pascal’s triangle is an infinite two-dimensional pattern of numbers whose fir
ID: 3762019 • Letter: 2
Question
2. 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.
>>> pascalLine(0)
[1]
>>> pascalLine(2)
[1, 2, 1]
>>> pascalLine(3)
[1, 3, 3, 1]
>>> pascalLine(4)
[1, 4, 6, 4, 1]
2 3 3 4 6 4Explanation / Answer
def pascal(n): """Prints out n rows of Pascal's triangle. It returns False for failure and True for success.""" row = [1] k = [0] for x in range(max(n,0)): print row row=[l+r for l,r in zip(row+k,k+row)] return n>=1 (or) def scan(op, seq, it): a = [] result = it a.append(it) for x in seq: result = op(result, x) a.append(result) return a def pascal(n): def nextrow(row, x): return [l+r for l,r in zip(row+[0,],[0,]+row)] return scan(nextrow, range(n-1), [1,]) for row in pascal(4): print(row) pascal:{(x-1){0+':x,0}} pascal 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.