Skip to content

S3Q2 · Hollow Pyramid Pattern

⚡ Quick Reference

Type: Full I/O problem

Core idea: Three row types -- top (single *), hollow middle (* + spaces + *), full bottom row. Leading spaces = n - i. Interior spaces = 2*i - 3.

n = int(input())
for i in range(1, n + 1):
    if i == 1:
        print(' ' * (n - 1) + '*')
    elif i == n:
        print('*' * (2 * n - 1))
    else:
        print(' ' * (n - i) + '*' + ' ' * (2 * i - 3) + '*')

Key rules: - Bottom row width = 2*n - 1 - Leading spaces at row i = n - i - Interior spaces at row i = 2*i - 3 (only for 2 <= i <= n-1) - No trailing spaces -- build each row ending with *


Problem Statement

Problem

Given n >= 2, print a hollow pyramid with n rows. Single * at the top, boundary *s on left and right in middle rows, interior filled with spaces, and a full row of *s at the bottom.

Examples:

Input
4
Output
   *
  * *
 *   *
*******
Input
5
Output
    *
   * *
  *   *
 *     *
*********

Understanding the problem

Every row has three parts: leading spaces, left *, interior, right *. Derive the formulas from the pattern:

Row i Leading spaces Stars at edges Interior spaces Total width
1 (top) n - 1 1 (just one *) -- n
2 n - 2 2 2*2-3 = 1 2n-1
3 n - 3 2 2*3-3 = 3 2n-1
i (mid) n - i 2 2*i - 3 2n-1
n (base) 0 full row -- 2n-1

For n = 4:

   *         ← 3 spaces + *
  * *         ← 2 spaces + * + 1 space + *
 *   *        ← 1 space + * + 3 spaces + *
*******       ← 7 stars (2*4-1)

Interior spaces formula: 2*i - 3

At row i, the pyramid is 2*i - 1 characters wide. Subtracting the two boundary stars leaves 2*i - 1 - 2 = 2*i - 3 interior spaces. At i = 2: 2*2-3 = 1 space; at i = 3: 2*3-3 = 3 spaces -- matches the output.

Don't use str.center() for the top row

'*'.center(2*n-1) looks convenient but pads with trailing spaces (e.g. ' * ' for n=4). Build the top row as ' '*(n-1) + '*' instead -- it ends with * and has no trailing spaces.


Solution approaches

n = int(input())
for i in range(1, n + 1):
    if i == 1:
        # Top: n-1 leading spaces, single star
        print(' ' * (n - 1) + '*')
    elif i == n:
        # Base: full row, width = 2*n - 1
        print('*' * (2 * n - 1))
    else:
        # Hollow middle: left *, interior spaces, right *
        leading  = n - i
        interior = 2 * i - 3
        print(' ' * leading + '*' + ' ' * interior + '*')
n = int(input())
def row(i):
    if i == 1:   return ' ' * (n - 1) + '*'
    if i == n:   return '*' * (2 * n - 1)
    return ' ' * (n - i) + '*' + ' ' * (2 * i - 3) + '*'
print('\n'.join(row(i) for i in range(1, n + 1)))
n = int(input())
row = lambda i: (
    ' '*(n-1)+'*'        if i == 1 else
    '*'*(2*n-1)          if i == n else
    ' '*(n-i)+'*'+' '*(2*i-3)+'*'
)
print('\n'.join(row(i) for i in range(1, n + 1)))

Key takeaways

01

Derive formulas from a small example

Trace n=4 by hand: count leading spaces and interior spaces for each row. Pattern: leading = n-i, interior = 2*i-3. Always verify formulas against at least two rows before coding.

02

Avoid trailing spaces by building strings ending in *

Hollow middle rows end with * -- no trailing spaces possible. The top row is ' '*(n-1) + '*', not '*'.center(width), which would pad with spaces on the right.

03

Three-type row classification

Pattern problems almost always reduce to a small number of row types. Here: top (special case), hollow middle (formula-driven), solid base. Identify the types first, then derive the formula for each.