Pre-Calculus for Programmers

Expansion coefficients

Expansion coefficients

You are writing the helper that a small algebra tool calls when it needs to expand (a+b)n(a+b)^{n}. It does not need the letters, only the numbers in front of each term, in order.

Given n, return the coefficients of (a+b)n(a+b)^{n} starting with the term in ana^{n} and ending with the term in bnb^{n}. That is row n of Pascal's triangle, counting the single 1 at the top as row 0, so the answer always has n+1n+1 numbers in it.

Worked example

Input: n = 4

(a+b)^4 = a^4 + 4a^3b + 6a^2b^2 + 4ab^3 + b^4

Output: [1, 4, 6, 4, 1]
The fourth row.

The awkward ends

Input: n = 0    Output: [1]        because (a+b)^0 = 1
Input: n = 1    Output: [1, 1]     because (a+b)^1 = a + b
The two smallest cases.

Constraints

  • n is a whole number from 0 to 30.
  • The order matters, and the row is returned left to right.
  • The largest number that can appear is the middle of row 30, which is 155117520, so ordinary whole-number arithmetic is enough.
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.