A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 18 — Alpha-Triangle Pattern

Easy

A letter triangle where each row slides one step earlier in the alphabet: the top row is the single last letter (E for n = 5), and every row below starts one letter sooner and runs up to that same final letter, ending with the full run A B C D E. It is a growing triangle whose starting value — not just its length — moves with the row.

E
D E
C D E
B C D E
A B C D E
Output for n = 5

How to think about it

Number the rows i = 0, 1, ..., n-1. Row i has i + 1 letters, ending at the n-th letter chr(ord('A') + n - 1) and starting at chr(ord('A') + n - 1 - i). So the start letter walks backwards down the alphabet as i grows: for n = 5 the rows start at E, D, C, B, A. Within a row the letters simply ascend by one from the start. Two quantities move together — length = i + 1 and start offset = n - 1 - i — and their sum is always n - 1, which is why every row ends at the same final letter.

Code

The code computes the row's starting character code, start = ord('A') + n - 1 - i, then generates i + 1 consecutive letters from it. All rows share the same right edge (the n-th letter); only the left edge moves — that is the whole design.

def pattern(n): for i in range(n): start = ord("A") + n - 1 - i letters = [chr(start + j) for j in range(i + 1)] print(" ".join(letters))

Key points

  • Row i: start = chr(ord('A') + n - 1 - i), length = i + 1, and the end is always the n-th letter.
  • Start offset and length sum to n - 1 on every row — the invariant that keeps the right edge fixed.
  • Unlike Pattern 14, rows do not restart at A; each starts one letter earlier than the row above.
  • Read the figure diagonally: each descending diagonal is a constant letter.
  • The top row is a single letter — the n-th one, not A. Getting E (for n = 5) confirms the offset math.

Suggested questions

Pattern 15 — Reverse Letter Triangle Pattern Letters shrink from a fixed start at A; here they grow toward a fixed end.
Pattern 14 — Increasing Letter Triangle Pattern The basic letter triangle — every row restarts at A there.
Pattern 17 — Alpha-Hill Pattern Letters climb to a peak and come back down within the row.
Reverse a String II ↗ Manipulating character sequences by position — the skill this pattern exercises.

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.