Step 01 Beginner Problems — Patterns
Pattern 9 — Diamond Star Pattern
A full diamond: the star pyramid of Pattern 7 stacked directly on top of the inverted pyramid of Pattern 8, giving 2n rows with the widest row of 2n - 1 stars appearing twice in the middle. It shows how the big patterns in this series are assembled from smaller ones you already know.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
How to think about it
The figure splits into two halves of n rows each. In the top half, row i (0-based within the half) has n - i - 1 blank slots and 2i + 1 stars — exactly Pattern 7. In the bottom half, row i has i blank slots and 2(n - i) - 1 stars — exactly Pattern 8. Because both halves include their widest row, the 2n - 1 star row is printed twice, once as the pyramid's base and once as the inverted pyramid's base. The sequence of widths, 1, 3, ..., 2n-1, 2n-1, ..., 3, 1, is a palindrome, just like the figure.
Code
The code is literally the two earlier solutions pasted together: a first loop running Pattern 7's row formula, then a second loop running Pattern 8's. No new formula is needed — the craft is in reusing the halves verbatim, including the duplicated widest row.
def pattern(n):
for i in range(n):
indent = " " * (n - i - 1)
stars = " ".join(["*"] * (2 * i + 1))
print(indent + stars)
for i in range(n):
indent = " " * i
stars = " ".join(["*"] * (2 * (n - i) - 1))
print(indent + stars)Key points
- A diamond = pyramid (Pattern 7) + inverted pyramid (Pattern 8), 2n rows total.
- The widest row (2n - 1 stars) appears twice — that duplication is part of the classic shape.
- Widths go 1, 3, ..., 2n-1, 2n-1, ..., 3, 1 — palindromic, like the figure itself.
- Reuse beats reinvention: if your two halves are correct separately, the diamond is correct.
- Variant alert: some versions print the widest row only once (2n - 1 rows) — know which one a problem asks for.