Quiz 2
Registry Synced

python-week5

558 words
3 min read

Reading compass

Now · Week map

Deep study for Quiz 2 week 5. Nested loops multiply work: outer drives rows, inner completes fully each time.

Week map

Outer loop → inner loop completes per outer step → iteration count product → search flag → running min/max → break scope.

Nested loop notation

  • Outer index → often row or first coordinate → runs its full range once per “round.”
  • Inner index → completes entire range for each outer value → total body runs = outer count × inner count.
  • break → exits innermost loop only → outer continues unless restructured.
Mini-example:
python
count = 0
for i in range(3):
    for j in range(2):
        count += 1
# count = 6  (3 × 2)

Search pattern

python
found = False
for item in seq:
    if item == target:
        found = True
        break
Set flag, break on match. for/else: else runs only if loop did not break.

Min/max pattern

python
best = seq[0]
for x in seq[1:]:
    if x > best:
        best = x
Initialize from first element — not 0 when data may be all negative.

Pattern families

Easy — Nested count

Multiply outer and inner trip counts. Trace one outer step showing full inner run. List final values of loop variables.

Medium — Search and index

Linear search with flag or break. Track index with enumerate or manual counter. Report first match vs all matches.

Hard — Min/max with updates

Safe initializer for negatives. Nested search: find max in each row, or best pair sum. break only leaves inner loop — trace carefully.

Worked mini-examples

Example 1 — Grid pairs.
python
pairs = []
for i in range(2):
    for j in range(3):
        pairs.append((i, j))
# 6 pairs: (0,0)..(1,2)
Example 2 — Search.
python
nums = [4, 7, 2, 9]
target = 7
found = False
for n in nums:
    if n == target:
        found = True
        break
# found is True
Example 3 — Max.
python
data = [-3, -8, -1, -5]
m = data[0]
for x in data:
    if x > m:
        m = x
# m = -1
Example 4 — break scope.
python
for i in range(3):
    for j in range(3):
        if j == 1:
            break
    # inner breaks at j=1; outer continues
# outer runs 3 times; inner runs 2 times each → 6 inner bodies

Traps

  • break only leaves inner loop in nested structure.
  • Max initialized to 0 fails for all-negative data.
  • Off-by-one: range(len) vs direct iteration.
  • Inner variable shadows outer (for i inside for i).
  • Counting iterations as n + m instead of n × m.

Diagnostic (try yourself)

  1. How many times does the body run?
python
for a in range(4):
    for b in range(3):
        pass
  1. After nested loops below, what is j?
python
for i in range(2):
    for j in range(5):
        pass
  1. Write a loop that sets found = True and breaks when target appears in list items.
  2. Find max of [-10, -3, -7] — why should best not start at 0?
  3. In a 3×3 nested loop with break when j == 0 in the inner loop, how many inner bodies execute per outer step?
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.