A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 3 — Right-Angled Number Pyramid

Easy

The growing star triangle of Pattern 2 re-skinned with numbers: every row lists the counting numbers 1, 2, 3, ... up to the row's length. It teaches you to generate a row's tokens from a formula on the position inside the row (the column j) instead of repeating one fixed token.

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

How to think about it

Number the rows i = 0, 1, ..., n-1. Row i has i + 1 entries, and the entries are exactly 1, 2, 3, ..., i+1 — so the entry in column j (0-based) of row i is always j + 1, independent of the row. The outer loop fixes the row length (i + 1); the inner loop walks j from 0 to i and emits str(j + 1). The last entry of row i is i + 1, which is also the row's length — a handy self-check while dry-running.

Code

The inner list comprehension builds [str(j + 1) for j in range(i + 1)] — the numbers 1 through i + 1 as strings — and the row is printed joined by spaces. Converting to str before joining is essential: you cannot join integers with a string separator.

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

Key points

  • Row i holds i + 1 numbers; the value at column j is j + 1 regardless of the row.
  • Two loops, two jobs: the outer sets the length, the inner generates values from the column index.
  • Every row restarts at 1 — the counter is the column, not a running total (contrast Pattern 13).
  • Convert numbers to strings before ' '.join, or Python raises a TypeError.
  • Last value of row i equals i + 1 — use it as a quick correctness check.

Suggested questions

Pattern 4 — Right-Angled Number Pyramid II Same triangle shape, but every entry in a row becomes the row number itself.
Pattern 6 — Inverted Numbered Right Pyramid The shrinking version — rows count down from the full 1..n line.
Pattern 13 — Increasing Number Triangle Pattern The counter stops resetting each row and keeps running across the whole figure (Floyd's triangle).
Count all Digits of a Number ↗ Another loop that walks positions one by one and extracts a value per position.

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.