Pyramid · Pattern
Diamond Rows
Diamond Rows
PyramidSymbol pattern, solid style, size 5. Changing the rubric, style or size above re-shapes the loops — the structure stays identical while only the printed value or the cell filter changes.
Diamond Rows · output
Symbol · Solid · size 5
n = int(input("Enter number of rows: "))
# Upper half — 1, 2, 3, ..., n stars
for i in range(1, n + 1):
print((2 * (n - i)) * " ", end='')
for j in range(1, i + 1):
print('*', end=' ')
print()
# Lower half — n - 1, ..., 2, 1 stars
for i in range(n - 1, 1 - 1, -1):
print((2 * (n - i)) * " ", end='')
for j in range(1, i + 1):
print('*', end=' ')
print()Behind the Scenes Execution
registers · line tracking · call stack · memory regions| Address | Variable | Value |
|---|---|---|
| Stack (4) | ||
| 0x200 | n | 5 |
| 0x204 | i | -1 |
| 0x208 | j | -1 |
| 0x20C | count | 0 |
| String Pool (static) (2) | ||
| 0x400000 | pool["*"] | * |
| 0x400008 | pool["\n"] | \n |
| Heap (9) | ||
| 0x3000 | grid[0] | |
| 0x3020 | grid[1] | |
| 0x3040 | grid[2] | |
| 0x3060 | grid[3] | |
| 0x3080 | grid[4] | |
| 0x30A0 | grid[5] | |
| 0x30C0 | grid[6] | |
| 0x30E0 | grid[7] | |
| 0x3100 | grid[8] | |
Diamond Rows
A diamond made of steps instead of a smooth curve: rows grow 1, 2, 3, …, n stars and then shrink back, but each row is padded and the stars are spaced out so the outline reads as stacked treads. Loops run twice — one for the grow half, one for the shrink half.
The size-5 grid
Nine rows confined to 5 columns, all right-aligned to column 4: widths climb 1, 2, 3, 4, 5 then fall 4, 3, 2, 1, so the sequence is a staircase rather than a smooth pyramid. Every row starts at column 4 - width + 1.
Off-by-one traps
The lower half has to begin at width 4; starting it at 5 prints two 5-wide rows and shifts the rest of the staircase down one row.
Step by step
- Type n when the program asks.
- Section “// Upper half”: loop i = 1 to n.
- Print 2*(n - i) spaces, then print i “star + space” pairs — the treads of the ladder.
- Newline after the row.
- Section “// Lower half”: loop i = n - 1 down to 1, the same padded pairs in reverse.
Complexity
When to use it
- A second take on the diamond
- Seeing that “padding + what you print” defines the style
grid[r] rows in the Heap region and the i / j / count registers on the Stack — every printed character is one write to the grid.Recommended resourcesAffiliate links · may earn us a commission