Problem 178349 · medium · Phase 01 Prerequisites & Setup

Centered Pyramid

nested loops · strings · arithmetic

Build a centred pyramid of n rows. Row i (counting from 1) contains 2*i - 1 stars, with spaces on both sides so that every row is exactly 2*n - 1 characters wide. Return the rows as a list of strings.

Examples

Input:  n = 3
Output: ["  *  ", " *** ", "*****"]

Input:  n = 1
Output: ["*"]

Constraints

  • 0 <= n <= 40
  • Return [] when n is 0.

Goals

  • Work out the number of stars and padding for each row from the row index
  • Keep every row the same width so the pyramid is centred
Starting Python…