Step 01 Beginner Problems — Patterns
Pattern 4 — Right-Angled Number Pyramid II
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
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.