Step 01 Beginner Problems — Logic Building (Patterns)
Hard
Hard patterns (roughly Patterns 19–22 and the trickier mediums) break the single-formula comfort of the easy band: two quantities change at once, shapes mirror each other, borders matter more than interiors, or every cell depends on its distance from the edge. This lesson teaches the four techniques that crack them — composition, mirroring, border conditions and the min-distance formula.
Technique 1: Compose from halves you already own
Complex silhouettes are almost always two simpler patterns stacked or placed side by side. The butterfly/symmetry shapes are a triangle and its mirror sharing each row; the diamond was pyramid + inverted pyramid. When a shape intimidates you, draw a line where its behaviour changes and solve each region with the observation table separately — hard patterns are medium patterns wearing a seam.
Technique 2: Mirror with index arithmetic
Mirrored segments never need a separate formula: the right half is the left half read backwards. In a row of width w, column j mirrors column w - 1 - j. The number crown pattern shows the idea — ascending numbers, a gap that shrinks by 2 each row, then the same numbers descending. Build each side from its own loop and let the gap be a third segment in your table.
def number_crown(n):
for i in range(1, n + 1):
left = ''.join(str(x) for x in range(1, i + 1))
gap = ' ' * (2 * (n - i))
right = ''.join(str(x) for x in range(i, 0, -1))
print(left + gap + right)
number_crown(4)
# 1 1
# 12 21
# 123 321
# 12344321Technique 3: Border conditions for hollow shapes
Hollow patterns (like the hollow rectangle) print a symbol only on the boundary and spaces inside. The condition is pure index logic: cell (i, j) is on the border when i == 0 or i == n - 1 or j == 0 or j == m - 1. One if inside a plain double loop solves the whole family — hollow triangles, hollow diamonds and frames all reduce to choosing the right border predicate for the shape's geometry.
Technique 4: The min-distance formula for concentric patterns
The number pattern (Pattern 22) prints, in every cell, a value that depends on how deep the cell sits inside the square. Depth of cell (i, j) in a square of side s is min(i, j, s-1-i, s-1-j) — its distance to the nearest edge — and the printed value is n - depth. No state, no counters: the cell's coordinates alone decide its value. Whenever a pattern looks the same at every scale, suspect a distance formula.
def number_pattern(n):
s = 2 * n - 1
for i in range(s):
row = []
for j in range(s):
depth = min(i, j, s - 1 - i, s - 1 - j)
row.append(str(n - depth))
print(' '.join(row))
number_pattern(3)
# 3 3 3 3 3
# 3 2 2 2 3
# 3 2 1 2 3
# 3 2 2 2 3
# 3 3 3 3 3Debugging hard patterns
Work at n = 3 or 4 and print your formula table beside the output: for each row write the expected segment counts from the picture, then instrument mentally which segment first disagrees. Almost every hard-pattern bug is a single off-by-one in one segment (gap shrinking by 1 instead of 2 is the classic), and comparing segment by segment finds it in one pass instead of ten blind edits.
Key points
- Hard patterns are composites: find the seam, solve each region with its own observation table.
- Mirror segments need no new formula — column j mirrors column w - 1 - j.
- Hollow shapes are one border predicate inside a plain double loop (i/j on an edge).
- Concentric patterns are coordinate formulas: value = n - min distance to any edge.
- Debug segment by segment at n = 3–4; expect a single off-by-one, most often in the shrinking gap.