A rotating alphabet pattern reprints the same letter set each row, starting one letter further along and wrapping earlier letters in reverse so every line stays the same length.
Remember
Rule: for start i, print i..top, then reverse wrap of A..(i-1)
ABCDE
BCDEA
CDEBA
DECBA
EDCBA ← top = 'E' (5 letters each row)
Not a pure left rotation: after BCDEA you get CDEBA (reverse wrap), not CDEAB. Print k - 1 on the wrap loop so the row-start letter is not duplicated.
Approach
How to Solve It
Two ways to emit the same rows — nested index loops, then optionally a suffix + reverse-prefix rewrite.
Method
Idea
Best for
Forward + wrap
Print i..top, then wrap with k - 1
Learning, interviews, exams
String slice
letters[i:] + letters[:i][::-1]
Readable rewrite once the idea clicks
Pseudocode
Pseudocode
for i from 'A' to top:
for j from i to top:
print j (no newline)
for k from i down to 'B':
print (k - 1) (no newline)
print newline
Cheat sheet
Goal
Pattern
Letter table
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Row starts
for i in range(5): (A..E)
Forward run
for j in range(i, 5): print(letters[j], end="")
Reverse wrap
for k in range(i, 0, -1): print(letters[k - 1], end="")
End the row
print()
Row length
ord(top) - ord("A") + 1 (same on every row)
Avoid double start
Print k - 1, not k, on the wrap loop
Printing Letters vs Starting a New Line
API
Effect
Use for
print(..., end="")
Stays on the same line
Each letter on forward and wrap
print()
Ends the current line
After both inner loops
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the top letter and the rotating rows update instantly — every line stays the same length.
One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.
Live resultTop E · 5 rows · 5 letters/row
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
Trace
Worked Walkthrough — A–E
Trace each row’s forward run, reverse wrap, and full line.
i
Forward
Wrap (rev)
Printed row
0 (A)
ABCDE
(empty)
ABCDE
1 (B)
BCDE
A
BCDEA
2 (C)
CDE
BA
CDEBA
3 (D)
DE
CBA
DECBA
4 (E)
E
DCBA
EDCBA
Every row length is 5. Note the wrap is the reverse of the prefix — row 3 is CDEBA, not CDEAB.
Code
Python Programs
Three complete programs: fixed A–E, top-letter input, and a string-slice rewrite. Use View Output to reveal sample results.
Example 1 — Fixed A–E
Forward run i..E plus wrap run using k - 1.
Python
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(5):
for j in range(i, 5):
print(letters[j], end="")
for k in range(i, 0, -1):
print(letters[k - 1], end="")
print()
Output
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
How It Works
1. Index the alphabet.letters[0] is A, letters[4] is E.
2. Outer loop picks the start.i runs from 0 to 4 — one start letter per row.
3. Forward run. Print letters[j] from i through 4.
4. Reverse wrap. Count k down from i and print letters[k - 1] so the start letter is not repeated.
When i = 2 (letter C), forward prints CDE and wrap prints BA → CDEBA.
Example 2 — Top Letter Input
Same forward + wrap rules with ord/chr loops. Prefer validating a single A–Z character in real apps.
Python
top = input("Enter top letter (like E): ").strip().upper()
if len(top) != 1 or not ("A" <= top <= "Z"):
print("Please enter a single letter.")
raise SystemExit
top = ord(top)
for i in range(ord("A"), top + 1):
for j in range(i, top + 1):
print(chr(j), end="")
for k in range(i, ord("A"), -1):
print(chr(k - 1), end="")
print()
Output (when user enters D)
Enter top letter (like E): D
ABCD
BCDA
CDBA
DCBA
How It Works
1. Prompt and read. Ask for a top letter, strip whitespace, and uppercase it.
2. Same wing rules. Forward is i..top; wrap uses chr(k - 1) so the join stays clean.
3. Safer input tip. Prefer:
Safer input
raw = input("Enter top letter (like E): ").strip().upper()
if len(raw) != 1 or not ("A" <= raw <= "Z"):
print("Enter one letter from A to Z.")
raise SystemExit
top = ord(raw)
Example 3 — String Slice Rewrite
Often clearer to read: take the suffix from the start index, then append the reverse of the prefix.
Python
letters = "ABCDE"
for i in range(len(letters)):
print(letters[i:] + letters[:i][::-1])
Output
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
How It Works
1. Slice the suffix.letters[i:] is the forward run from the start index.
2. Reverse the prefix.letters[:i][::-1] reverses letters before i — same wrap as Examples 1–2.
3. One print. Concatenate and print the full row in a single call.
For i = 2, forward is CDE and reversed prefix is BA → CDEBA.
Edge Cases & Pitfalls
Check these before calling the solution done.
Print k
Duplicate start letter
If the wrap loop prints k instead of k - 1, rows like BCDEB repeat the start. Always use k - 1.
Forward wrap
Pure left rotation
Appending the prefix in order gives CDEAB, not CDEBA. This pattern reverses the wrap.
print() inside
Column of letters
If bare print() is inside either inner loop, each letter lands on its own line. Use end="" for letters; call print() only after both loops.
top = A
Single letter
Output is just A — wrap never runs. A good sanity check.
top < A
Empty output
Outer loop never runs if top < ord("A"). Validate A–Z before looping.
Bad input
Validate one letter
Strip, uppercase, and require length 1 in A–Z — reject empty or multi-character tokens.
Analysis
Time and Space Complexity
Program
Time
Extra space
Forward + wrap (Examples 1–2)
O(n²)
O(1) (plus the fixed alphabet string)
String slice (Example 3)
O(n²)
O(n) per temporary row string
For n = ord(top) - ord("A") + 1 letters, each of n rows prints n characters — still quadratic in n.
Remember
Key Takeaways
Rule: for start i, print i..top, then reverse wrap of A..(i-1).
No duplicate start: wrap with k - 1, not k.
Break the row: call print() only after both loops.
Complexity:O(n²) time; O(1) extra space for nested loops.
One line: for each start i, print i..top, then the reverse of A..(i-1), then print().
Frequently Asked Questions
The first loop prints from the row start through E (or your top letter). The second prints the letters before the start (wrapping) in reverse using k-1 so the boundary letter is not duplicated.
When k equals i, printing k would repeat the first letter of the row. Printing k-1 starts the wrap with the previous letter (e.g. BCDE then A gives BCDEA).
Each row prints from the row start to the end, then prints earlier letters in reverse to complete the row. That creates the rotation effect.
Yes. Forward length plus wrap length equals top−A+1, so each row has the same number of letters.
Not exactly. A pure left rotation of ABCDE would give BCDEA, CDEAB, DEABC, EABCD. This pattern wraps earlier letters in reverse, so you get CDEBA, DECBA, EDCBA.
print(..., end="") stays on the same line. Bare print() ends the current line. Letters use end=""; the row break uses print() after both inner loops.
For n letters, each of n rows prints n characters, so O(n²).
Strip and upper the input, require a single A–Z character, and reject empty or multi-character tokens.
🤔
Did you know?
Outer i is the row start. First inner loop prints i through E. Second inner loop wraps by counting down from i and printing k - 1 (not k) so the boundary letter isn’t duplicated. Every row prints the same length: E - A + 1 letters.