A shop's till prints a total at the bottom of every receipt. You are auditing a batch of receipts: each one is a tuple (prices, printed_total), where prices is a list of item prices in pounds (floats such as 2.35) and printed_total is the total the till printed.
Write disagreeing_receipts(receipts) that returns the positions (0-based, in order) of the receipts whose items do not add up to the printed total.
Prices are stored as floats, so an honest receipt can add up to something like 0.30000000000000004 instead of 0.3. Treat the sum and the printed total as equal when they differ by at most 0.000001. Every real mistake on a receipt is at least one penny.
The helper till_roll(n, seed) returns n random receipts, about one in five with a wrong total, so you can try your function with Run.
Examples
Input: receipts = [([0.1, 0.2], 0.3), ([1.5, 2.25], 3.85), ([], 0.0)]
Output: [1]
Explanation: 0.1 + 0.2 is 0.30000000000000004, which agrees with 0.3 within the
tolerance. 1.5 + 2.25 = 3.75, ten pence short of 3.85. An empty receipt totals 0.0.
Input: receipts = [([0.7, 0.1, 0.2], 1.0), ([19.99], 20.0)]
Output: [1]
Constraints
0 <= len(receipts) <= 2000- each receipt has at most 40 prices, each between
0.01and50.00 - the result does not depend on randomness or the clock
Goals
- Compare floating-point results with a tolerance instead of ==
- Explain why 0.1 + 0.2 is not exactly 0.3