Quiz 2
Registry Synced

computationalthinking-week6

533 words
3 min read

Reading compass

Now · Week map

Deep study for Quiz 2 week 6. Tables organize records in rows; dictionaries map keys to values for fast lookup and counting.

Week map

Row/column table → record as dict → key-value lookup → frequency tally → table vs dict tradeoff.

Table notation

  • Row → one record → e.g. student name + score + section.
  • Column → one field across rows → all scores, all names.
  • Cell → intersection of row and column → single value.
  • Header → column labels → keys when converting to dict.
Mini-table:
NameScore
Ada88
Ben92
Row 1 cell (Ada, Score) = 88. Column Score = {88, 92}.

List of records (table as data)

text
table ← [
  {Name: Ada, Score: 88},
  {Name: Ben, Score: 92}
]
Access row 0 field Score → 88.

Dictionary notation

  • Key → unique identifier → string, number, tuple.
  • Value → data stored → any type.
  • Lookup → given key, retrieve value in O(1)O(1) average time (conceptually constant).
  • map[key] ← value → insert or update.
Mini-example: frequency of letters in "aba":
text
freq ← empty map
for each character c in string:
    if c in freq:
        freq[c] ← freq[c] + 1
    else:
        freq[c] ← 1
Result: {a: 2, b: 1}.

Pattern families

Easy — Read table cell

From small grid or list-of-dicts, fetch one value. Identify row vs column.

Medium — Build dict from table

Convert parallel columns to records. Count occurrences. Lookup by key; handle missing key.

Hard — Aggregate with dict

Group rows by category key. Sum or average values per group. Merge two tables on shared key.

Worked mini-examples

Example 1 — Row access.
Three rows, columns (ID, Qty): row 2 ID cell = value in column ID at index 1 (0-based).
Example 2 — Dict lookup.
phone["Ana"] = "555-0100". Lookup "Ana" → number. Lookup missing key → error unless default policy stated.
Example 3 — Frequency.
Items [red, blue, red, green]: freq red=2, blue=1, green=1.
Example 4 — Dict of lists (column store).
text
data.Name ← [Ada, Ben]
data.Score ← [88, 92]
Column Name row index 1 → Ben.
Example 5 — Update vs insert.
If key exists, overwrite value; dict size unchanged. New key increases size by 1.

Traps

  • Row index vs ID column value — row 3 ≠ ID 3 unless stated.
  • Missing key in dict — define behavior (0, null, error).
  • Counting rows vs counting distinct keys.
  • Assuming table sorted unless specified.
  • Confusing list index with dict key.

Diagnostic (try yourself)

  1. In a 4-row table with columns A and B, what does cell (row 3, B) mean?
  2. Map {x: 10, y: 20}. What is value at x? What happens at z without default?
  3. Count how many times "the" appears in word list [the, cat, the, sat].
  4. Represent the mini-table above as one dict mapping Name → Score.
  5. When is dict better than scanning entire table for each lookup?
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.