Per-rate price calculators
From a list of discount rates, build a price calculator for each rate. make_calculators(rates) creates, for each rate, "a function that takes a price and returns the price discounted by that rate", and returns them as a list in rate order. Complete make_calculators. Example calcs = make_calculators([0.1, 0.2, 0.3]) → 3 functions. calcs[0](10000) == 9000.0 (10% off)
- •rates come in as decimals between 0 and 1 (0.1 = 10%).
- •standard library only.
- •fixed-amount discounts / stacking coupons.
- •currency rounding policy.
▶About this challenge
Build per-rate price calculators in Python using the standard library only. From a list of discount rates it builds a price calculator for each rate and returns them as a list in rate order. You practice verifying that functions created inside a loop each compute with the right value.
Building several functions inside a loop and collecting them is a common Python pattern. What matters is understanding clearly how each function captures the value present when it is created. Calling just one function can look fine, so the habit of building several and calling each one to confirm the results differ per rate is the key. This problem deals with the area of how functions created in a loop capture values.
- What ability does this problem practice?
- Verifying that several functions created inside a loop each work with the right value.
- What cases should the tests feed in?
- Don't call just one function. Build functions for several rates and call each, asserting the result differs per rate.
- How do I run this problem?
- Use the standard library only and run the tests with python3 -m pytest -q. Add a case under tests/ for any new behavior.