Similar Problems

Similar Problems not available

Calculate Amount Paid In Taxes - Leetcode Solution

Companies:

LeetCode:  Calculate Amount Paid In Taxes Leetcode Solution

Difficulty: Easy

Topics: array simulation  

Problem statement:

The problem statement is as follows:

Given an array of incomes, calculate the amount that each individual paid in taxes based on the following tax rate schedule:

  • For incomes between $0 and $10,000, the tax rate is 0%
  • For incomes between $10,001 and $20,000, the tax rate is 10%
  • For incomes between $20,001 and $50,000, the tax rate is 20%
  • For incomes over $50,000, the tax rate is 30%

You are required to return the list of calculated taxes for each input income rounded to the nearest hundredth.

Example:

Input: incomes = [5000, 10000, 15000, 20000, 30000, 40000, 50000, 60000, 70000]

Output: [0, 0, 500, 1000, 3000, 6000, 10000, 12000, 15000]

Explanation: For an income of $5000, the tax rate is 0%. So, the amount paid in taxes is 0.

For an income of $10,000, the tax rate is 0%. So, the amount paid in taxes is 0.

For an income of $15,000, the tax rate is 10%. So, the amount paid in taxes is (15000 - 10000) * 0.1 = 500.

Similarly, for incomes of $20,000, $30,000, $40,000, $50,000, $60,000, and $70,000, the calculated taxes are $1,000, $3,000, $6,000, $10,000, $12,000, and $15,000, respectively.

Solution:

The problem involves iterating over the given array of incomes and calculating the amount paid in taxes for each income based on the tax rate schedule. We can use a simple if-else ladder or a switch-case statement for this purpose.

Here is the step-by-step solution:

  1. Initialize an empty list to store the calculated taxes.
  2. Use a for loop to iterate over the given list of incomes.
  3. Inside the loop, use an if-else ladder or a switch-case statement to calculate the tax for the current income based on the tax rate schedule.
  4. Append the calculated tax to the list of taxes.
  5. Return the list of calculated taxes.

Here is the Python code for the solution:

def calculate_taxes(incomes):
    taxes = []
    
    for income in incomes:
        if income <= 10000:
            tax = 0
        elif income <= 20000:
            tax = (income - 10000) * 0.1
        elif income <= 50000:
            tax = 1000 + (income - 20000) * 0.2
        else:
            tax = 9000 + (income - 50000) * 0.3
        
        taxes.append(round(tax, 2))
    
    return taxes

In this code, we have used an if-else ladder to calculate the tax for each income based on the tax rate schedule. The round() function is used to round off the calculated tax to the nearest hundredth.

Let's test the function with the given example:

incomes = [5000, 10000, 15000, 20000, 30000, 40000, 50000, 60000, 70000]

print(calculate_taxes(incomes))  # Output: [0, 0, 500.0, 1000.0, 3000.0, 6000.0, 10000.0, 12000.0, 15000.0]

The function has correctly calculated the taxes for each income as per the given tax rate schedule.

Calculate Amount Paid In Taxes Solution Code

1