Step 01 Beginner Problems — Patterns
Pattern 19 — Symmetric-Void Pattern
Two solid blocks of stars separated by a void that opens and then closes: the figure starts as one solid band of 2n stars, splits into two blocks that drift apart until only single stars remain at the edges, then comes back together. It is Pattern 12's 'two sides plus a gap' idea rendered in stars and mirrored top-to-bottom.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
How to think about it
Think of each row as three parts across a fixed canvas of 2n slots: a left block of stars, a gap of blank slots, and a right block equal to the left. In the top half, row i (0-based) has n - i stars per side and a gap of 2i slots — the void widens by two each row as the blocks erode from the inside. The bottom half runs the same formulas in reverse: row i has i + 1 stars per side and a gap of 2(n - i - 1) slots. The middle two rows are identical (1 star, a gap of 2n - 2 slots, 1 star), and the first and last rows are the solid 2n-star band.
Code
The code builds rows as token lists — side stars, then gap placeholders (' ' tokens that keep slot widths uniform), then side stars again — and joins them. A first loop generates the opening half and a second loop the closing half; concatenating the two lists and printing them in order gives the full symmetric figure.
def pattern(n):
lines = []
for i in range(n):
side = ["*"] * (n - i)
gap = [" "] * (2 * i)
lines.append(" ".join(side + gap + side))
for i in range(n):
side = ["*"] * (i + 1)
gap = [" "] * (2 * (n - i - 1))
lines.append(" ".join(side + gap + side))
for line in lines:
print(line)Key points
- Row anatomy: side stars + gap + side stars, always totalling 2n slots.
- Top half row i: stars per side = n - i, gap = 2i. The bottom half mirrors it.
- The two middle rows are identical — the void is widest there (2n - 2 slots).
- Placeholder blank tokens keep both blocks aligned; raw space printing breaks the right block.
- The figure is symmetric both horizontally and vertically — fold it either way to check.