Step 01 Beginner Problems — Patterns
Pattern 18 — Alpha-Triangle Pattern
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
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.