Step 01 Beginner Problems — Patterns
Pattern 17 — Alpha-Hill Pattern
A hill of letters, centered like the star pyramid: each row climbs A, B, C, ... up to a peak letter and then descends symmetrically back to A, so every row is a palindrome. Row widths are the odd numbers 1, 3, 5, ..., 2n - 1, matching Pattern 7's skeleton.
A
A B A
A B C B A
A B C D C B A
A B C D E D C B A
How to think about it
Number the rows i = 0, 1, ..., n-1. Row i begins with n - i - 1 blank slots (the same centering as Pattern 7), then prints 2i + 1 letters: an ascending run A, B, ..., chr(ord('A') + i) of length i + 1, followed by a descending run of length i that walks back down to A. The peak letter of row i is the (i+1)-th letter, and it appears exactly once, at the center. The bottom row (i = n - 1) climbs to the n-th letter and returns, spanning 2n - 1 letters with no indentation.
Code
The code builds the ascending list [chr(ord('A') + j) for j in range(i + 1)] and the descending list for j from i - 1 down to 0, concatenates them, and prefixes the indent ' ' * (n - i - 1). Splitting the row into an up-run and a down-run makes the palindrome structure explicit and avoids tracking a direction flag inside one loop.
def pattern(n):
for i in range(n):
indent = " " * (n - i - 1)
up = [chr(ord("A") + j) for j in range(i + 1)]
down = [chr(ord("A") + j) for j in range(i - 1, -1, -1)]
print(indent + " ".join(up + down))Key points
- Row i: blanks = n - i - 1, letters = 2i + 1 (i + 1 ascending, then i descending).
- The peak letter chr(ord('A') + i) appears once, exactly at the center.
- Every row is a palindrome — read it backwards to check your descending run.
- Same centering math as Pattern 7; only the token generator changed.
- Common bug: starting the descent at the peak itself (duplicating it) — start at i - 1 instead.