easy +10 pts

Print Pyramid Pattern

Build a classic centered pyramid from asterisks using loops and string repetition.

Write a function `print_pyramid(n)` that takes a positive integer `n` and returns a list of strings representing a centered pyramid of asterisks with `n` rows. Row `i` (0-indexed) contains `i + 1` asterisks, separated by a single space, and is centered using spaces on the left only (do NOT add trailing spaces). The total width of the widest row is `2 * n - 1` characters (including the spaces between asterisks). For each row, the number of leading spaces is `(total_width - row_length) // 2`. The function should return a list of strings, where each string is a row of the pyramid. The function should NOT print anything; it must return the list. Example: for `n = 3`, the returned list is `[' *', ' * *', '* * *']` (note that there are two leading spaces in the first row, one in the second, and zero in the third).

Constraints

1 <= n <= 100. The function should run in O(n^2) time or better. Do not print; return a list.

Example

>>> print_pyramid(1)
['*']
>>> print_pyramid(3)
['  *', ' * *', '* * *']
>>> print_pyramid(5)
['    *', '   * *', '  * * *', ' * * * *', '* * * * *']
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Calculate the length of each row as `2 * (i + 1) - 1` for row index i.
The narrowest row (top) has length 1, so the leading spaces are `(2*n - 1 - 1) // 2`.
Build each row by joining a list of asterisks with spaces: `' '.join(['*'] * (i + 1))`.
Center by prepending the required number of spaces, not by using `str.center` (it adds trailing spaces).
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.