SECTION 18 / 31

Build customer → category → row counts

1 min read
Abhinav’s five-category row-count profile is [3,8,2,0,1], with total 14.
Abhinav’s five-category row-count profile is [3,8,2,0,1], with total 14.
REAL COURSE DATA · KEYS, COUNTS AND CONNECTIONS

Build customer → category counts

Follow the dictionary as its keys and values change. Predict each lookup or update before revealing the next step.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next key, whether it already exists, and the value that will be stored.

profile = {}
foreach bill in bills {
    customer = bill.Customer
    if (not isKey(profile,customer)) {
        profile[customer] = {}
    }
    foreach item in bill.Items {
        category = broadCategory(item.Category)
        if (not isKey(profile[customer],category)) {
            profile[customer][category] = 0
        }
        profile[customer][category] = profile[customer][category] + 1
    }
}

The lab pre-fills all five category keys with zero for a readable table. The pseudocode above creates only observed category keys. They are equivalent for the displayed profile if missing category counts are treated as zero when forming the five-column vector. Category order is [Apparel, Food, Toiletries, Utilities, Household].

CustomerApparelFoodToiletriesUtilitiesHouseholdTotal rows
Abhinav3820114
Advaith3414012
Ahmed01815024
Akhil020002
Akshaya31340121
Aparna010214
George040004
Julia060006
Mohith140005
Neeraja061007
Radha050005
Rajesh020305
Srivatsan11213421
Sudeep140016
Suresh050117
Vignesh0363012

A zero here means no such row was observed in these supplied bills. It does not establish that the customer never buys that category elsewhere. Customer → category and customer → customer tables answer different questions.

Try it yourself

In profile[Ahmed][Food], what does each key mean?

Show explained answer

Ahmed selects the customer; Food selects the category within that customer’s dictionary.

Try it yourself

Can bills be shuffled before this accumulation?

Show explained answer

Yes. Each row still contributes to the same customer and category.

Try it yourself

Does total(profile[customer]) count rupees?

Show explained answer

No. Here it totals observed item rows.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑