A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 5 — Inverted Right Pyramid

Easy

Pattern 2 flipped upside down: the first row is the full base of n stars and each following row loses one star, until the last row has a single star. It is the fundamental shrinking triangle — the inner count is driven by how many rows remain, not by how many have passed.

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

How to think about it

Number the rows i = 0, 1, ..., n-1. Row i contains exactly n - i stars and no leading spaces. At i = 0 that gives n stars (the full width); at i = n - 1 it gives 1 star. The outer loop still runs n times; only the inner count formula changes, from Pattern 2's i + 1 to n - i. Together, i + 1 and n - i are the two workhorse formulas of this whole series — growing and shrinking.

Code

For each row the code prints a list of n - i '*' tokens joined by spaces. As i climbs, the list shrinks by one each iteration, which is exactly the picture. No indentation is added, so the triangle stays glued to the left margin while its right edge retreats.

def pattern(n): for i in range(n): print(" ".join(["*"] * (n - i)))

Key points

  • Master formula: row i prints n - i stars — count what is left, not what is done.
  • First row must be the full n stars; the last row exactly 1.
  • Growing (i + 1) and shrinking (n - i) counts are the two core formulas of the series.
  • Left-aligned shrinking needs no spaces; centered shrinking (Pattern 8) adds the space formula.
  • Total stars printed: n + (n-1) + ... + 1 = n(n+1)/2.

Suggested questions

Pattern 2 — Right-Angled Triangle Pattern The growing twin — run both back to back and you have the skeleton of the half diamond.
Pattern 6 — Inverted Numbered Right Pyramid The same shrinking counts with the numbers 1..n-i instead of stars.
Pattern 8 — Inverted Star Pyramid Shrinking plus centering — the inverted pyramid adds the leading-space formula.
Reverse a number ↗ Works a quantity down step by step until one digit remains — the same countdown mindset.

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.