A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 17 — Alpha-Hill Pattern

Easy

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
Output for n = 5

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.

Suggested questions

Pattern 7 — Star Pyramid The star pyramid with the identical space/star formulas — swap stars for hills of letters.
Pattern 16 — Alpha-Ramp Pattern Letters driven purely by the row index; here they also vary within the row.
Pattern 9 — Diamond Star Pattern Stack a pyramid on its inverse; the alpha-hill is the single-pyramid letter analogue.
Check if String is Palindrome or Not ↗ Every row of this pattern is a palindrome — this problem checks that property directly.

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.