Back to Practice
#0211
Generate Pascal Triangle
MediumPython25 min20 XP
Problem
Return the first n rows of Pascal triangle as a list of rows. Each row should be a list of integers.
Why This Matters
Pascal triangle is a classic pattern question because it tests indexing, nested lists, and reuse of previous results.
Function Signature
def pascal_triangle(n):
Examples
Example 1
Inputn = 5
Output[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
Every middle value is the sum of the two values above it.
Constraints
- Return the exact requested value.
- Handle small edge cases cleanly.
- Prefer readable Python over clever shortcuts.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputn = 5
Expected[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
Every middle value is the sum of the two values above it.
Hidden Test Categories
n = 0n = 1n = 8