Mixed alphabet rows keep a fixed width: each row starts with a growing descending prefix, then fills the rest with a shrinking ascending suffix — no cyclic wrap.
Row 1 is pure ascending (ABCDE); the last row is pure descending (EDCBA). Compare Program 26 (rotating wrap) and Program 29 (layered diamond).
Approach
How to Solve It
For each row index r, print start..A descending, then B..end ascending.
Method
Idea
Best for
Two loops inline
Descending prefix, then ascending suffix with end=""
Learning, interviews, exams
List + join
Append letters, then ''.join(row)
Debugging; inspect the row before print
Pseudocode
Pseudocode
base = 'A'
top = base + rows - 1
for r from 0 to rows - 1:
start = base + r
end = top - r
for code from start down to base: // prefix
print code
for code from base+1 to end: // suffix (skip A)
print code
print newline
Cheat sheet
Goal
Pattern
Top letter
top = ord("A") + rows - 1
Walk rows
for r in range(rows):
Bounds
start = base + r, end = top - r
Descending prefix
for code in range(start, base - 1, -1):
Ascending suffix
for code in range(base + 1, end + 1): — start at B
Width
Always rows letters per line
End the row
print()
vs Program 26
No wrap — BABCD, not BCDEA
Printing Letters vs Starting a New Line
API
Effect
Use for
print(chr(code), end="")
Stays on the same row
Each letter in prefix and suffix
print()
Ends the current row
After both inner loops
Print letters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the mixed alphabet grid updates instantly — every line stays the same width.
Whole numbers from 1 to 10. Size 5 means letters A–E and width 5. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · width 5
ABCDE
BABCD
CBABC
DCBAB
EDCBA
Trace
Worked Walkthrough — rows = 3
top = C. Trace prefix and suffix for each row index r.
r
Prefix / suffix
Printed row
0
A + BC
ABC
1
BA + C
BAC
2
CBA + (empty)
CBA
Each of n rows prints n letters → O(n²).
Code
Python Programs
Three complete programs: fixed height, row-count input(), and a list-join style. Use View Output for sample results.
Example 1 — Fixed rows = 5
Descending prefix to A, then ascending suffix from B to a shrinking end.
Python
rows = 5
base = ord("A")
top = base + rows - 1
for r in range(rows):
start = base + r
end = top - r
for code in range(start, base - 1, -1):
print(chr(code), end="")
for code in range(base + 1, end + 1):
print(chr(code), end="")
print()
Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
How It Works
1. Bounds.start = base + r grows the prefix; end = top - r shrinks the suffix.
2. Prefix. Print start..A descending. On row 0 that is just A.
3. Suffix. Print B..end. When end < B (last row), the suffix is empty → EDCBA.
Example 2 — Row Count Input
Read how many rows to print. Cap at 26 so letters stay in A–Z.
Python
raw = input("Enter number of rows (1-26): ").strip()
try:
rows = int(raw)
except ValueError:
print("Please enter a whole number.")
raise SystemExit(1)
rows = max(1, min(rows, 26))
base = ord("A")
top = base + rows - 1
for r in range(rows):
start = base + r
end = top - r
for code in range(start, base - 1, -1):
print(chr(code), end="")
for code in range(base + 1, end + 1):
print(chr(code), end="")
print()
Output (when user enters 3)
ABC
BAC
CBA
How It Works
1. Validate rows. Parse an integer, then clamp to 1..26.
2. Same dual loops. Three rows use A–C with width 3 — row 2 is BAC, not cyclic BCA.
Example 3 — List Join
Collect letters in a list, then print with ''.join(row) — same shape, easier to inspect.
Python
rows = 5
base = ord("A")
top = base + rows - 1
for r in range(rows):
start = base + r
end = top - r
row = []
for code in range(start, base - 1, -1):
row.append(chr(code))
for code in range(base + 1, end + 1):
row.append(chr(code))
print("".join(row))
Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
How It Works
1. Build then print. Both loops append to row instead of printing immediately.
2. Join once."".join(row) concatenates without spaces — identical output to Example 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
double A
Suffix starts at A
Starting the second loop at base duplicates A (e.g. BAAC). Keep range(base + 1, end + 1).
wrap habit
Confusing with Program 26
This is not a rotate: row 2 is BABCD, not BCDEA.
newline early
Bare print() inside a half
Use end="" for letters; call bare print() only after both loops.
rows > 26
Top past Z
Cap rows at 26 so ord("A") + rows - 1 stays in A–Z.
rows = 1
Single A
Prefix is A; suffix is empty because end < B.
Bad input
Non-integer rows
Catch ValueError from int(...) before computing top.
Analysis
Time and Space Complexity
Program
Time
Extra space
Inline print (Examples 1–2)
O(n²)
O(1)
List join (Example 3)
O(n²)
O(n) per row for the list
There are n rows and each prints n letters, so total work is quadratic.
Remember
Key Takeaways
Two passes: descending start..A, then ascending B..end.
Fixed width: as the prefix grows, the suffix shrinks — every row has n letters.
Skip A: suffix starts at B so the join does not print AA.
Next step: Program 31 draws an alphabet X with diagonals.
One line: for each r, print start..A down then B..end up with start = A+r and end = top−r.
Frequently Asked Questions
Each row combines a descending prefix from the row start letter down to A with an ascending suffix from B up to a shrinking end letter. Row 1 is ABCDE; row 2 is BABCD; row 5 is EDCBA.
Program 26 rotates fixed-width rows with wrap-around (ABCDE, BCDEA). Here the prefix descends and the suffix ascends without cyclic wrap — BABCD, not BCDEA.
The first loop prints the descending prefix from start down to A. The second prints the ascending suffix from B to end — skipping A avoids duplicating the letter at the join point.
A is already printed as the last character of the descending prefix (except row 1 where start is A). Starting the suffix at B prevents AA in rows like BAC.
Each row uses letters A through the nth letter. With rows > 26 you would need characters beyond Z unless you define a wrap policy.
print(chr(code), end="") keeps letters on the same row. print() ends the row after both inner loops finish.
O(n²) where n is rows. There are n rows and each row prints n characters.
Use try/except ValueError around int(input()), then clamp rows between 1 and 26.
🤔
Did you know?
Each row uses two passes: a descending prefix from the row start letter down to A, then an ascending suffix from B up to end = top - r. Row 1 is pure ascending ABCDE; the last row is pure descending EDCBA.