A fruit farm sells strawberries to the cafés in town. Each café has its own weekly demand rule: at a price of P euros per kilogram it wants
A café with rule (30, 4) wants 30 kg when strawberries are free, 4 kg less for every euro on the price, and nothing at all from 7.50 euros per kg upwards (its choke price a / b): it cannot order a negative amount.
The farm's market demand at a price is the total that all cafés want at that same price.
Write market_demand(buyers, prices). buyers is a list of pairs (a, b), one per café; prices is a list of prices. Return a list with the market demand at each price, in the order of prices. With no cafés, the demand is 0 at every price.
Press Run with plot(market_demand(buyers, ps), ps) for many prices ps to see the market demand curve (quantity across, price up). It bends at every café's choke price.
Examples
Input: buyers = [(30, 4), (20, 1), (12, 3)], prices = [2, 5, 8]
Output: [46, 25, 12]
Explanation: at 2 euros the cafés want 22 + 18 + 6 = 46 kg. At 5 euros the third
café's rule gives 12 - 15 = -3, so it buys nothing: 10 + 15 + 0 = 25 kg.
At 8 euros only the second café still buys: 0 + 12 + 0 = 12 kg.
Input: buyers = [(30, 4)], prices = [7.5, 0]
Output: [0.0, 30]
Explanation: 7.5 is exactly this café's choke price.
Constraints
- answers are compared with a tolerance of
1e-6
Goals
- Add up individual demands at the same price to get the market demand
- Treat a buyer whose rule gives a negative amount as buying nothing
- Evaluate a demand rule at many prices in one function