File: calculate-amount-paid-in-taxes/solution.py

Date: 2026-06-06

Time: 15:28

calculate-amount-paid-in-taxes/solution.py

Purpose

This file solves LeetCode 2303 — Calculate Amount Paid in Taxes. It implements a progressive tax calculator: given a set of tax brackets and an income, compute the total tax owed. The file is self-contained — it holds both the solution function and its unit tests.

Key Components

tax_amount(brackets, income) -> float (line 7)

The sole public function. Contract:

The function models how real progressive taxation works: each bracket taxes only the *marginal* income within that band, not the full income.

Flow

The loop (lines 17–22) walks brackets in order, maintaining prev as the lower bound of the current band:

1. Compute taxable = min(upper, income) - prev — the income falling within this band.

2. If taxable <= 0, income has been fully accounted for; break early.

3. Accumulate taxable * percent / 100 into tax.

4. Advance prev = upper to set the floor for the next band.

The min(upper, income) clamp is the key insight — it ensures the final bracket only taxes income up to the actual income, not the full bracket ceiling.

Patterns

Dependencies

Imports: only unittest from the standard library — no external dependencies.

Imported by: the testsolution.py in this directory, plus hundreds of other testsolution.py files across the repo. The "Imported By" list in the prompt is misleading — those other test files don't actually import *this* solution; they share the same structural pattern (each imports its own directory's solution). The real dependent is calculate-amount-paid-in-taxes/test_solution.py.

Invariants

Error Handling

None. The function trusts its inputs — no validation of bracket ordering, no checks for negative percentages or income. This is typical for LeetCode solutions where inputs are guaranteed by the problem constraints.

Tests

Seven test cases (lines 25–46) cover:

All use assertAlmostEqual to handle floating-point comparison, which is appropriate given the /100 division.

Topics to Explore

Beliefs