SECTION 07 / 17

Shopping bills: find loyal customers

2 min read

In this lecture, a loyal customer has at least two observed bills, all from the same shop. One visit is too little evidence for this definition. Several visits to different shops fail the definition. We describe only the bills in our dataset.

Neeraja has two observed bills from SV Stores. A single visit is not enough to pass the lecture’s loyalty rule.
Neeraja has two observed bills from SV Stores. A single visit is not enough to pass the lecture’s loyalty rule.
REAL COURSE DATA · THINK BEFORE EACH STEP

Group bills to find loyal customers

Trace the records, compare the methods and watch what changes. Back and Reset let you try again.

Ready

CURRENT RECORD X

Ready to begin

Choose Next step to follow the actual course records.

What we remember

Processed
0

Start with Next step. Predict what changes before reading the explanation.

Group the bills by Customer
for each customer bin B:
    if number of bills in B < 2:
        report "Too little evidence"
    else:
        FirstShop = the shop on the first bill
        SameShop = true
        for each bill X in B:
            if X.Store != FirstShop:
                SameShop = false
        if SameShop:
            report "Loyal in these observations"
        else:
            report "Uses multiple shops"

We do not need to compare every bill with every other bill. Within each customer bin, compare each shop name with one reference shop. The inner scans together inspect every bill once, even though the code has a loop over bins and a loop over their contents. Work depends on how often records are visited, not only on counting WHILE keywords.

There are 16 customer bins: 9 single-visit customers, 6 customers who visited multiple shops, and Neeraja, the only customer with repeated bills from one shop. Srivatsan has 4 bills from 2 shops; Ahmed has 4 bills from 2 shops. Seeing a different shop proves a customer fails, but seeing only the first two same-shop bills does not yet prove they pass.

The lecture also tries collecting one customer’s bills while scanning the remaining pile. Removing the whole completed customer group prevents repeating that customer’s task. However, repeatedly searching for the next customer’s bills can still revisit other bills. Grouping all customers first avoids those repeated full searches when bins can be located efficiently.

Try it yourself

Four bills for a customer: SV Stores, SV Stores, SV Stores, Sun General. Loyal?

Show explained answer

No. The last bill breaks the “all bills from one shop” condition. Never return true after checking only the first few matching bills.

Try it yourself

Why group by customer instead of shop?

Show explained answer

The question asks about one customer’s behaviour across shops. A shop bin mixes customers and does not by itself tell whether a particular customer also visited another shop.

Try it yourself

Does “loyal in these bills” mean the person never uses another shop?

Show explained answer

No. It is a conclusion about the supplied observations under the stated rule, not a claim about all their shopping.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑