brackets where brackets[i] = [upper_i, percent_i] represents the i^th tax bracket and a constant integer income representing your income.[upper, percent] where:
upper is the upper bound (inclusive) of the income while percent is the tax rate on the income within this bracket.i^th tax bracket is the upper bound of the (i-1)^th tax bracket plus 1.income is an integer.brackets be empty?
brackets will always contain at least one bracket.income will be covered by the given brackets.def calculateTax(brackets, income):
total_tax = 0
previous_upper_bound = 0 # This keeps track of the lower bound of the current bracket
for upper, percent in brackets:
if income > upper:
taxable_income = upper - previous_upper_bound
else:
taxable_income = income - previous_upper_bound
total_tax += taxable_income * percent / 100
# If the income is less than or equal to the upper bound, we've processed all taxable income
if income <= upper:
break
# Update previous upper bound to the current upper for the next iteration
previous_upper_bound = upper
return total_tax
# Example usage
brackets = [[3000, 10], [5000, 20], [10000, 30]]
income = 4500
print(calculateTax(brackets, income)) # Expected result is the calculated tax amount
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?