A mirror diagonal diamond extends Program 53’s V-shape: print the top half from 1 to rows, then mirror the same row logic from rows - 1 back to 1 — total 2n - 1 lines.
Remember
Rule: top i = 1..rows
bottom i = rows-1..1
each row: left i == j, right i == k
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1 ← rows = 5
In Python reuse the same row logic twice: top with for i in range(1, rows + 1), bottom with for i in range(rows - 1, 0, -1), then a bare print() after each row.
Approach
How to Solve It
Reuse Program 53’s row logic twice — once counting up, once counting down from rows - 1 so the peak row is not duplicated.
Method
Idea
Best for
Two outer loops
Top 1..rows, bottom rows-1..1; same inner diagonals
Learning, interviews, exams
Rows input
Same logic with a user-chosen peak height
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows: // top half
for j from 1 to rows:
print i if i == j else a space
for k from (rows - 1) down to 1:
print i if i == k else a space
print newline
for i from (rows - 1) down to 1: // bottom half
// same row logic as above
print newline
Cheat sheet
Goal
Pattern
Top half
for i in range(1, rows + 1):
Bottom half
for i in range(rows - 1, 0, -1):
Left diagonal
print(j if i == j else " ", end="")
Right diagonal
print(k if i == k else " ", end="") with k = rows-1..1
Total lines
2 * rows - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(..., end="")
Stays on the same line
Each column (digit or space) in both halves
print()
Ends the current line
After both inner loops finish a row
Columns use end=""; a bare print() ends the row once.
Try it
Live Preview
Change the peak row count and the full diamond updates instantly — capped at 9 so every digit stays a single character.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 9×9 cells
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
Trace
Worked Walkthrough — Peak rows = 5
Count how the two outer loops build nine lines without repeating the tip.
Outer loop
i values
Lines printed
Top
1, 2, 3, 4, 5
V-half ending with tip 5
Bottom
4, 3, 2, 1
Mirror back — skips i = 5
Total lines = 5 + 4 = 9 = 2n - 1. Each line has 2n - 1 characters → O(n²).
Code
Python Programs
Three complete programs: fixed rows = 5, user-input peak, and a compact rows = 3 demo. Use View Output for sample results.
Example 1 — Fixed rows = 5
Hard-coded peak — top loop 1..rows, bottom loop rows-1..1, same inner diagonal logic each row.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(1, rows + 1):
print(j if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(k if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows + 1):
print(j if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(k if i == k else " ", end="")
print()
Output
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
How It Works
1. Top half. Same as Program 53 — i runs 1 to 5, printing the growing V.
2. Bottom half.i runs 4 down to 1 with identical inner loops — mirrors without repeating the tip.
3. Result. Nine lines forming a full diamond with digits on both diagonals.
Example 2 — User Input (rows)
Read the peak row count and draw the same full diamond.
Python
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Please enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Please enter a positive whole number.")
raise SystemExit(1)
for i in range(1, rows + 1):
for j in range(1, rows + 1):
print(j if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(k if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows + 1):
print(j if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(k if i == k else " ", end="")
print()
Output (when user enters 4)
Enter the number of rows: 4
1 1
2 2
3 3
4
3 3
2 2
1 1
2. Same core. Both outer loops and both diagonal conditions adjust automatically from rows.
3. Single-digit tip. Cap demos at rows ≤ 9 so every digit stays one character wide.
Example 3 — Compact rows = 3
Same two-outer-loop structure with a smaller peak for quick paper tracing.
Python
rows = 3
for i in range(1, rows + 1):
for j in range(1, rows + 1):
print(j if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(k if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows + 1):
print(j if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(k if i == k else " ", end="")
print()
Output
1 1
2 2
3
2 2
1 1
How It Works
1. Five lines. Top prints i = 1..3; bottom prints i = 2..1 — five lines total.
2. Trace on paper. Confirm the tip 3 appears once, then the V opens again downward.
Edge Cases & Pitfalls
Check these before calling the solution done.
i = rows twice
Doubled tip
If the bottom loop starts at i = rows, the middle row prints twice. Keep for i in range(rows - 1, 0, -1).
missing bottom
V instead of diamond
Omitting the second outer loop leaves only Program 53’s V-half. Add the rows-1..1 pass after the tip.
k = rows
Doubled center column
If the right inner loop starts at k = rows, the middle digit doubles on each row. Keep range(rows - 1, 0, -1).
print()
Broken rows
If bare print() sits inside either half, each column lands on its own line. Call it only after both inner loops.
rows = 1
Single tip
Output is just 1 — the bottom loop does not run. A good sanity check.
Bad input
int(input()) raises
Wrap with try/except ValueError so non-numeric input does not crash the script.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
2n - 1 lines × 2n - 1 characters each → about 4n² prints. For n = 5 that is 81 characters across 9 lines.
Remember
Key Takeaways
Two passes: top i = 1..rows, bottom i = rows-1..1 — same i == j / i == k row logic.
Skip the tip twice: bottom starts at rows - 1; right half starts at rows - 1.
print vs print(): columns use end=""; bare print() advances after both inner loops.
Next step: Program 55 fills a triangle column-wise into a 2D list.
One line: top 1..rows, bottom rows-1..1, each row prints diagonal digits with spaces, then print().
Frequently Asked Questions
The first loop prints the top V-half from i = 1 to rows. The second prints the bottom half from rows-1 down to 1, mirroring the shape.
Starting at rows would print the middle row twice. rows-1 skips the peak row already printed by the top half.
It prints digits on both diagonals per row — top half grows to rows, then the bottom half mirrors back to 1, forming a symmetric diamond.
Program 53 prints only the top V-half. Program 54 adds a second outer loop to mirror the same row logic downward.
The left loop uses i == j for the main diagonal. The right loop uses i == k for the mirrored diagonal, with spaces elsewhere.
Change rows or read it from user input with int(input()) inside try/except — see Example 2.
O(n²) for n rows because the diamond has about 2n-1 lines and each line scans about 2n-1 positions.
Yes. Print when i == j or i + j == rows + 1 in a single column loop.
Use try/except ValueError around int(input()) so bad input does not crash the script.
One line prints a single 1 — the bottom loop starts at 0 and does not run.
🤔
Did you know?
Program 53’s V-shape becomes a full diamond by adding a second outer loop from rows-1 down to 1. Total lines = 2n-1 with about 2n-1 characters per line — O(n²) overall.