Quiz 2

624 words
3 min read
Python Week 1: the first filter for runtime behavior
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 at i+1 for unique pairs.
  • Iteration count → outer n times inner mn × m body 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:
text
for i from 0 to n-1:
    for j from i+1 to n-1:
        compare item[i] with item[j]
Count: n(n1)2\frac{n(n-1)}{2}.
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.
text
found ← 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 i and j after nested for.
  • 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.
text
count ← 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 j never advances toward stop.
  • Row/column order swapped in grid interpretation.
  • Assuming always — restricted inner changes count.

Diagnostic (try yourself)

  1. How many times does the body run?
text
for i from 0 to 4:
    for j from 0 to 2:
        # body
  1. For n=6, how many unique unordered pairs (i,j) with j > i?
  2. Values [14, 6, 21, 9, 14] binned by index = value // 10. List the five bin indices.
  3. List [1,2,3,2]. How many unique pairs have equal values?
  4. Why use j = i+1 instead of j = 0 when detecting duplicates once per pair?
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.