Step 01 Beginner Problems — Patterns
Pattern 20 — Symmetry Pattern
The mirror journey of Pattern 19: two star triangles start as far apart as possible — single stars at the far edges — and grow toward each other row by row until they merge into a solid band of 2n stars at the center, then separate again. The figure is symmetric across both axes, like an hourglass turned on its side.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
How to think about it
Use a single row counter k = 0, 1, ..., 2n - 1 (2n rows). Let s = min(k, 2n - 1 - k) + 1 — the distance from the nearer end, plus one. Row k has s stars on the left, a gap of 2(n - s) blank slots, and s stars on the right, always filling the same 2n-slot canvas. At the ends (k = 0 and k = 2n - 1) s = 1: lone stars at the edges with the maximum void of 2n - 2 slots. In the middle (k = n - 1 and k = n) s = n: the gap closes completely and the row is solid. The min() folds the second half onto the first, so one formula generates the whole figure.
Code
One loop over 2n rows computes side_count from the folded index min(k, 2n - 1 - k) + 1, then joins side stars, gap placeholders, and side stars. Folding the index with min() is the key move — it makes the bottom half automatically mirror the top without a second loop.
def pattern(n):
for k in range(2 * n):
side_count = min(k, 2 * n - 1 - k) + 1
side = ["*"] * side_count
gap = [" "] * (2 * (n - side_count))
print(" ".join(side + gap + side))Key points
- Row k: side = min(k, 2n - 1 - k) + 1 stars, gap = 2(n - side) slots — one formula, whole figure.
- 2n rows; the solid 2n-star row appears twice at the center.
- Index folding with min() replaces a second, mirrored loop.
- It is Pattern 19 run backwards: the void closes, then reopens.
- Every row spans exactly 2n slots — check the first and last rows for equal width.