This problem is part of our Ada Lovelace Day celebration.
It is for practice only and will NOT count towards your grade!
(just have fun and use it to improve your skills: there is no submission deadline!)
In this problem you should submit a function as described. Inside the function do not print anything that was not asked!
The name of the .py source file you submit to Mooshak should NOT start with a digit (you will get an error if you submit it like that).
Ada Lovelace was an English mathematician who, in 1843, collaborated with Charles Babbage to show how the Analytical Engine (his proposed mechanical computer) could be used for complex computations.
Her notes included an algorithm for calculating Bernoulli numbers, and she is often described as the first computer programmer.
To calculate Bernoulli numbers, Ada needed to use Pascal's triangle. Each row starts and ends with 1, and each interior entry is the sum of the two entries above it.
row 0: 1 row 1: 1 1 row 2: 1 2 1 row 3: 1 3 3 1 row 4: 1 4 6 4 1
The same rows can be written as Python lists:
| Row | Values |
| 0 | [1] |
| 1 | [1, 1] |
| 2 | [1, 2, 1] |
| 3 | [1, 3, 3, 1] |
| 4 | [1, 4, 6, 4, 1] |
Here are the rules:
For example, to go from row 3 to row 4:
previous row: [1, 3, 3, 1] new row: [1, 4, 6, 4, 1] 4 = 1 + 3 6 = 3 + 3 4 = 3 + 1
Help Ada by implementing function pascal_row(n). The function receives an integer n and returns a list of integers containing the numbers in row n of Pascal's triangle, from left to right.
The function should return only the requested row.
The following limits are guaranteed in all the test cases that will be given to your program:
| 0 ≤ n ≤ 100 | row to calculate |
| Example Function Calls | Example Output |
print(pascal_row(0)) print(pascal_row(1)) print(pascal_row(2)) print(pascal_row(5)) print(pascal_row(7)) |
[1] [1, 1] [1, 2, 1] [1, 5, 10, 10, 5, 1] [1, 7, 21, 35, 35, 21, 7, 1] |