Foundations Loops through pattern printing
11. Star Pyramid
Given an integer n, return the pattern below as a list of strings, one string per row, top row first.
*
***
*****
*******
That is the pattern for n = 4. The pyramid has n rows. The top row holds one star, and each row below it
holds two stars more than the row above, so the bottom row holds 2n − 1 stars and starts at the left edge.
Every row is centred over the bottom row, so it starts with enough spaces to put its middle star above the
middle of the bottom row.
The stars in a row sit side by side. A row has spaces only before its first star, never between stars and never after the last one.
Example 1
Input: n = 3
* *** *****
Explanation: The three rows hold 1, 3 and 5 stars, after 2, 1 and 0 spaces. Nothing comes after the last star of a row.
Example 2
Input: n = 1
*Example 3
Input: n = 5
* *** ***** ******* *********
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 100
Clarifying questions
What you would ask an interviewer before writing any code.