/**
 * Given the income, deductions, and tax brackets, calculate the income tax.
 *
 * @param income The total income
 * @param deductions The total deductions
 * @param taxBrackets A list of tuples representing the income tax brackets. Each tuple contains the lower bound and the respective tax rate.
 * @return The calculated income tax
 *
 * Example:
 * calculateIncomeTax(50000.0, 10000.0, listOf(Pair(0.0, 0.1), Pair(20000.0, 0.2), Pair(50000.0, 0.3))) // 10000.0
 */
fun calculateIncomeTax(income: Double, deductions: Double, taxBrackets: List<Pair<Double, Double>>): Double {

    // Sort the tax brackets in descending order
    val sortedTaxBrackets = taxBrackets.sortedByDescending { it.first }

    // Calculate the taxable income
    val taxableIncome = income - deductions

    // Iterate through the tax brackets
    for ((lowerBound, taxRate) in sortedTaxBrackets) {
        if (taxableIncome > lowerBound) {
            return (taxableIncome - lowerBound) * taxRate
        }
    }

    return 0.0
}
/**
 * Given the income, deductions, and tax brackets, calculate the income tax.
 *
 * @param income The total income
 * @param deductions The total deductions
 * @param taxBrackets A list of tuples representing the income tax brackets. Each tuple contains the lower bound and the respective tax rate.
 * @return The calculated income tax
 *
 * Example:
 * calculateIncomeTax(50000.0, 10000.0, listOf(Pair(0.0, 0.1), Pair(20000.0, 0.2), Pair(50000.0, 0.3))) // 10000.0
 */
fun calculateIncomeTax(income: Double, deductions: Double, taxBrackets: List<Pair<Double, Double>>): Double {
/**
 * Given the income, deductions, and tax brackets, calculate the income tax.
 *
 * @param income The total income
 * @param deductions The total deductions
 * @param taxBrackets A list of tuples representing the income tax brackets. Each tuple contains the lower bound and the respective tax rate.
 * @return The calculated income tax
 *
 * Example:
 * calculateIncomeTax(50000.0, 10000.0, listOf(Pair(0.0, 0.1), Pair(20000.0, 0.2), Pair(50000.0, 0.3))) // 10000.0
 */
fun calculateIncomeTax(income: Double, deductions: Double, taxBrackets: List<Pair<Double, Double>>): Double {

    // Sort the tax brackets in descending order
    val sortedTaxBrackets = taxBrackets.sortedByDescending { it.first }

    // Calculate the taxable income
    val taxableIncome = income - deductions

    // Iterate through the tax brackets
    for ((lowerBound, taxRate) in sortedTaxBrackets) {
        if (taxableIncome > lowerBound) {
            return (taxableIncome - lowerBound) * taxRate
        }
    }

    return 0.0
}