Pascal’s Triangle
Pascal’s Triangle was developed by a French mathematician named Blaise Pascal. The rows are numbered starting with 0 and go all the way to n, just like list indices in Python. The triangle always starts with a single term in the 0th row, which is a [1]. The nth row of the triangle has n+1 terms, and each term is found by adding the values of the two terms above it. For example, row number 2 (actually the third row) has three terms: [1, 2, 1]. The row above it, row number 1, is [1, 1], and the row above that one, row 0, is just [1]. Pascal’s Triangle has several uses in mathematics, such as binomial expansions. (http://en.wikipedia.org/wiki/Pascal%27s_triangle contains more information.) See how the terms are obtained in the figure below.
Task 1:
Here are some examples of calling the pascal_row function:
>>> pascal_row(0) [1]
>>> pascal_row(1) [1, 1]
>>> pascal_row(5)
[1, 5, 10, 10, 5, 1]
CS 115 – Hw 4
Task 2:
Write a second function called pascal_triangle that takes as input a single integer n and returns a list of lists containing the values of the all the rows up to and including row n.
Here are some examples of calling the pascal_triangle function:
>>> pascal_triangle(0)
[[1]]
>>> pascal_triangle(1) [[1], [1, 1]]
>>> pascal_triangle(5)
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]]
Task 3:
Write two functions, named test_pascal_row and test_pascal_triangle,with no
arguments. They should do what the name suggests – test the corresponding function. There should be at least four tests each. For testing, use assertions, in the way shown in the file assert_LCS_change.py that was posted online. In brief, each test asserts that a call of your function equals the value expected.