624 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Computational Thinking · Week 4 — Nested iteration **Nested loops** visit pairs or grid cells. Week 4 patterns: pair counting, duplicate detection, and **binning** tallies.

Computational Thinking · Week 4 — Nested iteration
Nested loops visit pairs or grid cells. Week 4 patterns: pair counting, duplicate detection, and binning tallies.
Week map
Outer loop → inner loop completes per outer step → index pairs (i,j) → unique unordered pairs → bin array → birthday collision pattern.
Nested loop notation
- Outer index
i→ often row or first item position. - Inner index
j→ column or second item; may start ati+1for unique pairs. - Iteration count → outer
ntimes innerm→n × mbody executions if full grid.
Mini-grid
i in 0..2, j in 0..2 (both 0,1,2): 3×3 = 9 pairs including (0,0),(1,1),(2,2).Unique unordered pairs
From
n items, compare each pair once without double-count:textfor i from 0 to n-1: for j from i+1 to n-1: compare item[i] with item[j]
Count: 2n(n−1).
Mini-example:
n=4 → pairs (0,1),(0,2),(0,3),(1,2),(1,3),(2,3) → six pairs.Trap: Inner
j from 0 to n-1 counts (0,1) and (1,0) separately — 12 pairs for n=4.Binning
Fixed buckets
bin[0..B-1]. For each value v, compute bucket index, increment bin[k].Example: scores 0–100 in bins width 10 → index
k = v // 10 (watch overflow at 100).List
[23, 45, 17, 39, 45] with bins 0-9,10-19,…:- 23→bin2, 45→bin4, 17→bin1, 39→bin3, 45→bin4
- bin4 count 2.
Birthday / duplicate pattern
Nested loops compare pairs for equality. If equal, “shared birthday” or duplicate found.
textfound ← False for i ... for j from i+1 ... if item[i] == item[j]: found ← True
Self-pair
i=j usually skipped when inner starts at i+1.Pattern families
Easy — Count loop executions
range(n)×range(m)body count.- Last values of
iandjafter nestedfor. - Grid row-major order listing of
(i,j).
Medium — Binning tallies
- Initialize bin array size from domain.
- Map value to index; increment correct bin.
- Boundary: value exactly on bin edge.
Hard — Duplicate / pair logic
- Unique pair enumeration without double count.
- Count pairs with sum equal target.
- Early exit flags vs counting all matches.
Worked mini-examples
Example 1 — Full grid count.
textcount ← 0 for i from 1 to 3: for j from 1 to 2: count ← count + 1 # 3 * 2 = 6
Example 2 — Unique pairs.
n=5, inner j = i+1 .. n-1. Pairs: 4+3+2+1 = 10.Example 3 — Duplicate.
List
[3,1,4,1,5]. Compare unique pairs; (1,3) positions values 4 and 1 — no; (3,4) values 1 and 1 — match once.Example 4 — Bins width 5.
Values
[7, 12, 3, 18, 12]. Index v//5: 7→1, 12→2, 3→0, 18→3, 12→2.Bins [1,1,2,1,0,...] for indices 0..3 at least.
Example 5 — Pair sum.
List
[2,5,3]. Pairs with sum 7: (2,5) and (5,2) if full grid — 2 if ordered; 1 if unique unordered.Traps
- Double-counting pairs when inner should start at
i+1. - Bin index off-by-one at boundaries (0-based vs 1-based bins).
- Infinite inner loop if
jnever advances toward stop. - Row/column order swapped in grid interpretation.
- Assuming
n²always — restricted inner changes count.
Diagnostic (try yourself)
- How many times does the body run?
textfor i from 0 to 4: for j from 0 to 2: # body
-
For
n=6, how many unique unordered pairs(i,j)withj > i? -
Values
[14, 6, 21, 9, 14]binned byindex = value // 10. List the five bin indices. -
List
[1,2,3,2]. How many unique pairs have equal values? -
Why use
j = i+1instead ofj = 0when detecting duplicates once per pair?