Fix the rounding diff check for a billing refactor
Your task
An agent refactored your billing module, and the unit tests still pass. A close read shows one change in behavior: the old proration code rounded half a cent up, and the new code rounds half to even (banker's rounding). Before you merge, you run both rounding rules over a large set of invoice rows and list every row where the amounts differ.
An assistant wrote proration_rounding_divergences(monthly_cents, days_used, days_in_period). It is in the editor. It has a bug. Find it and fix it.
Input and output:
monthly_cents,days_used,days_in_period: one invoice row per index: the monthly price in cents, the days the customer used, and the days in the billing period.- Return the indices of the rows where the two rules give different amounts, in increasing order.
Rules the tests check:
- If the lists differ in length, use only the indices all three have.
- Skip a row whose
days_in_periodis 0 or less. - Treat a negative price as 0. Clamp
days_usedto the range 0 todays_in_period. - The exact prorated amount, in cents, is price times days used, divided by days in the period. Work with exact integers, not floating point.
- Half up: round to the nearest whole cent, and an amount exactly halfway between two cents goes up.
- Half to even: round to the nearest whole cent, and an amount exactly halfway goes to whichever of the two neighbors is even.
- A row diverges when the two rules give different whole cents.
Examples
proration_rounding_divergences(monthly_cents: list[int], days_used: list[int], days_in_period: list[int]) -> list[int]Example 1
Input
proration_rounding_divergences([101], [15], [30])Output
[0]Example 2
Input
proration_rounding_divergences([103], [15], [30])Output
[]Example 3
Input
proration_rounding_divergences([1000, 101], [30, 15], [30, 30])Output
[1]