Editor

Row of Pascal's Triangle

#275 · Lists · Intermediate · 60 XP · about 10 min

Pascal's triangle starts with a single 1 at the top. Each row below starts and ends with 1, and every other number is the sum of the two numbers diagonally above it:

        1          row 0
       1 1         row 1
      1 2 1        row 2
     1 3 3 1       row 3
    1 4 6 4 1      row 4

Write pascal_row(n) that returns row n as a list of integers. Rows are numbered from 0, so row n has n + 1 numbers.

Examples

pascal_row(0)  # returns [1]
pascal_row(4)  # returns [1, 4, 6, 4, 1]
pascal_row(6)  # returns [1, 6, 15, 20, 15, 6, 1]

Constraints

  • 0 <= n <= 500
  • The numbers get very large, which Python integers handle without trouble

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.