Neural Sync Active
computationalthinking-week6
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:
| Name | Score |
|---|---|
| Ada | 88 |
| Ben | 92 |
Row 1 cell (Ada, Score) = 88. Column Score = {88, 92}.
List of records (table as data)
texttable ← [ {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) average time (conceptually constant).
map[key] ← value→ insert or update.
Mini-example: frequency of letters in "aba":
textfreq ← 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).
textdata.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)
-
In a 4-row table with columns A and B, what does cell (row 3, B) mean?
-
Map {x: 10, y: 20}. What is value at x? What happens at z without default?
-
Count how many times "the" appears in word list [the, cat, the, sat].
-
Represent the mini-table above as one dict mapping Name → Score.
-
When is dict better than scanning entire table for each lookup?