Quiz 2

Computational Thinking · Week 2 — Iteration & selection

1026 words
5 min read
2026-08-16T00:00:00.000Z
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

accumulation, max/min, AND predicates — concepts, pattern families, and traps for Quiz 2 week 2. # Week 2 — iteration & selection > **Quiz 2 scope:** Weeks 1–8 per IITM May 2026 foundation courses.

Week 2 — iteration & selection

Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.
Part of the Quiz 2 prep system%20%C2%B7%20%5BWeeks%201%E2%80%938%20index%5D(.%2Fmay-2026-ct-quiz-2-weeks-1-8-prep) · Pattern atlas · Formula chains.

Week map

Loop over collection → accumulate → compare for max → filter with AND

Classify → Represent → Execute → Trap-check

  • Recognize: Ask: Initial value for product accumulation?
  • Procedure: Initialize total 0. For each x, add to total. Result after last item.
  • Variations / traps: Watch for: Max of empty without check.

Formula chain (compressed)

accumulator → max/min scan → AND predicate over loop → selection diamond.
  1. Accumulatorsum ← sum + x — running total
  2. Max/minif x > best: best ← x — linear extremum
  3. AND over itemsall satisfy P(x) — flag stays true
  4. Selectionif condition → branch — at most one path
  5. Countercount ← count + 1 — how many match

Deep study

Computational Thinking · Week 2 — Iteration and selection

Loop patterns combine walking data with filters (AND predicates) and aggregates (sum, max, count).

Week map

Loop over collection → neutral start value → accumulate → scan for max/min → filter with AND → empty input edge cases.

Accumulation notation

  • Sum accumulator → start 0 → total ← total + x.
  • Product accumulator → start 1 → prod ← prod * x.
  • Count accumulator → start 0 → increment when condition holds.
  • Max scan → start first item or sentinel → replace when x > best.

AND filter

All conditions must pass:
“positive and even” → x > 0 and x % 2 == 0.
Contrast OR (week 3 procedures): “positive or even” passes more items.
Mini-list: [3, -4, 6, 8, -2]. Positive and even → only 6, 8.

Max and min scans

text
best ← first item
for each x in data:
    if x > best:
        best ← x
Empty data: no first item — algorithm must guard or define error.
Mini-example: [5, 2, 9, 9, 1]. Max scan ends 9. For positive max only: ignore nonpositive, start best at first positive or None.

Pattern families

Easy — Sum or count list

  • Sum all elements.
  • Count how many equal a target.
  • Product of list (start 1).

Medium — Max with AND filter

  • Largest among entries meeting two tests.
  • Count items where both predicates true.
  • Sum only negatives in list.

Hard — Compound loop logic

  • Multiple accumulators in one pass (sum and count together).
  • Empty list: max undefined; count returns 0.
  • Index loop vs for-each — same totals if bounds correct.

Worked mini-examples

Example 1 — Sum.
Data [10, 20, 5]. total=0 → 10 → 30 → 35.
Example 2 — AND count.
Data [2, 3, 4, 5, 6]. Count even and >3:
  • 4 yes, 6 yes → count 2.
Example 3 — Filtered max.
Data [-1, 8, 3, 12, 5]. Max among positive:
Candidates 8,3,12,5 → max 12.
Example 4 — Empty.
Data []. Sum loop leaves total=0. Max scan without guard — undefined; safe design returns sentinel or “no data”.
Example 5 — Dual accumulator.
Data [1,2,3,4]. Track sum and count of evens in one pass:
Evens 2,4 → sum 6, count 2.

Traps

  • Product accumulator starting at 0 (always 0).
  • OR filter when problem says “all conditions”.
  • Max of empty without check.
  • Off-by-one manual index: 0..len-1 not 0..len.
  • Updating max before checking filter — polluted by ineligible items.

Diagnostic (try yourself)

  1. List [7, -2, 4, 0, 11]. What is the sum of positive entries only?
  2. Same list: how many entries are both positive and less than 10?
  3. List [3, 9, 1, 9, 2]. What is the maximum value? If we only consider values ≥ 5, what is the max?
  4. Why does product accumulation start at 1, not 0?
  5. One pass: for [5, 10, 15, 20], find sum and count of multiples of 5.

ChatGPT prep archive

Archived import for extra depth — complements the notes above, not official IITM material.

Core concepts

  • Accumulation: start neutral (0 for sum, 1 for product) then combine each item.
  • Max/min scan: keep best-so-far; compare each element.
  • Selection with AND: all conditions must pass—think checklist.
  • Loop guard: ensure collection non-empty before picking first as max.

Notation & vocabulary

GoalStart value
sum0
product1
maxfirst item or sentinel

Pattern families

Easy — Sum list

Initialize total 0. For each x, add to total. Result after last item.

Medium — Max with AND filter

Only consider items where predicate true (e.g. positive AND even). Update max among eligible only.

Hard — Count satisfying all

Nested logic: loop with compound condition incrementing counter when all tests pass. Empty input yields 0 count, not error.
Drill these on the pattern atlas — filter to week 2.

Traps

  • Max of empty without check.
  • OR used when problem says all conditions.
  • Accumulator wrong neutral element.
  • Off-by-one in manual index loop.

Retrieval prompts

  1. Initial value for product accumulation?
  2. How does AND filter differ from OR?
  3. What if max is sought on empty data?

Practice loop

  1. Read Deep study (if present) or core concepts once.
  2. Recite the formula chain without looking.
  3. Open one easy pattern on the interactive atlas for week 2.
  4. Attempt without solutions; mark studied after an honest try.
  5. Say one trap aloud before closing the tab.
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.