A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 4 — Right-Angled Number Pyramid II

Easy

A growing triangle where every entry in a row is the same: the row number. Row 1 is just '1', row 2 is '2 2', row 3 is '3 3 3' and so on — Pattern 3 with each row flattened to a single repeated value.

1
2 2
3 3 3
4 4 4 4
5 5 5 5 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 every entry equals i + 1 (the 1-based row number). Two facts combine: the count comes from the row index (i + 1 copies) and the value also comes from the row index (the token str(i + 1)). There is no inner variation at all — the inner loop is just a repetition. Compare Pattern 3, where the value came from the column j instead: same skeleton, different source for the token.

Code

The code repeats the single token str(i + 1) exactly i + 1 times in a list and prints it joined by spaces. Because the token never changes within a row, list multiplication [token] * count replaces the inner loop entirely.

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

Key points

  • Row i: value = i + 1 and count = i + 1 — both come from the row index.
  • When a row is one repeated token, list multiplication [tok] * k replaces the inner loop.
  • Contrast with Pattern 3: there the value came from the column index j + 1.
  • Watch the +1s: with 0-based rows the printed number is i + 1, not i — row 0 must print '1', never '0'.
  • The same trick powers Pattern 16 (Alpha-Ramp), with letters instead of digits.

Suggested questions

Pattern 3 — Right-Angled Number Pyramid The sibling triangle where values come from the column instead of the row.
Pattern 16 — Alpha-Ramp Pattern Identical structure with letters — the row's own letter repeated across the row.
Pattern 6 — Inverted Numbered Right Pyramid Rows shrink from n entries to 1 — practice the n - i count with numbers.
Factorial of a given number ↗ Both lean on building a result step by step from the numbers 1..n in order.

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.