Step 01 Beginner Problems — Patterns
Pattern 5 — Inverted Right Pyramid
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.
* * * * * * * * * * * * * * *
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.