A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 6 — Inverted Numbered Right Pyramid

Easy

The numbered counterpart of Pattern 5: the top row lists 1 through n, and each row below drops the largest remaining number, ending with a lone '1'. It drills the shrinking count n - i together with values generated from the column index.

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Output for n = 5

How to think about it

Number the rows i = 0, 1, ..., n-1. Row i has n - i entries, and the entries are 1, 2, ..., n-i — so the entry at column j is j + 1, and the row simply stops earlier each time. The outer loop decides the length (n - i); the inner loop fills values from the column (j + 1). Row 0 is the complete sequence 1..n; the final row (i = n - 1) has length 1 and shows just 1.

Code

For each row the code builds [str(j + 1) for j in range(n - i)] — the numbers from 1 up to the current row length — and prints them joined. Changing only the range bound from Pattern 3's i + 1 to n - i converts the growing triangle into this shrinking one.

def pattern(n): for i in range(n): nums = [str(j + 1) for j in range(n - i)] print(" ".join(nums))

Key points

  • Row i: length = n - i and values = 1..(n-i) — both shrink together.
  • The last number printed in row i equals the row's length — an easy dry-run check.
  • Patterns 3 and 6 differ by a single bound: i + 1 grows, n - i shrinks.
  • Values restart at 1 on every row; nothing carries over between rows.
  • Common slip: using n - i for the count but printing up to n — keep the two in sync.

Suggested questions

Pattern 3 — Right-Angled Number Pyramid The growing version of this exact numbered triangle.
Pattern 5 — Inverted Right Pyramid The same shrink formula with stars — confirms you own the n - i count.
Pattern 15 — Reverse Letter Triangle Pattern Shrinking rows of letters A.. — the alphabetic mirror of this pattern.
Palindrome Number ↗ Number manipulation with loops — a natural next drill after shrinking 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.