A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 8 — Inverted Star Pyramid

Easy

The star pyramid of Pattern 7 standing on its head: the first row is the full base of 2n - 1 stars and each row below sheds two stars — one from each side — until a single star remains at the bottom tip. The leading spaces grow exactly as fast as the stars shrink.

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

How to think about it

Number the rows i = 0, 1, ..., n-1. Row i prints i blank slots followed by 2(n - i) - 1 stars. At i = 0: no blanks, 2n - 1 stars. At i = n - 1: n - 1 blanks and exactly 1 star. The star formula 2(n - i) - 1 is just Pattern 7's 2i + 1 run backwards, and the blank count i is Pattern 7's shrinking blank count run forwards. Symmetry around the same center column is preserved on every row.

Code

Each row is ' ' * i for the indent (two characters per empty slot, matching the token width) plus 2(n - i) - 1 joined '*' tokens. The two quantities are computed from the same i, so the rows narrow and drift right in lockstep.

def pattern(n): for i in range(n): indent = " " * i stars = " ".join(["*"] * (2 * (n - i) - 1)) print(indent + stars)

Key points

  • Row i: blanks = i, stars = 2(n - i) - 1.
  • Widths step down through the odd numbers: 2n-1, 2n-3, ..., 1.
  • It is Pattern 7 with both formulas reversed — learn one, derive the other.
  • The final row is n - 1 blank slots and a single star, sitting on the center column.
  • Off-by-one trap: 2(n - i) - 1, not 2(n - i) + 1 — test with n = 1, which must print a single star.

Suggested questions

Pattern 7 — Star Pyramid The upright pyramid — the direct twin of this one.
Pattern 9 — Diamond Star Pattern Join this to Pattern 7 and the diamond appears.
Pattern 5 — Inverted Right Pyramid Shrinking without centering — compare how the missing space loop changes the silhouette.
Fibonacci Number ↗ Rows here step by a constant difference; Fibonacci steps by the sum of the previous two — a good contrast in sequences.

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.