Syllabus Lesson 26 of 141 · Use AI to understand
Use AI to understand

AI-off twin: the price bands

Why it matters If an idea only works on the problem you learned it from, you memorised an answer. A twin tests the idea on a new surface, with the tutor off.

The tutor is switched off for this problem. Work it out yourself.

A marketplace keeps its catalogue of 200,000 prices, in pence, in a sorted export on a remote store that charges per value read. The pricing team sends batches of price bands, such as "from 10.00 to 49.99 inclusive", and wants to know how many products fall in each.

The data

from creative.budget import Tape
tape = Tape([199, 450, 450, 999, 2500])  # sorted prices, every read counted
bands = [(400, 999), (1000, 1000)]        # both ends included
# band_counts(tape, bands) -> [3, 0]
Your turn

Write band_counts(tape, bands). tape is a Tape of sorted whole-number prices (repeats allowed). bands is a list of (lo, hi) pairs. Return a list with, for each band, how many prices p have lo <= p <= hi (if hi < lo the band is empty). Budget: with 200,000 prices and 200 bands you may read at most 30,000 values. len(tape) is free.

AI off: no tutor, hints or worked solution on this one. Your own thinking only.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.