A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 10 — Half Diamond Star Pattern

Easy

Half a diamond, sliced vertically: star counts climb 1, 2, 3, ..., n and then descend n-1, ..., 1, all glued to the left edge. With 2n - 1 rows it is the simplest 'go up, then come down' pattern — one count that changes direction at the peak.

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

How to think about it

Number all rows k = 0, 1, ..., 2n - 2 (that is 2n - 1 rows). The star count is k + 1 while k < n (the climb) and 2n - 1 - k once k >= n (the descent). At the peak row k = n - 1 the count is exactly n, and it appears only once — unlike Pattern 9's doubled widest row. Equivalently, the count is min(k + 1, 2n - 1 - k): the distance-driven minimum of the 'growing' and 'shrinking' formulas. There are no leading spaces; the silhouette comes purely from the count.

Code

A single loop runs over all 2n - 1 rows and picks the count with an if/else: i + 1 on the way up, 2n - 1 - i on the way down. One print statement serves both halves — the direction change lives entirely in the count formula.

def pattern(n): for i in range(2 * n - 1): if i < n: count = i + 1 else: count = 2 * n - 1 - i print(" ".join(["*"] * count))

Key points

  • Rows = 2n - 1; counts run 1, 2, ..., n, ..., 2, 1 with the peak n printed once.
  • Count formula: i + 1 while i < n, else 2n - 1 - i — equivalently min(i + 1, 2n - 1 - i).
  • Contrast Pattern 9: there the widest row repeats; here the peak appears a single time.
  • No space loop at all — the silhouette comes purely from the count.
  • Bounds check: range(2n - 1) rows, and the last row must be a single star.

Suggested questions

Pattern 9 — Diamond Star Pattern The full diamond — this shape's centered, doubled-peak relative.
Pattern 2 — Right-Angled Triangle Pattern Just the climb: the growing triangle is this pattern's first half.
Pattern 20 — Symmetry Pattern The same rise-and-fall count drives the side triangles of the symmetry pattern.
Sum of First N Numbers ↗ Counts 1..n..1 sum to n squared — verify it against this figure's total stars.

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.