A sequential decreasing alphabet triangle prints one continuous letter stream across rows that shrink by one letter each time — never restarting at A.
Remember
Rule: keep code at ord('A') outside the outer loop;
row i prints (n - i + 1) letters, then code += 1 each time
A B C D E
F G H I
J K L
M N
O ← 5 rows (15 letters, A through O)
Companion to Program 22: same continuous letter stream, but this pattern shrinks each row and stays left-aligned instead of growing with pad cells. Compare also Program 13 (growing continuous) and Program 5 (decreasing but resets to A).
Approach
How to Solve It
Two ways to emit the same stream — countdown inner bounds, or an explicit letters-per-row count.
Method
Idea
Best for
Countdown inner
j from n down to i; print next letter
Learning, interviews, exams
Explicit count
count = n - i + 1; loop count times
When the shrink rule should read clearly
Pseudocode
Pseudocode
code = ord('A')
for i from 1 to n:
for j from n down to i:
print chr(code); code = code + 1
print newline
Print letters without a newline, then end the row once.
Try it
Live Preview
Change the height and the shrinking stream updates instantly — including the letter total.
Whole numbers from 1 to 6 so the stream stays in A–Z (n(n+1)/2 ≤ 21 for n = 6).
Live result5 rows · 15 letters
A B C D E
F G H I
J K L
M N
O
Trace
Worked Walkthrough — 5 rows
Trace each row’s length and the letters taken from the running counter.
i
Letters
Count
Printed row
1
A..E
5
A B C D E
2
F..I
4
F G H I
3
J..L
3
J K L
4
M..N
2
M N
5
O
1
O
Total letters: 5 + 4 + 3 + 2 + 1 = 15 = 5×6/2 (A through O). That triangular sum is why time is O(n²).
Code
Python Programs
Three complete programs: fixed 5 rows, input() row-count, and an explicit count-per-row rewrite. Use View Output to reveal sample results.
Example 1 — Fixed 5 Rows
One counter code supplies letters; the inner loop decides how many times to print it in each row.
Python
code = ord("A")
for i in range(1, 6):
for j in range(5, i - 1, -1):
print(chr(code), end=" ")
code += 1
print()
Output
A B C D E
F G H I
J K L
M N
O
How It Works
1. Start the stream.code = ord("A") lives outside the outer loop so it never resets.
2. Outer loop picks the row.i runs from 1 to 5.
3. Inner loop shrinks.j runs from 5 down to i — five letters, then four, then three, …
4. Print and advance.print(chr(code), end=" ") prints the next letter with a trailing space, then code += 1.
When i = 3, the inner loop runs three times and prints J K L; the next row continues at M.
Example 2 — Row Count Input
If rows are large, letters go past Z unless you wrap or cap. Prefer try/except ValueError and a letter-budget check.
Python
n = int(input("Enter number of rows (like 5): "))
if n < 1:
raise SystemExit
code = ord("A")
for i in range(1, n + 1):
for j in range(n, i - 1, -1):
print(chr(code), end=" ")
code += 1
print()
Output (when user enters 3)
Enter number of rows (like 5): 3
A B C
D E
F
How It Works
1. Prompt and read. Ask for a row count, then convert with int().
2. Same stream and shrink. Only the shared width follows n — letter count is n(n+1)/2.
3. Safer input tip. Prefer:
Safer input
try:
n = int(input("Enter number of rows (like 5): "))
except ValueError:
print("Enter a positive whole number.")
raise SystemExit
if n < 1:
print("Enter a positive whole number.")
raise SystemExit
if n * (n + 1) // 2 > 26:
print("Too many letters for A–Z. Use a smaller n.")
raise SystemExit
Example 3 — Explicit Count Per Row
Often clearer to read: print n - i + 1 letters from the shared stream.
Python
n = 5
code = ord("A")
for i in range(1, n + 1):
count = n - i + 1
for _ in range(count):
print(chr(code), end=" ")
code += 1
print()
Output
A B C D E
F G H I
J K L
M N
O
How It Works
1. Same continuous stream.code still starts at ord("A") outside the outer loop.
2. Name the width.count = n - i + 1 makes the shrink rule obvious — row 1 prints 5 letters; row 5 prints 1.
3. Optional tidy rows. Collect letters in a list and use " ".join(row) if you want no trailing space.
Edge Cases & Pitfalls
Check these before calling the solution done.
Reset code
Every row starts at A
If you put code = ord("A") inside the outer loop, you lose the continuous stream. Keep code outside.
j = 1..i
Growing triangle by mistake
An ascending inner bound reprints a growing shape. Use j from n down to i (or count = n - i + 1).
print() inside
Column of letters
If bare print() is inside the inner loop, each letter lands on its own line. Use end=" " for letters; call print() only after the inner loop.
n = 1
Single A
Output is just A. A good sanity check.
Past Z
Letter budget
Total letters = n(n+1)/2. For A–Z only, keep that ≤ 26 (so n ≤ 6 without wrapping).
Bad input
Use try/except
int(input()) raises on letters — prefer try/except ValueError and require n ≥ 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Countdown inner (Examples 1–2)
O(n²)
O(1)
Explicit count (Example 3)
O(n²)
O(1)
Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — still quadratic in n. Same totals as an inverted triangle of stars; only the printed symbols differ.
Remember
Key Takeaways
Rule: keep code outside; row i prints n - i + 1 letters from the stream.
Never reset: putting code = ord("A") inside the outer loop restarts every row.
Break the row: call print() only after the inner loop.
Complexity:O(n²) time from the triangular letter count; O(1) extra space.
One line: for each row i, print n - i + 1 letters from the shared stream, then call print().
Frequently Asked Questions
Because the pattern is one continuous alphabet stream A through O. Resetting the counter would restart each row from A.
So the sequence continues across rows (A, B, C, ...). If you reset code each row, every row would start at A again.
For the fixed 5-row example, the row lengths are 5, 4, 3, 2, 1. Each new row prints one fewer letter.
end=" " keeps letters on the same line with a space between them. Bare print() after the inner loop ends the row.
Build a row list and use " ".join(row), or print a space only between letters (not after the last letter in the row).
Both use a continuous letter stream. Program 22 grows rows and right-aligns with pad cells; this pattern shrinks row length each time and stays left-aligned.
O(n²) for n rows because total prints are 1+2+...+n = n(n+1)/2.
Use try/except ValueError around int(input()), require n ≥ 1, and cap so n(n+1)/2 ≤ 26 if you want only A–Z letters.
🤔
Did you know?
One counter starts at ord('A') and never resets. The outer loop controls row count; the inner loop length decreases each row (5, 4, 3, 2, 1). A trailing space after each letter keeps a clean column layout in monospace output.