A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 9 — Diamond Star Pattern

Easy

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.

        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
Output for n = 5

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.

Suggested questions

Pattern 7 — Star Pyramid The top half of this diamond, standalone.
Pattern 8 — Inverted Star Pyramid The bottom half of this diamond, standalone.
Pattern 10 — Half Diamond Star Pattern A sideways cousin — a half diamond built from a single growing-then-shrinking count.
Pattern 20 — Symmetry Pattern Another two-sided symmetric figure, built from facing triangles instead of pyramids.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.