easy +10 pts

Pascal Triangle Row

Compute the nth row of Pascal's triangle efficiently.

Pascal's triangle is a triangular array of the binomial coefficients. The rows are numbered starting from 0. Row 0 is [1], row 1 is [1, 1], row 2 is [1, 2, 1], and so on. Write a function `pascals_triangle_row(n)` that takes a non-negative integer `n` and returns the `n`th row as a list of integers.

Constraints

0 ≤ n ≤ 30. The output row has length n+1. The numbers can be large but fit within Python's int. Time complexity: O(n^2) or better.

Example

>>> pascals_triangle_row(0)
[1]
>>> pascals_triangle_row(1)
[1, 1]
>>> pascals_triangle_row(4)
[1, 4, 6, 4, 1]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Each row can be built from the previous row by adding adjacent elements.
The first and last elements of every row are always 1.
You can also use the binomial coefficient formula with a running product for O(n) time.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.