Centered Pascal Pyramid Renderer
StringsAuto-graded · 15 test casesLast updated
Build the first n rows of Pascal's triangle and render them as a centered text pyramid with fixed-width, right-aligned cells and floor-based indentation.
Write a function that builds the first n_rows rows of Pascal's triangle and returns them as a single multi-line string, laid out as a centered pyramid.
Row r (starting at 0) contains the binomial coefficients C(r, 0), C(r, 1), ..., C(r, r).
Layout rules
- Let
max_valuebe the largest number anywhere in the triangle, and letwbe the number of decimal digits inmax_value. - Write every number right-aligned in a cell of exactly
wcharacters, padding on the left with spaces. - Within a row, join the cells with exactly one space between neighbors. Call the result the row's body.
- Let
Lbe the length of the body of the last row (the widest one). - For each row, put
(L - len(body)) // 2spaces (integer floor division) in front of its body. This is the only padding added. - Do not add trailing spaces to any line.
- Join the lines with a single newline character
"\n", with no newline after the last line.
Because the indent uses floor division, the pyramid may lean by half a column when w is even. Follow the rule exactly as stated.
Task
Return the resulting string for a given n_rows.
Constraints
1 <= n_rows <= 60- The function returns a
str; it does not print anything. - Every row has no trailing whitespace, and the whole string has no trailing newline.
Examples
Example 1
Input: render_pascal_pyramid(5)
Output: ' 1\n 1 1\n 1 2 1\n 1 3 3 1\n1 4 6 4 1'
Example 2
Input: render_pascal_pyramid(1)
Output: '1'
Example 3
Input: render_pascal_pyramid(3)
Output: ' 1\n 1 1\n1 2 1'
Hints
Hint 1
Row r of Pascal's triangle is just C(r, 0..r), or you can build it from the previous row by adding adjacent pairs.
Hint 2
The widest number always appears in the last row, but computing the maximum over all rows is just as safe. Its digit count gives the cell width.
Hint 3
Build each row's body first (right-aligned cells joined by one space), then use the length of the last body to compute each row's indent with floor division.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Split the work into two steps: generate the numbers, then lay them out.
- Generate. Row
ris[C(r, 0), ..., C(r, r)]. These can be computed withmath.comb, or by summing adjacent entries of the previous row. - Find the cell width. Take the largest value in the triangle (it is the middle of the last row) and compute
w = len(str(max_value)). - Build row bodies. Right-justify each number to width
wand join with a single space. - Center. The last row's body is the longest, with length
L. Every other row gets(L - len(body)) // 2leading spaces, so its body sits as close to the center as integer spacing allows. - Assemble. Join all lines with
"\n". Nothing is added on the right, so there are no trailing spaces.
Why it is correct
- Pascal's identity
C(r, k) = C(r-1, k-1) + C(r-1, k)withC(r, 0) = C(r, r) = 1gives exactly the rows required, andcombcomputes the same values directly. - Each cell has width
w, which is at least the digit count of every value, so no number overflows its cell and every column in a row has the same width. - Each body's length is
(k)·w + (k-1)forkcells, so bodies grow by a constant amount per row. The indent formula places each body's center at or within half a column of the last row's center. Whenwis even the floor can shift a row by half a column, which is the intended behavior of the rule.
Complexity
With n = n_rows:
- Time: O(n²) numbers are generated and formatted. Each has up to O(n) digits, so the output size is O(n³) characters in the worst case.
- Space: O(n²) numbers stored, plus the output string.
Reference solution
from math import comb
def render_pascal_pyramid(n_rows: int) -> str:
rows = []
for r in range(n_rows):
rows.append([comb(r, k) for k in range(r + 1)])
max_value = max(max(row) for row in rows)
width = len(str(max_value))
bodies = [" ".join(str(v).rjust(width) for v in row) for row in rows]
last_len = len(bodies[-1])
return "\n".join(" " * ((last_len - len(b)) // 2) + b for b in bodies)