FOUNDATION · WEEK 07

Dictionaries, frequencies and customer patterns

Learn keys and values through shared birthdays, word frequencies, shopping profiles and pronoun matching, with easy-English examples and interactive traces. Reuse course datasets and choose keys and values that answer the next question.

31 study sections33 illustrations2 resources
Start Week 7 →

Study sections

Follow the order or choose a section
01

A dictionary connects a key to a value

A dictionary is a collection of labelled entries. Each entry has a KEY, which tells us what to look for, and a VALUE, which is the info…

1 illustrations
02

Create the dictionary before assigning entries

Start with d = {}. This declares an empty dictionary. Then d[7] = 76 creates a key 7 and stores 76. d[21] = 74 creates a second entry.…

2 illustrations
03

A key is a label, not a list position

The dictionary {7:76,21:74} has two entries. Yet marks[21] is a valid lookup because 21 is a key. It does not request the 21st element.…

1 illustrations
04

Check whether the key exists before reading it

With d={7:0}, isKey(d,7) is True and d[7] is 0. isKey(d,99) is False. Do not assume a missing lookup automatically returns 0 or False i…

1 illustrations
05

Loop through keys, then read their values

Do not assume dictionary keys come out in the order they were inserted, in number order, or in alphabetical order. Our visual table cho…

1 illustrations
06

Build a marks dictionary and compute an average

Create chemMarks={}, then scan each score card. Store chemMarks[X.CardNo]=X.Chemistry. The lecture shows names as keys; the lab uses un…

1 illustrations
07

Create on the first visit; accumulate on later visits

For Kohli’s first illustrative 20, create runs[Kohli]=20. For a later 30, read 20 and replace it with 20+30=50. Writing runs[Kohli]=30…

1 illustrations
08

Use seen to avoid reversed pairs without marking cards

Mark the OUTER card before checking inner cards. The mark excludes the card itself and all earlier outer cards. A later inner card rema…

1 illustrations
09

Build a word-length frequency dictionary

Use the provided LetterCount field. The Word field retains punctuation, so counting the visible characters of “morning.” is a different…

1 illustrations
10

Count students from each city

Now use CityTown as the key and a student count as the value. Each record increments exactly one city. This is the same create-or-incre…

1 illustrations
11

Use day and month as a birthday key

Earlier weeks used all pairs, month bins and sorting. A dictionary provides another route: read each student once, compute a day-and-mo…

1 illustrations
12

Keep duplicate dates in a second dictionary

birthdays remembers every observed birthday; duplicates remembers only dates seen at least twice. Both are dictionaries of True flags.…

1 illustrations
13

Store the IDs who share each birthday

On Aditya’s card, birthdays[15 Mar] becomes [7]. On Monika’s card it becomes [7,21]. Here length(birthdays[15 Mar]) is 2, while length(…

1 illustrations
14

A dictionary value can be another dictionary

birthdays[15 Mar] is now {7:True,21:True}. To find IDs, use keys(birthdays[15 Mar]). To ask whether ID 21 is in that date group, test i…

1 illustrations
15

Choose what to remember and what work remains

Under the course’s constant-time dictionary-operation assumption, a birthday presence/count scan does one small amount of dictionary wo…

1 illustrations
16

First choose the unit: item rows, quantities or money

A bill item row represents one listed product entry. The quantity column says how much of that product was purchased. Price × quantity…

1 illustrations
17

Make the broad-category map explicit

Use the same rule for every bill. Keep the original detailed label alongside the broad group so the aggregation is reversible by return…

1 illustrations
18

Build customer → category → row counts

The lab pre-fills all five category keys with zero for a readable table. The pseudocode above creates only observed category keys. They…

1 illustrations
19

Separate simple groups before comparing detailed profiles

The lecture explores coarse bins before examining all profile differences. First set aside customers whose only observed category is Fo…

1 illustrations
20

Add absolute category gaps to measure distance

abs means absolute value: abs(8−13)=5 and abs(13−8)=5. Every gap is non-negative, so a larger Food count cannot cancel a smaller Utilit…

1 illustrations
21

Equal totals can hide different baskets

Abhinav totals 14 rows and Srivatsan totals 21. The absolute difference of totals is 7, but their five-category Manhattan distance is 1…

1 illustrations
22

Use a customer-by-customer table for all 15 pairs

Use indices only as local labels: 1 Abhinav, 2 Akshaya, 3 Ahmed, 4 Srivatsan, 5 Advaith, 6 Vignesh. These are not bill numbers or perma…

1 illustrations
23

Close pairs do not automatically form one close group

With close defined as distance ≤10, the qualifying pairs are 1–2 (7), 1–5 (10) and 5–6 (10). But the candidate group {1,5,6} contains a…

1 illustrations
24

Count food rows for each customer, then find the maximum

The source’s foodD creates a customer key only when a Food row occurs. A different valid representation creates every observed customer…

1 illustrations
25

Change the key to compare shops

To ask which shop has most Food rows, keep the same qualifying item test but use bill.Store as the key. The fixed three-shop problem co…

1 illustrations
26

Keep bill IDs when a count alone loses useful detail

A count such as Ahmed→18 cannot tell us which shops supplied the food. Ahmed’s saved IDs are [18,20,25,29] in original bill-number orde…

1 illustrations
27

Turn “usually buys there” into a precise check

A vague statement such as “top food buyers shop at the top food shop” needs a rule. We test one explicit all-bills version: for the sel…

1 illustrations
28

Index noun and pronoun positions with lists

The dataset field is SerialNumber, beginning at 0. The slides use SerialNo for the same position role. Our lab maps the concept to the…

1 illustrations
29

Map each pronoun to the nearest earlier noun

For each pronoun p, reset matched to −1. Walk the sorted noun positions. Every n<p is a candidate; updating matched as n increases leav…

1 illustrations
30

Debug the first occurrence, the repeated key and empty input

Trace tiny inputs before processing the whole dataset. Use no records, one record, the same group twice, a missing key, a stored zero,…

1 illustrations
31

Choose keys and values from the question

A list preserves an ordered sequence and repeated occurrences. A dictionary connects unique keys to current values. Combining them give…

2 illustrations