Follows the top-half concentric square in Program 46; next is the powers of 11 pattern in Program 48.
Approach
How to Solve It
Reuse the square’s row builder twice — once going in, once going out.
Method
Idea
Best for
Two-half diamond
Top k..1, bottom 2..k, same cell rule
Learning, interviews, exams
Input k
Same logic with a user-chosen peak
Practice / demos
Pseudocode
Pseudocode
for i from k down to 1:
print left half j = k..1 and right half j = 2..k
(value = j if j > i else i)
for i from 2 to k:
print the same row rule again
print newline after each row
Cheat sheet
Goal
Pattern
Top half
for i in range(k, 0, -1):
Bottom half
for i in range(2, k + 1):
Left / right
range(k, 0, -1) / range(2, k + 1)
Cell value
print(j if j > i else i, end=" ")
End the row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(..., end=" ")
Stays on the same line
Each number plus a trailing space
print()
Ends the current line
After both half-loops finish a row
Print numbers without a newline, then end the row once.
Try it
Live Preview
Change k and the concentric diamond updates instantly.
Whole numbers from 1 to 7. Size = (2k − 1) × (2k − 1). Tap a chip or type a value — the preview redraws as you go.
Top half walks in; bottom half walks out (skip repeating the center).
Half
i
Printed row
Top
3
3 3 3 3 3
Top
2
3 2 2 2 3
Top
1
3 2 1 2 3
Bottom
2
3 2 2 2 3
Bottom
3
3 3 3 3 3
Total lines = 2*k − 1. Cells for k = 5 = 9 × 9 = 81.
Code
Python Programs
Three complete programs: fixed k = 5, input()k, and a compact k = 3 demo. Use View Output for sample results.
Example 1 — Fixed k = 5
Top half + bottom half with the same j > i cell rule.
Python
k = 5
for i in range(k, 0, -1):
for j in range(k, 0, -1):
print(j if j > i else i, end=" ")
for j in range(2, k + 1):
print(j if j > i else i, end=" ")
print()
for i in range(2, k + 1):
for j in range(k, 0, -1):
print(j if j > i else i, end=" ")
for j in range(2, k + 1):
print(j if j > i else i, end=" ")
print()
1. Top half.i runs from 5 down to 1 — same concentric rows as Program 46.
2. Bottom half.i runs from 2 up to 5 so the center row is not printed twice.
3. Cell rule. Both halves use left k..1, right 2..k, and j if j > i else i.
Example 2 — User Input (k)
Read k and build the full diamond with the same row logic.
Python
try:
k = int(input("Enter k: "))
except ValueError:
print("Please enter a positive whole number.")
raise SystemExit(1)
if k < 1:
print("Please enter a positive whole number.")
raise SystemExit(1)
for i in range(k, 0, -1):
for j in range(k, 0, -1):
print(j if j > i else i, end=" ")
for j in range(2, k + 1):
print(j if j > i else i, end=" ")
print()
for i in range(2, k + 1):
for j in range(k, 0, -1):
print(j if j > i else i, end=" ")
for j in range(2, k + 1):
print(j if j > i else i, end=" ")
print()
2. Same diamond. Only the peak changes from the literal 5 to the user value.
3. Safer input tip. Cap demos for readable output:
Safer input tip
if k < 1 or k > 7:
print("Enter a whole number from 1 to 7.")
raise SystemExit(1)
Example 3 — Compact k = 3
Same algorithm with a smaller peak — easier to trace by hand.
Python
k = 3
for i in range(k, 0, -1):
for j in range(k, 0, -1):
print(j if j > i else i, end=" ")
for j in range(2, k + 1):
print(j if j > i else i, end=" ")
print()
for i in range(2, k + 1):
for j in range(k, 0, -1):
print(j if j > i else i, end=" ")
for j in range(2, k + 1):
print(j if j > i else i, end=" ")
print()
Output
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
How It Works
1. Same rules. Only k changes — both outer loops and the cell rule stay identical.
2. Quick check. Five lines total; the middle row is 3 2 1 2 3.
Edge Cases & Pitfalls
Check these before calling the solution done.
no bottom
Forgot the bottom half
You get Program 46’s square only. Add for i in range(2, k + 1):.
i = 1..k
Bottom starts at i = 1
The center row prints twice. Start the bottom at i = 2.
k = 1
Single cell
Output is just 1 — the bottom loop never runs.
print() inside
print() inside a half-loop
That breaks the row. Call bare print() only after both inner loops.
j = 1..k
Right half starts at j = 1
The center digit doubles on every row. Start the mirror at j = 2.
input()
Catch ValueError
Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(k²)
O(1)
User input (Example 2)
O(k²)
O(1)
The grid is about (2k − 1)² cells, so total work is quadratic in k.
Remember
Key Takeaways
Rule: top k..1, bottom 2..k; each cell is j if j > i else i.
vs 46: Program 46 is the top half only; this page adds the outward mirror.
end=" " vs print(): numbers stay on the line; bare print() advances after both halves.
Complexity:O(k²) time; O(1) extra space.
One line: print concentric rows inward to 1, then mirror them outward to finish the diamond.
Frequently Asked Questions
A full concentric number diamond: outer layer k, values decrease to 1 at the center, then increase back to k — a (2k-1)×(2k-1) grid.
The first prints the top half (i = k down to 1). The second prints the bottom half (i = 2 up to k) to mirror the shape.
Program 46 prints only the top half (k rows). Program 47 adds the bottom half loop to form a complete diamond.
When column j is still outside the current row layer i, print j. Otherwise print i (the current layer value).
Each row has 2*k - 1 numbers. With k = 5, width and height are both 9.
Change k or read it from input — both outer loops and row width adjust automatically — see Example 2.
O(k²) because the grid has about (2k-1)² cells and each is printed once.
The center prints 1 — the deepest layer of the concentric pattern.
Use try/except ValueError around int(input()) and require k >= 1 — see Example 2.
🤔
Did you know?
Top half: for i in range(k, 0, -1):. Bottom half: for i in range(2, k + 1):. Each cell: j if j > i else i. Grid size = 2k - 1 rows and columns.