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