Pyramid · Pattern
Butterfly
Butterfly
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.
Butterfly · output
Symbol · Solid · size 5
n = 5
for i in range(2 * n):
for j in range(2 * n):
if (i < n and (j <= i or j >= 2 * n - 1 - i)) or (i >= n and (j <= 2 * n - 1 - i or j >= i)):
print('*', end='')
else:
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 (10) | ||
| 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] | |
| 0x3120 | grid[9] | |
Butterfly
Two triangles facing each other with an empty gap that narrows and widens again: on every row the left wing grows while the right wing mirrors it, and the wings swap behaviour at the middle row — a butterfly. The row width formula min(i, 2n - 1 - i) + 1 drives both wings.
The size-5 grid
Ten rows across 10 columns with two wings: wing width climbs 1 to 5 then falls back, the left wing spans columns 0 to w - 1 and the right wing spans columns 10 - w to 9, and the gap between them is 10 - 2w spaces. The two middle rows have their wings touching.
Off-by-one traps
A gap of 2(5 - w) + 1 fuses the wings one row too early and clips the right edge on the widest rows; the wings and the gap must always total 10 columns.
Step by step
- Type n when the program asks — each wing is n rows tall and the body has 2n rows total.
- Loop i = 1 to n (upper half): print i values flush left, then pad the middle with 2·(n − i) spaces, then print i values again.
- Newline after each row — the gap closes as i approaches n.
- Loop i = n - 1 down to 1 (lower half): repeat exactly the same row recipe, so the wings shrink back.
Complexity
When to use it
- Mirrored blocks on one line
- Combining grow and shrink phases in a single loop
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