Gusto Payroll supports tax configurations that change by filing status, income range, and tax year. Given a paycheck and a dynamic collection of tax rules, calculate the employee's estimated federal withholding for that paycheck.
All monetary values are integer cents. Annualize the current paycheck and pre-tax deductions using pay_periods, select the matching rule with the greatest priority, and calculate tax progressively over the rule's brackets.
Implement calculate_withholding(gross_pay, pay_periods, pre_tax_deductions, filing_status, rules). gross_pay, pre_tax_deductions, and every rule amount are nonnegative integers in cents. pay_periods is a positive integer. rules is a list of objects with priority, statuses, income_min, income_max, standard_deduction, brackets, and credits.
A rule matches when the filing status is listed in statuses, annual gross income is at least income_min, and it is below income_max, unless income_max is null. Each bracket has an exclusive up_to limit in taxable-income cents, or null for infinity, and an integer rate_bps, where 10,000 basis points equals 100%. Each credit has an amount and optional max_taxable_income. Apply credits only when eligible, never allowing tax below zero. Round each bracket's tax and the final per-paycheck amount to the nearest cent, with halves rounded up.
def calculate_withholding(gross_pay, pay_periods, pre_tax_deductions, filing_status, rules):