A reverse centered alphabet pyramid closes Program 28’s layered square into a full diamond: descend floors from the top letter down to A, then ascend from B back up — same row rule, no duplicated center.
Remember
Upper: i = k..0 Lower: i = 1..k (skip A)
Row: left j = k..0, right j = 1..k
cell = alpha[j] if j > i else alpha[i]
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E ← center once
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E ← top = E (k = 4)
Rows and width are both 2k + 1 (9 for A–E). Program 30 moves on to mixed decreasing/increasing letter rows.
Approach
How to Solve It
Print each floor twice in phases: down through A, then up from B — always with the same left/right j > i row.
Method
Idea
Best for
Two-phase outer loops
Upper k..0 + lower 1..k with shared row body
Matching this classic sample
Helper print_row
Own the floor rule once; call it from both phases
Clearer code; reuse from Program 28
Pseudocode
Pseudocode
k = index of top letter (E → 4)
alpha = "A".."Z"
print_row(i):
for j from k down to 0: // left
print (j > i ? alpha[j] : alpha[i])
for j from 1 to k: // right (skip 0)
print (j > i ? alpha[j] : alpha[i])
print newline
for i from k down to 0: // upper (incl. A)
print_row(i)
for i from 1 to k: // lower (skip A)
print_row(i)
Print cells without a newline, then end the row once.
Try it
Live Preview
Change the size (top letter) and the full reverse-centered pyramid updates instantly — including row count and width.
Whole numbers from 1 to 10. Size 5 means top letter E and 9 rows. Tap a chip or type a value — the preview redraws as you go.
Live result5 letters · top E · 9 rows
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E
Trace
Worked Walkthrough — top = C (k = 2)
Size is 2×2 + 1 = 5 rows and width. Upper includes the center; lower skips it.
Phase
Floor i
Printed row
Upper
2 (C)
C C C C C
Upper
1 (B)
C B B B C
Upper
0 (A)
C B A B C
Lower
1 (B)
C B B B C
Lower
2 (C)
C C C C C
Each of 2k+1 rows prints 2k+1 cells → O(n²) for n = k+1 letters.
Code
Python Programs
Three complete programs: fixed A–E, top-letter input(), and a helper-method style. Use View Output for sample results.
Example 1 — Fixed A–E
Same row logic as Program 28, printed in two phases to complete the diamond.
Python
k = ord("E") - ord("A")
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# Upper half (E down to A)
for i in range(k, -1, -1):
for j in range(k, -1, -1):
print(alpha[j] if j > i else alpha[i], end=" ")
for j in range(1, k + 1):
print(alpha[j] if j > i else alpha[i], end=" ")
print()
# Lower half (B up to E) — skip repeating the A row
for i in range(1, k + 1):
for j in range(k, -1, -1):
print(alpha[j] if j > i else alpha[i], end=" ")
for j in range(1, k + 1):
print(alpha[j] if j > i else alpha[i], end=" ")
print()
Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E
How It Works
1. Upper phase. Floors run from k down to 0 — exactly Program 28, ending on the A-center row.
2. Lower phase. Floors run from 1 (B) up to k. Starting at 1 avoids a second center line.
3. Same cell rule. Both phases use left k..0, right 1..k, and j > i ? alpha[j] : alpha[i].
Example 2 — Top Letter Input
Works for A..top with the same two-phase pyramid. Validate a single A–Z character.
Python
raw = input("Enter top letter (like E): ").strip().upper()
if len(raw) != 1 or not ("A" <= raw <= "Z"):
print("Please enter a single letter A-Z.")
raise SystemExit(1)
k = ord(raw) - ord("A")
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(k, -1, -1):
for j in range(k, -1, -1):
print(alpha[j] if j > i else alpha[i], end=" ")
for j in range(1, k + 1):
print(alpha[j] if j > i else alpha[i], end=" ")
print()
for i in range(1, k + 1):
for j in range(k, -1, -1):
print(alpha[j] if j > i else alpha[i], end=" ")
for j in range(1, k + 1):
print(alpha[j] if j > i else alpha[i], end=" ")
print()
Output (when user enters C)
C C C C C
C B B B C
C B A B C
C B B B C
C C C C C
How It Works
1. Prompt and validate. Strip, uppercase, and require a single A–Z letter.
2. Scale with k. Both phases and both halves use the same k. For top = C you get 5 rows of width 5 (2k+1).
Example 3 — Helper Method
Often clearer: one method owns the floor rule; another prints a full row so both phases stay thin.
Python
def print_cell(alpha, j, i):
print(alpha[j] if j > i else alpha[i], end=" ")
def print_row(alpha, k, i):
for j in range(k, -1, -1):
print_cell(alpha, j, i)
for j in range(1, k + 1):
print_cell(alpha, j, i)
print()
k = ord("E") - ord("A")
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(k, -1, -1):
print_row(alpha, k, i)
for i in range(1, k + 1):
print_row(alpha, k, i)
Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E
How It Works
1. One rule owner.print_cell owns the j > i choice; print_row owns both halves.
2. Thin phases. The two outer loops only decide which floors to visit — handy when you already built Program 28’s row helper.
Edge Cases & Pitfalls
Check these before calling the solution done.
double center
Lower phase starts at 0
Starting the lower scan at i = 0 duplicates the A-center row. Keep range(1, k + 1).
open square
Missing lower phase
Forgetting the second outer loop leaves Program 28’s open square. Add floors 1..k.
double A
Right half starts at 0
Inside each row, starting the right scan at j = 0 duplicates the middle A. Keep range(1, k + 1).
newline early
Bare print() inside halves
Use end=" " for cells; call bare print() only after both halves finish.
top = A
Single A
Output is just A (lower half empty) — a good sanity check.
Bad input
Validate one letter
Reject empty strings and multi-character input before computing k.
Analysis
Time and Space Complexity
Program
Time
Extra space
Inline phases (Examples 1–2)
O(n²)
O(1) beyond the alphabet string
Helper method (Example 3)
O(n²)
O(1) beyond the alphabet string
There are 2k+1 rows and each prints 2k+1 cells, so total work is quadratic in the number of letters n = k+1.
Remember
Key Takeaways
Reuse: Program 28’s row is the upper half; add floors 1..k to close the diamond.
Skip A: lower phase starts at 1 so the center row prints once.
Size: rows and width are both 2k + 1 (9 for A–E).
Next step: Program 30 shifts to mixed decreasing/increasing letter rows.
One line: print floors k..0 then 1..k with Program 28’s left/right j > i row rule.
Frequently Asked Questions
The first loop decreases i from k down to 0, printing each layered row through the A-center. The second increases i from 1 back to k with the same row rule so the pyramid widens again without repeating the center.
Because the A-centered row already appears in the upper half. Starting from 1 prevents duplicating the center line.
If n is the number of letters from A to the top letter (n = k+1), total rows are 2k+1 — the same as the row width. For A..E (k=4), that is 9 rows.
It prints the border letter when column index j is above the current row floor i; otherwise it prints the floor letter. The same rule applies on both left and right halves of every row.
The left scan goes k down to 0; the right scan goes 1 up to k so the middle A appears once and the row mirrors.
O(n²) because there are O(n) rows and each row prints O(n) cells.
Read a line, strip it, call .upper(), require a single A–Z character, and reject empty or multi-character input.
Program 28 is exactly the upper half of this pyramid. Program 29 reuses that row logic, then mirrors upward from B to E for the closed diamond.
🤔
Did you know?
Reuse Program 28's row logic twice: first with i from k down to 0 (A), then with i from 1 (B) up to k so the center row is not duplicated. Each row stays full width (2k+1); total rows are also 2k+1.