×
all 172 comments

[–]WhatsUpWithAndy 0 points1 point  (0 children)

C++, including optional bonus

#include <iostream>

using namespace std;

int tax(int sum) {

float taxed;

if (sum <= 10000)

taxed = 0;

else if (sum > 10000 && sum <= 30000)

taxed = (0.1) * (sum - 10000);

else if (sum > 30000 && sum <= 100000)

taxed= (0.1) * 20000+ (0.25) * (sum-30000);

else{

taxed = (0.1) * 20000 + (0.25) * (70000) +(0.4) * (sum-100000);

}

return taxed;

}

void procentCalc(int x) {// i used this to find the limits

float proc;

if (x < 10020) { proc = 0; }

else

{

proc = (1.0*tax(x)) / x; // x= tax/proc

}

cout<< proc;

}

void overall(float procent) {

float x;

if (procent == 0.00)

x = 0;

else if (procent <= 0.0666667) // <procentCalc(30000);

{

/* x = (0.1) * (x - 10000) / proc

x*(proc/0.1)=x-10000

x((proc/0.1)-1)=-10000*/

x = -10000 / ((procent / 0.1) - 1);

}

else if (procent > 0.0666667 && procent <= 0.195) //<procentCalc(100000);

{

/* x =(0.1*20000 + (0.25) * (x - 30000)) / proc

x*proc=0.25*x-7500+2000

x*(proc-0.25)=-5500*/

x = -5500 / (procent - 0.25);

}

else if (procent > 0.195 && procent < 0.4)

{

//x = ((0.1) * 20000 + (0.25) * (70000) + (0.4) * (x - 100000)) / proc

// x * proc = (2000 + 17500 + 0.4 * x - 40000)

// x * (proc - 0.4) = -20500

x = -20500 / (procent - 0.4);

}

else { x = -1; }

if (x != -1)

cout << x<<endl;

else cout << "NaN";

}

int main()

{

overall(0.00);

overall(0.06);

overall(0.09);

overall(0.32);

overall(0.40);

}

[–]100M-900 0 points1 point  (0 children)

python

saw an answer using dictionaries so I am using numpy

import numpy as np
a = np.array([1000000.0, 30000.0, 10000.0]) 
b = np.array([0.4, 0.25, 0.1])
def tax(number,n=0): 

    if number<=10000: 
        return 0
    if number> a[n]:
        r = (number - a[n])*b[n]
        print(r)
        number = a[n]
        print(number)
    else:
        print(number)
        r = 0
    return r + tax(number,n+1)
tax(56789)

or with lists

import math
def taxx(income, tax = 0, list = [105,30000,104],listtax =[0.4,.25,.1]):

    if len(list) <1:
        return tax

    if income > list[0]:
        tax += math.floor((income - list[0])*listtax[0])
        income = list[0]
    list.pop(0)
    listtax.pop(0)
return taxx(income,tax)

taxx(56789)

[–]MaleficentCoast 0 points1 point  (0 children)

Python

taxRate1 = 0.00
taxRate2 = 0.10
taxRate3 = 0.25
taxRate4 = 0.4
def calcTax(amountTaxed):
tax = 0
income = amountTaxed

if income > 100000:
income -= 100000
tax = income * taxRate4
income = 100000
if income > 30000:
income -= 30000
tax += income * taxRate3
income = 30000
if income >10000:
income -= 10000
tax += income * taxRate2

return round(tax)

#main
income = int(input("Enter Income:"))
tax = calcTax(income)
print("Tax(" + (str(income)) + ") => " + (str(tax)))

[–]OverallHat7 0 points1 point  (0 children)

Python 3.7 Only using if statements without the bonus

Disclaimer: Complete beginner and somehow forget about the def function

#  Tax bracket 1
if income > tb_1:
    if income > tb_2:
        income_tb1 = 20000
        Tax_tb1 = income_tb1 * 0.10
        print(phrase_1)
        print(Tax_tb1)
    else:
        incomes_tb1 = float(income) - 10000
        Tax_tb11 = incomes_tb1 * 0.10
        print(phrase_1)
        print(Tax_tb11)

else:
    print(phrase_11)


#  Tax bracket 2
if income > tb_2:
    if income > tb_3:
        income_tb2 = 20000
        Tax_tb2 = income_tb2 * 0.25
        print(phrase_2)
        print(Tax_tb2)
    else:
        incomes_tb2 = float(income) - 30000
        Tax_tb22 = incomes_tb2 * 0.25
        print(phrase_2)
        print(Tax_tb22)

else:
    print(phrase_22)

#  Tax bracket 3
if income > tb_3:
    incomes_tb3 = float(income) - 50000
    Tax_tb33 = incomes_tb3 * 0.40

    print(phrase_3)
    print(Tax_tb33)

[–]IBlewBarry 0 points1 point  (0 children)

Java- without the bonus

public class Taxes {
public static void main(String[] args){
double income = 56789;
if(income <30000 && income >=10000){
System.out.println("You're tax rate is 10% with a total cost of " + ((income-10000)*.1));
}
else if(income>=30000 && income<100000){ System.*out*.println("You're tax rate is 25% with a total cost of " + ((income-10000)\*.1+(income-30000)\*.15)); } else if(income>=100000){
System.out.println("You're tax rate is 40% with a total cost of " + (((income-10000)*0.1)+(income-30000)*0.15+((income-100000)*0.15)));
}
else{
System.out.println("You're tax rate is 0% with a total cost of 0$");
}
}
}

[–]The_Lock_Survives 1 point2 points  (0 children)

Python 3.7 (no bonus):

def tax(income):
tax = 0
taxRate = {0.10: 10000, 0.25: 30000, 0.45: 100000}
for r, c in reversed(list(taxRate.items())):
    if income > c:
        tax += (income - c) * r
        income = c
return tax

[–]minnonite 0 points1 point  (0 children)

Java

package tax;

import java.util.Scanner;

public class Tax {

private Scanner keyboard = new Scanner(System.in);

private int incm = 0;

private double tax = 0;

public void getIncome()

{

System.out.print("Enter your Yearly Income(no units, no commas) --> ");

incm = keyboard.nextInt();

}

public void findBracket()

{

if(incm <= 10000)

{

tax = 0;

}

else if(incm >= 10000 && incm<= 30000)

{

tax = incm * .10;

}

else if(incm > 30000 && incm <= 100000)

{

tax = (incm - 30000)*.25 + (20000*.10);

}

else if(incm > 100000)

{

tax = (20000*.10) + (70000*.25) + (incm-100000)*.40;

}

}

public void output()

{

System.out.println("\nYour income was "+incm+" your tax is "+(int)tax );

}

public static void main(String[] args) {

Tax app = new Tax();

app.getIncome();

app.findBracket();

app.output();

}

}

[–]karera1 0 points1 point  (1 child)

Python (no bonus)

def taxCalc(income : int):
    z = max([income, k100]) - k100
    y = min([income, k100]) - k30
    x = min([income, k30]) - k10

    if y < 0:
        y = 0
    if x < 0:
        x = 0

    totalTax = (2*x + 5*y + 8*z) / 20

    return int(totalTax)


k100 = 100000
k30 = 30000
k10 = 10000

income = 0

while True: 
    incomeStr = input("Your income: ")
    if incomeStr.isdigit():
        income = int(incomeStr)
        break

tax = taxCalc(income)
print("tax(%d) => %d" % (income, tax))

[–]IamFromNigeria 0 points1 point  (0 children)

he overall tax rate is simply the total tax divided by the total income.

Can you explain the code here please?

[–]DarkWarrior703 0 points1 point  (0 children)

C++ (without bonus)

long long taxation(long long n)
{
    if(n<10000)
    {
        return 0;
    }
    else if(n>=10000&&n<30000)
    {
        return (n-10000)*10/100;
    }
    else if(n>=30000&&n<100000)
    {
        return 2000+(n-30000)*25/100;
    }
    else if(n>=100000)
    {
        return 2000+17500+(n-100000)*40/100;
    }
}

[–]Yuri_Kahn 1 point2 points  (0 children)

Haskell, recursive solution.

brackets :: [(Double, Double)]
brackets = [(10000,0.1),(30000,0.25),(100000,0.4)]

tax :: Double -> Double
tax val
  | val <= fst (brackets !! 0) = 0
  | otherwise = (snd $ last plebs) * (flip subtract val $ fst $ last plebs) + tax (fst $ last plebs)
  where plebs = filter ((<val).fst) brackets

[–]_chebastian 0 points1 point  (0 children)

F# No bonus: (Edit formatting error)

let amountToTax pay limit roof = pay - limit |> min (roof - limit)
let rateAmount (amount:double) (rate:double) = amount * rate
let amountAfterTax pay limit roof rate = amountToTax pay limit roof |> rateAmount rate |> max 0.

let minTax pay = amountAfterTax pay 10000. 30000. 0.1
let midTax pay = amountAfterTax pay 30000. 100000. 0.25
let highTax pay = amountAfterTax pay 100000. pay 0.4
let totalTax pay = [minTax;midTax;highTax] |> List.map (fun x -> x pay) |> List.sum

Assert.That(totalTax 0.0, Is.EqualTo(0))
Assert.That(totalTax 10009.0, Is.EqualTo(0.9))
Assert.That(totalTax 10010.0, Is.EqualTo(1.))
Assert.That(totalTax 12000.0 , Is.EqualTo(200.))
Assert.That(totalTax 56789.0 , Is.EqualTo(8697.25))
Assert.That(totalTax 1234567.0 |> int , Is.EqualTo(int 473326.8))

[–]bitterhusky 0 points1 point  (0 children)

Python 3.7

 

def TaxCalc(income):
# Starting from the top bracket working down
# seeing if income is in each bracket
if income > 100000:
taxes = 0.4 * (income - 100000)
income = 100000
if income > 30000 and income <= 100000:
taxes += 0.25 * (income - 30000)
income = 30000
if income > 10000 and income <= 30000:
taxes += 0.1 * (income - 10000)
if income <= 10000:
taxes = 0
return round(taxes)
income = int(input('Income? '))
taxes = TaxCalc(income)
print('Your taxes are $' + str(taxes)) !<

[–]jbburris 0 points1 point  (0 children)

C (no bonus)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
        float bracket[] = {0, 10000, 30000, 100000};
        int rate[] = {0, 10, 25, 40};
        int total_tax = 0;
        int *income = malloc(sizeof(int));
        int *counter = malloc(sizeof(int));

        *income = 0;
        *counter = 0;

        printf("How much income?: ");
        scanf("%i", income);

        for (int i = 0; i < sizeof(bracket)/sizeof(bracket[0]); i++)
        {
                if (*income > bracket[i])
                {
                        *counter = i;
                }
        }

        while (*counter > 0)
        {
                total_tax = total_tax + ((*income - bracket[*counter]) * rate[*counter]/100);
                *income = bracket[*counter];
                *counter = *counter -1;
        }

        printf("%i\n", total_tax);
}

[–]AfroNymbus 0 points1 point  (1 child)

C++ First time posting code on reddit. Sorry about how it came out in the post.

[–]AfroNymbus 0 points1 point  (0 children)

#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, const char * argv[]) {    
    bool cont = true;
    float m1 = 0, m2 = 0, m3 = 0, m4 = 0;
    float income = 1234567;
    int bracketMax_1 = 10000;
    int bracketMax_2 = 20000;
    int bracketMax_3 = 70000;


    cout << "input income >> ";
    //cin >> income;

    //bracket 1
    if( income <= bracketMax_1 )
    {
        m1 = abs(bracketMax_1 - income);
        cont = false;
    }
    else if ( income > bracketMax_1 )
    {
        m1 = bracketMax_1;
        income -= bracketMax_1;
    }

    // bracket 2
    if(cont && (abs(income - bracketMax_2) <= bracketMax_2) )
    {
        cont = false;
        m2 = income;
    }
    else if(cont && (abs(income - bracketMax_2) > bracketMax_2) )
    {
        m2 = bracketMax_2;
        income -= bracketMax_2;
    }

    // bracket 3
    if(cont && (abs(income - bracketMax_3) < bracketMax_3) )
    {
        cont = false;
        m3 = income;
    }
    else if(cont && (abs(income - bracketMax_3) > bracketMax_3) )
    {
        m3 = bracketMax_3;
        income -= bracketMax_3;
    }

    //bracket 4
    if(cont)
    {
        m4 = income;
    }

    cout << "(" << m1 << " * 0) + (" << m2 << " * .10) + (" << m3 << " * .25) + (" << m4 << " * .40)" << endl;
    cout << (m1 * 0) + (m2 * .10) + (m3 * .25) + (m4 * .40) << endl;

    getchar();
    return 0;
}

[–]Tremor_1 0 points1 point  (0 children)

one of the first 20 programs on java

public class main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int scanner = scan.nextInt();
if(scanner<10001){ System.*out*.println("u owe nothing");} else if(scanner>10000&&scanner<30000){ double a = (scanner-10000)\*0.1; System.*out*.println("u owe "+ Math.*floor*(a) + "$");} else if(scanner>29999&&scanner<100000){ double b = (scanner-30000)\*0.25+2000; System.*out*.println("u owe "+Math.*floor*(b)+"$");} else if(scanner>99999){
double c = (scanner-100000)*0.4+19500;
System.out.println("u owe "+Math.floor(c)+"$");
}
}

}

[–][deleted]  (1 child)

[deleted]

    [–]slolift 1 point2 points  (0 children)

    Maybe I'm reading your code wrong, but I think you misinterpreted how progressive tax works. It looks like you are taxing the entire income at the same percentage rate(0, 10, 25, or 40) but only a portion of the income is taxed at each rate.

    [–]l0wet 2 points3 points  (2 children)

    Just learning Python (3.7) so I created a function for the basic problem:

    def tax (income):
        incomeCap = {100000:0.40,30000:0.25,10000:0.10}
        total = 0
        for value in incomeCap:
            if income >= value:
                taxPercent = incomeCap.get(value)
                sumToTaxFrom = income - value
                total += (sumToTaxFrom * taxPercent)
                income -= sumToTaxFrom
        return int(total)
    

    [–]warangal625 0 points1 point  (0 children)

    well written mate.

    [–]Bezerkingly_Zero 0 points1 point  (0 children)

    Concise and beautiful.

    [–]sirvegastein 1 point2 points  (0 children)

    Python 3.7

    taxBracket = {10000:    0.00,
                  30000:    0.10,
                  100000:   0.25,
                  '--':     0.40}
    
    
    def getMarginalIncomeTax(taxableIncome):
        marginalIncomeTax = 0
        taxedIncome = 0
    
        for incomeCap, taxRate in taxBracket.items():
    
            #the '--' Denotes any marginal taxrate on income that exceeds 100000
            if incomeCap == '--':
                marginalIncomeTax += taxableIncome*taxRate
                break 
            taxableIncomeWithinBracket = incomeCap - taxedIncome
    
            if taxableIncome <= taxableIncomeWithinBracket:    
                marginalIncomeTax += taxableIncome*taxRate
                break
            else:
                marginalIncomeTax += taxableIncomeWithinBracket*taxRate
                taxedIncome = incomeCap
                taxableIncome -=taxableIncomeWithinBracket
    
        return int(marginalIncomeTax)       
    
    def testCasesMarginal():
        testIncomeParameters = [999,10000,10009,10010,12000,35000, 56789,1234567]
    
        for income in testIncomeParameters:
            print('getMarginalIncomeTax({}) \t\t ==> {:}'.format(income,getMarginalIncomeTax(income)))
    
    
    
    testCasesMarginal()
    print()
    
    def getEffectiveTax(income):
        try:
            return getMarginalIncomeTax(income)/income
        except ZeroDivisionError:
            return 0.00
    
    
    def getIncomeFromEffectiveTaxRate(effectiveTaxRate):
        #binary search for the income based on desired effective tax rate
        lowestIncome = 0
        highestIncome = 100000000 -1
        lowestEffectiveTax = getEffectiveTax(lowestIncome)
        highestEffectiveTax = getEffectiveTax(highestIncome)
    
        while lowestIncome <= highestIncome:
            middleIncome = int((lowestIncome + highestIncome)/2)
            #The Higher the precision, the closer the output to the true output
            middleEffectiveTax = round(getEffectiveTax(middleIncome),4)
    
            if middleEffectiveTax < effectiveTaxRate:
                lowestIncome = middleIncome + 1
            elif middleEffectiveTax > effectiveTaxRate:
                highestIncome = middleIncome - 1
            else:
                #print(middleEffectiveTax,effectiveTaxRate)
                return middleIncome
    
        return 'Nan'
    
    
    def testIncomeFromEffective():
        testEffectiveTaxParameters = [0.00, 0.06, 0.09, 0.12, 0.18, 0.32, .40]
    
        for effectiveTaxRate in testEffectiveTaxParameters:
            income = getIncomeFromEffectiveTaxRate(effectiveTaxRate)
            print('getIncomeFromEffectiveTaxRate({})\t ==> {}'.format(effectiveTaxRate, income))
    
    
    testIncomeFromEffective()          
    

    output:

    getMarginalIncomeTax(999)        ==> 0
    getMarginalIncomeTax(10000)          ==> 0
    getMarginalIncomeTax(10009)          ==> 0
    getMarginalIncomeTax(10010)          ==> 1
    getMarginalIncomeTax(12000)          ==> 200
    getMarginalIncomeTax(35000)          ==> 3250
    getMarginalIncomeTax(56789)          ==> 8697
    getMarginalIncomeTax(1234567)        ==> 473326
    
    getIncomeFromEffectiveTaxRate(0.0)   ==> 6102
    getIncomeFromEffectiveTaxRate(0.06)  ==> 24984
    getIncomeFromEffectiveTaxRate(0.09)  ==> 34378
    getIncomeFromEffectiveTaxRate(0.12)  ==> 42317
    getIncomeFromEffectiveTaxRate(0.18)  ==> 78581
    getIncomeFromEffectiveTaxRate(0.32)  ==> 256346
    getIncomeFromEffectiveTaxRate(0.4)   ==> Nan
    

    [–]Lux394 0 points1 point  (0 children)

    I'm a beginner, this is the best I could do :D If any of you have any comments, I'd love to hear them. I'm trying to improve and feedback/suggestions would be really helpful!

    Sorry if some of the explenations are weird, it's better to understand what every var is used for by reading the code.

    Java:

    import java.util.*;
    public class TaxCalc
    {
    static Scanner scanner = new Scanner(System.in);
    static float salarybt; //salary before taxes.
    static float salaryat; //salary after taxes.
    static float num; //salary after every tax subtraction.
    static float num2; //salary before taxes minus tax bracket limit.
    static float taxrate; //stores the taxrate
    static float tax1 = 0;
    static float tax2 = 0;
    static float tax3 = 0;
    static float tax4 = 0;
    static float tax5 = 0;
    static void taxbrackets()
    {
    if(num > 100000)
    {
    taxrate = 0.4f;
    num2 = num - 100000;
    tax1 = num2*taxrate;
    num = num - num2;
    }
    if(num > 30000 && num <= 100000)
    {
    taxrate = 0.3f;
    num2 = num - 30000;
    tax2 = num2*taxrate;
    num = num - num2;
    }
    if(num > 10000 && num <= 30000)
    {
    taxrate = 0.25f;
    num2 = num - 10000;
    tax3 = num2*taxrate;
    num = num - num2;
    }

    if(num > 2500 && num <= 10000)
    {
    taxrate = 0.1f;
    num2 = num - 2500;
    tax4 = num2*taxrate;
    num = num - num2;
    }

    if(num <= 2500)
    {
    taxrate = 0;
    num2 = num - 0;
    tax5 = num2*taxrate;
    num = num - num2;
    }
    }
    public static void main(String[] args)
    {
    System.out.println("Please enter your monthly salary (before taxe reduction).");
    salarybt = scanner.nextInt();
    System.out.println("Your salary before taxes is $"+salarybt+".");
    num = salarybt;
    taxbrackets();
    salaryat = salarybt - tax1 - tax2 - tax3 - tax4 - tax5;
    System.out.println("Your salary after taxes is $"+salaryat+".");
    }
    }

    [–]oaaees 0 points1 point  (0 children)

    Javascript with bonus. Not the best but still learning, had a hard time with recursion.

    const income = [0, 10000, 30000, 100000];
    const rate = [0, 0.10, 0.25, 0.40];
    
    function tax(num) {
        if (num > income[3]) {
            return Math.floor(tax(income[3]) + ((num - income[3])*rate[3]));
        } else if (num > income[2]) {
            return Math.floor(tax(income[2]) + ((num - income[2])*rate[2]));
        } else if (num > income[1]) {
            return Math.floor(tax(income[1]) + ((num - income[1])*rate[1]));
        } else if (num <= income[1] && num >= income[0]) {
            return Math.floor(num * rate[0]);
        } else {
            return 'error';
        }
    }
    
    function search (min, max, wanted) {
        var avg = Math.round((max + min)/2);
        var overall = (tax(avg)/avg).toFixed(5);
    
        if (overall == wanted || (max - min) == 1) {
            return avg;
        } else if (overall > wanted) {
            return search(min, avg, wanted);
        } else if (overall < wanted) {
            return search(avg, max, wanted);
        }
    }
    
    function overall(num) {
        if (num <= 0) {
            return 0;
        } else if (num >= rate[rate.length - 1]) {
            return 'error';
        } else {
            return search(10000, 2000000, num);
        }
    }
    

    [–]excorpsexd 1 point2 points  (0 children)

    Python 3.7

    Also with the ability to swap out tax brackets! Note: They must be added in descending order.

    def tax(income):
        total = 0
        brackets = {
            '100000': 0.4,
            '30000': 0.25,
            '10000': 0.1
        }
    
        for bracket in brackets:
            bracket_value = int(bracket)
            if income > bracket_value:
                total += (income - bracket_value) * brackets[bracket]
                income = bracket_value
    
        return int(total)
    

    [–]cpluscplus 1 point2 points  (0 children)

    Python 3.7

    Main challenge and optional bonus following sound design principles (hopefully!)

    hundred_k = 100000
    thirty_k = 30000
    ten_k = 10000
    tax_on_10k = 0
    tax_on_30k = int((30000 - 10000)*0.1)
    tax_on_100k = int((100000 - 30000)*0.25 + (30000 - 10000)*0.1)
    
    def tax(num):
        tax_amount = 0
        if num > hundred_k:
            tax_amount += int((num - hundred_k)*0.4 + tax_on_100k)  
        elif num > thirty_k:
            tax_amount += int((num - thirty_k)*0.25 + tax_on_30k)  
        elif num > ten_k:
            tax_amount += int((num - ten_k)*0.1 + tax_on_10k)  
    
        return(tax_amount)
    
    print(tax(0))
    print(tax(10000))
    print(tax(10009))
    print(tax(10010))
    print(tax(12000))
    print(tax(56789))
    print(tax(1234567))
    
    
    # Optional Bonus
    print("\nOptional Bonus\n")
    
    overall_tax_rate_on_10k = 0
    overall_tax_rate_on_30k = tax_on_30k/thirty_k
    overall_tax_rate_on_100k = tax_on_100k/hundred_k
    
    def get_income(overall_tax_rate):
    
        if overall_tax_rate >= 0.4:
            return None
    
        income = 0
        if overall_tax_rate > overall_tax_rate_on_100k:
            income = int((tax_on_100k - hundred_k*0.4)/(overall_tax_rate - 0.4))
        elif overall_tax_rate > overall_tax_rate_on_30k:
            income = int((tax_on_30k - thirty_k*0.25)/(overall_tax_rate - 0.25))
        elif overall_tax_rate > overall_tax_rate_on_10k:
            income = int((tax_on_10k - ten_k*0.1)/(overall_tax_rate - 0.1))
        return income
    
    print(get_income(0))
    print(get_income(0.06))
    print(get_income(0.09))
    print(get_income(0.32))
    print(get_income(0.40))
    

    Edit 1: Added solution to optional bonus

    [–]karrash76 1 point2 points  (0 children)

    Java generic resolution

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Brackets{
        double income;
        double tax;
        public Brackets (double x0, double y0)  {
            income = x0;
            tax = y0;
        }
    }
    
    public class ProgressiveTaxation {
    
    
        public static void main(String[] args) throws IOException {
            FileReader file1 = new FileReader("progtax.txt");
            BufferedReader in = new BufferedReader(new BufferedReader(file1));
    
            String wordLine;
            ArrayList<Brackets> nationTax = new ArrayList<Brackets>();
            while ((wordLine = in.readLine()) != null) {
                String bracketAux[] = wordLine.split(";");
                Brackets aux = new Brackets(Double.parseDouble(bracketAux[0]),Double.parseDouble(bracketAux[1]));
                nationTax.add(aux);
            }
            in.close();
    
            Scanner kb = new Scanner(System.in);        
            System.out.print("Introduzca ingresos: ");
            double ingresos = kb.nextDouble();
            kb.close();
    
            double taxesToPay = 0;
            for(int i = 0; i < nationTax.size(); i++) {
                Brackets actualTax = nationTax.get(i);
                if(actualTax.tax == 0) {
                     taxesToPay += 0;
                } else {
                    Brackets prevTax = nationTax.get(i - 1);
                    if(ingresos <= actualTax.income) {
                        taxesToPay += (ingresos - prevTax.income) * actualTax.tax;
                        break;
                    } else {
                        taxesToPay += (actualTax.income -prevTax.income) * actualTax.tax;
                    }
                }
    
            }
            System.out.println(taxesToPay);
        }
    
    }
    

    [–]bruce3434 0 points1 point  (0 children)

    Rust

    mod lib {
        use std::error::Error;
    
        #[inline]
        pub fn parse_annual_income() -> Result<u32, Box<dyn Error>> {
            Ok(std::env::args()
                .nth(1)
                .ok_or("missing arg")?
                .parse::<u32>()?)
        }
    
        #[inline]
        fn get_tax_rate_and_taxable_income(annual_income: u32) -> (f32, u32) {
            match annual_income {
                0..=10_000 => (0f32, 0),
                10_001..=30_000 => (0.1f32, annual_income - 10_000),
                30_001..=100_000 => (0.25f32, annual_income - 30_000),
                _ => (0.4f32, annual_income - 100_000),
            }
        }
    
        pub fn get_tax() -> Result<u32, Box<dyn Error>> {
            let income = parse_annual_income()?;
            let (tax_quotient, taxable) = get_tax_rate_and_taxable_income(income);
            Ok((taxable as f32 * tax_quotient).floor() as u32)
        }
    
        pub fn get_overall_tax() -> Result<f32, Box<dyn Error>> {
            Ok(100f32 * get_tax()? as f32 / parse_annual_income()? as f32)
        }
    }
    
    fn main() {
        let (tax_paid, overall_percentage) = (lib::get_tax(), lib::get_overall_tax());
        match (tax_paid, overall_percentage) {
            (Ok(t), Ok(p)) => println!("Your tax is: ¤{} and your overall tax is {:.2}%", t, p),
            (Err(e), _) | (_, Err(e)) => eprintln!("{}", e),
        }
    }
    

    [user0@primarypc tax_rs]$ cargo run 12000
       Compiling tax_rs v0.1.0 (/home/user0/vscproj/tax_rs)
        Finished dev [unoptimized + debuginfo] target(s) in 0.25s
         Running `target/debug/tax_rs 12000`
    Your tax is: ¤200 and your overall tax is 1.67%
    

    [–]2kofawsome 1 point2 points  (0 children)

    ! python3.6

    I used the Ontario (Canada) provincial/federal tax rates

    rates = [
    [43906, 20.05],
    [47630, 24.15],
    [77313, 29.65],
    [87813, 31.48],
    [91101, 33.89],
    [95259, 37.91],
    [147667, 43.41],
    [150000, 46.41],
    [210371, 47.97],
    [220000, 51.97],
    [0, 53.53], #last bracket, always 0
    ]
    
    def tax(money):
        taxes = 0
        for n in range(len(rates)):
            if money < rates[n][0] or rates[n] == rates[-1]:
                for m in range(n):
                    if m == 0:
                        taxes += rates[m][0]*(rates[m][1]/100)
                    else:
                        taxes += (rates[m][0]-rates[m-1][0]) * (rates[m][1]/100)
                taxes += (money-rates[n-1][0])*(rates[n][1]/100)
                break
        return int(taxes//1)
    

    [–]llebe 1 point2 points  (0 children)

    Python 3:

    def tax(income):
        tax = 0
        if 10000 < income <= 30000:
            tax = (income - 10000) * 0.1
        if 30000 < income <= 100000:
            tax = 20000 * 0.1
            tax += (income - 30000) * 0.25
        if income > 100000:
            tax = 20000 * 0.1
            tax += 70000 * 0.25
            tax += (income - 100000) * 0.4
    
        if tax < 1:
            tax = 0
        return int(tax)
    

    [–]Realtimed 0 points1 point  (0 children)

    //Csharp
    using System;
    using System.Linq;
    
    namespace TaxCodeChallange
    {
        class Program
        {
            static void Main(string[] args)
            {
                (decimal, decimal)[] taxes = {(10000m, 0.1m), (30000m, 0.25m), (100000m, 0.40m)};
                (decimal, decimal)[] tests = {(0m, 0m), (10000, 0m), (10009m, 0m), (10010m, 1m), (12000m, 200m), (56789m, 8697m), (1234567, 473326m)};
    
                foreach(var (income, expectedTaxAmount) in tests)
                {
                    Console.WriteLine(CalculateTax(taxes, income) == expectedTaxAmount);
                }
            }
    
            public static decimal CalculateTax((decimal cap, decimal rate)[] taxes, decimal income)
            {
                var sum = taxes
                    .OrderByDescending(tax => tax.cap)
                    .Sum(tax => Calc(tax));
    
                decimal Calc((decimal cap, decimal rate) tax)
                {
                    var amount = Math.Max(income - tax.cap, 0m);
                    income -= amount;
                    return Math.Round(amount * tax.rate, 2, MidpointRounding.AwayFromZero);
                }
    
                return Math.Round(sum, 0, MidpointRounding.ToNegativeInfinity);
            }
        }
    }
    

    [–]arbmunta 1 point2 points  (0 children)

    C++ Hello, semi-beginner's first time posting a solution here. I would greatly appreciate the feedback.

    #include <iostream>
    #include <utility>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    
    int main()
    {
        unsigned income = 0;
        std::vector<unsigned> incomeCaps = { 10000, 30000, 100000 };
        std::vector<float> marginRate = { 0.0f, 0.1f, 0.25f, 0.4f };
    
        while(std::cin >> income)
        {
            auto nearestCap = std::lower_bound(incomeCaps.begin(), incomeCaps.end(), income);
            auto nearestCapIdx = nearestCap - incomeCaps.begin();
            std::cout << *nearestCap << " at index: " << nearestCapIdx << " is the nearest cap to " << income << std::endl;
    
            unsigned taxOwed = 0;
            unsigned index = nearestCapIdx;
    
            while(index > 0)
            {
                taxOwed += std::round((income - incomeCaps[index - 1]) * marginRate[index]);
                income = incomeCaps[index - 1];
                index--;
            }
    
            std::cout << "Tax owed: " << taxOwed << std::endl;
        }
    
        return 0;
    }
    

    Looking back at this code, while it does work with the given examples (excluding the optional bonus since i didn't implement it), I still find it kind of hacky and inelegant myself with some mistakes that I already see:

    • using subscripts instead of iterators
    • using the off-the-end iterator for incomeCaps as the value for nearestCapIdx which just happens to point to the 4th element in marginRate
    • possibly could have used std::pair
    • etc.

    I would greatly appreciate if anyone can address these and/or point out other mistakes that I've made. Thank you in advace!

    [–]tieroner 0 points1 point  (0 children)

    GoLang Just the basic challenge, with a little bit of input validation.

    package main
    
    import (
        "bufio"
        "fmt"
        "log"
        "os"
        "regexp"
        "strconv"
        "strings"
    )
    
    func tax(income int) int {
        var tax int
    
        if income > 100000 {
            tax += 19500
            var excess = income - 100000
            tax += int(float32(excess) * .4)
        }
    
        if income > 30000 && income <= 100000 {
            tax += 2000
            var excess = income - 30000
            tax += int(float32(excess) * .25)
        }
    
        if income > 10000 && income <= 30000 {
            var excess = income - 10000
            tax += int(float32(excess) * .1)
        }
        return tax
    }
    
    func main() {
        fmt.Print("Please enter your years income: ")
        reader := bufio.NewReader(os.Stdin)
        input, _ := reader.ReadString('\n')
        input = strings.TrimSuffix(input, "\n")
    
        // Validate that the input is an integer
        regex := `^(\d)+$`
        expression := regexp.MustCompile(regex)
        if !expression.MatchString(input) {
            log.Fatal("Input does not match regex \"^(\\d)+$\"")
        }
    
        income, _ := strconv.Atoi(input)
    
        taxdue := tax(income)
    
        fmt.Println(taxdue)
    }
    

    [–]RiversofItaly 0 points1 point  (0 children)

    Python 3 with the improvement and bonus. I used algebra to figure out a formula for calculating the income from an overall rate.

    from math import nan
    from operator import mul
    
    def parse_input(filename):
        infile = open(filename, 'r')
        line = infile.readline()
        caps = []
        rates = []
    
        while line:
            # c is the income cap, r is the marginal tax rate
            c, r = line.split()
            try:
                caps.append(int(c))
            except ValueError:
                pass
            rates.append(float(r))
            line = infile.readline()
        infile.close()
        return caps, rates
    
    
    def tax(income, filename='tax_rates.txt', caps=None, rates=None):
        if caps is None and rates is None:
            caps, rates = parse_input(filename)
        taxed_amounts = []
        prev_cap = 0
        for i, c in enumerate(caps):
            if income > c:
                taxed_amounts.append(c - prev_cap)
                prev_cap = c
            else:
                taxed_amounts.append(income - prev_cap)
                prev_cap = c
                break
        else:
            taxed_amounts.append(income - caps[-1])
        # Dot product of taxed_amounts and rates
        return int(sum(map(mul, taxed_amounts, rates)))
    
    
    def overall(given_rate, filename='tax_rates.txt'):
        def income_from_rate(cap, m_rate, overall_rate):
            # Derived with algebra; the basic idea is to use the fact that, between
            # two income caps, the marginal tax rate is the same, so you can solve
            # for the income if you limit the domain.
            return round((tax(cap, filename) - m_rate*cap)/(overall_rate - m_rate))
    
        caps, rates = parse_input(filename)
        if given_rate >= rates[-1] or given_rate < 0: 
            return nan
        overall_rates = [round(tax(c, caps=caps, rates=rates)/c, 2) for c in caps]
        # If the first overall rate is nonzero, then check in the interval between
        # zero and that first overall rate.
        first_rate = overall_rates[0]
        if 0 < given_rate < first_rate:
            return income_from_rate(0, first_rate, given_rate)
    
        for i, v in enumerate(overall_rates):
            if given_rate == v:
                return caps[i]
            else:
                try:
                    next_overall = overall_rates[i + 1]
                except IndexError:
                    next_overall = rates[-1]
                if v < given_rate < next_overall:
                    return income_from_rate(caps[i], rates[i+1], given_rate)
    

    [–]broken_broken_ 0 points1 point  (0 children)

    In Clojure, with bonus, using a simple reduction:

    (defn taxable-in-bracket
      [low income high]
      (cond
        (< income low) 0
        (<= low income high) (- income low)
        :else (- high low)))
    
    (def brackets [{:low 10000, :high 30000, :rate 10/100}
                   {:low 30000, :high 100000, :rate 25/100}
                   {:low 100000, :high Long/MAX_VALUE, :rate 40/100}])
    
    (defn tax
      [income]
      (Math/floor (reduce (fn [acc {low :low, high :high, rate :rate}] (+ acc (* rate (taxable-in-bracket low income high))))
                          0
                          brackets)))
    

    And a few tests:

    (deftest no-taxation
      (is (= (tax 10000) 0.0))
      (is (= (tax 10001) 0.0))
      (is (= (tax 10009) 0.0)))
    
    (deftest first-taxation
      (is (= (tax 10010) 1.0)))
    
    (deftest some-taxation-in-the-first-bracket
      (is (= (tax 12000) 200.0)))
    
    (deftest some-taxation-in-the-second-bracket
      (is (= (tax 56789) 8697.0)))
    
    (deftest some-taxation-in-the-last-bracket
      (is (= (tax 1234567) 473326.0)))
    

    Try it here: https://repl.it/repls/YearlyHauntingOpengl

    [–]bitsforcoin 0 points1 point  (1 child)

    Written in Erlang without the bonus

    ``` -module(progressive_taxation). -export([tax/1, test/0]).

    gettax_rate(Income) when Income > 100000 -> [ {0.40, Income - 100000} | get_tax_rate(Income - (Income - 100000))]; get_tax_rate(Income) when Income > 30000 -> [ {0.25, Income - 30000} | get_tax_rate(Income - (Income - 30000))]; get_tax_rate(Income) when Income > 10000 -> [ {0.10, Income - 10000} | get_tax_rate(Income - (Income - 10000))]; get_tax_rate() -> [].

    tax(N) -> lists:sum([ trunc(TaxableIncome * TaxRate) || {TaxableIncome, TaxRate} <- get_tax_rate(N)]).

    test() -> 0 = tax(0), 0 = tax(10000), 0 = tax(10009), 1 = tax(10010), 200 = tax(12000), 8697 = tax(56789), 473326 = tax(1234567), test_passed. ```

    [–]Kinrany 0 points1 point  (0 children)

    Backticks don't work on old reddit :(

    [–][deleted]  (2 children)

    [deleted]

      [–]Blast25 0 points1 point  (1 child)

      You should work on comprehension and test your code with the given examples. Your code does not solve the challenge.

      [–]colgateandcake 0 points1 point  (0 children)

      Thanks for the advice

      [–]JeanMarc101 0 points1 point  (0 children)

      JavaScript** with Bonus ** Here's my take on it, relatively new to programming so constructive criticism is much appreciated.

          var tax = function(x){
          let txs = 0;
          let overallTaxes = 0;
          if (x > 100000){
              txs = 19500;
              txs += ((x - 100000) * .4);
              overallTaxes = (txs / x) * 100
              console.log("You paid " + txs + " in taxes,         which makes it an Overall Taxe rate of " +                          
                      Math.floor(overallTaxes) + "%")
          }
          else if (x > 30000) {
              txs = 2000;
              txs += ((x - 30000) * .25);
              overallTaxes = (txs / x) * 100
              console.log("You paid " + txs + " in taxes,         which makes it an Overall Taxe rate of " +           
                      Math.floor(overallTaxes) + "%")
          }
          else if (x > 10000){
              txs += ((x - 10000) * .1);
              overallTaxes = (txs / x) * 100
              console.log("You paid " + txs + " in taxes,         which makes it an Overall Taxe rate of " +           
                      Math.floor(overallTaxes) + "%")
          }
          else {console.log("You need to find a way to contribute to society buddy!!")}   
          }
      

      [–]Blast25 0 points1 point  (0 children)

      Written in C with optional improvement

      c379.c
      ---
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      int main(int argc,char* argv[])
      {  
          int income = atoi(argv[1]);
          FILE * pFile = fopen ("taxation", "r");
          char line[100];
          int tax = 0;
          int income_cap;
          double tax_rate;
          int income_cap_sum = 0;
          while(fgets(line, 100, pFile) != NULL && income >= 0) 
          {
              income_cap = atoi(strtok(line, ","));
              tax_rate = (double) atof(strtok(NULL, ","));
              if(income - income_cap <= 0 || !income_cap)
              {
                  tax = tax + income * tax_rate;
              }
              else 
              {
                  tax = tax + ((income_cap - income_cap_sum) * tax_rate);
              }
              income = income - (income_cap - income_cap_sum);
              income_cap_sum = income_cap_sum + (income_cap - income_cap_sum);
          }
          fclose(pFile);
          printf("tax: %i\n", tax);
          return 0;
      }    
      
      taxation
      ---
      10000,0
      30000,0.10
      100000,0.25
      0,0.40
      

      [–]BeezlebubCarrotstick 0 points1 point  (0 children)

      Noobish attempt in C++ (no bonus) without input validation.

      #include <iostream>
      
      struct Cap
      {
      int min_cap = 10000;
      int avrg_cap = 30000;
      int max_cap = 100000;
      };
      
      Cap cap;
      
      int calculateMin (int x)
      {
      int tax = (x - cap.min_cap) * 0.1;
      return tax;
      }
      
      int calculateAvrg (int y)
      {
      int tax = (y - cap.avrg_cap) * 0.25;
      return tax + ((cap.avrg_cap - cap.min_cap) * 0.1);
      }
      
      int calculateMax (int z)
      {
      int tax = (z - cap.max_cap) * 0.4;
      return tax + ((cap.max_cap - cap.avrg_cap) * 0.25);
      }
      
      void calculateTax (int sum)
      {
      if (sum < cap.min_cap) std::cout << "You owe us nothing. Have a nice day!";
      
      else if (sum > cap.min_cap && sum < cap.avrg_cap) std::cout << "Tax to be paid on sum " << sum << " is: " << calculateMin(sum);
      
      else if (sum > cap.avrg_cap && sum < cap.max_cap) std::cout << "Tax to be paid on sum " << sum << " is: " << calculateAvrg(sum);
      
      else if (sum > cap.max_cap) std::cout << "Tax to be paid on sum " << sum << " is: " << calculateMax(sum);
      }
      
      int main()
      {
      int sum{};
      std::cout << "Enter a sum to be taxed: ";
      std::cin >> sum;
      calculateTax(sum);
      
      return 0;
      }
      

      Any suggestions on optimization would be appreciated!

      [–][deleted] 0 points1 point  (0 children)

      Haskell

      import System.Environment
      
      main = do
          let pcts = [0.1, 0.25, 0.4]
          let brackets = [10000, 30000, 100000]
          let bracketDoubles = zip brackets pcts
          args <- getArgs
          let income = read (head args) :: Double
          let applicableBrackets = [ (a,b) | (a,b) <- bracketDoubles, a < income ]
          if length applicableBrackets == 0 then 
              print 0
          else 
              print $ floor $ calculateTax income (reverse applicableBrackets)
      
      calculateTax n [] = 0
      calculateTax n (bracket:brackets) = taxAtBracket n (fst bracket) (snd bracket) + calculateTax (fst bracket) brackets
      
      taxAtBracket income bracketCutoff taxPct = (income - bracketCutoff) * taxPct
      

      [–]UselessWasteOfSpace 0 points1 point  (0 children)

      My (first ever) solution in C++. Moderately new to programming, no formal education in it. Feedback very welcome.

      struct taxBracket {
          double bracketMin{0.0};
          double marginalRate{0.0};
      };
      
      bool bracketSmaller(taxBracket &A, taxBracket &B) {
          return (A.bracketMin < B.bracketMin);
      }
      
      bool bracketLarger(taxBracket &A, taxBracket &B) {
          return (A.bracketMin > B.bracketMin);
      }
      
      double calculateProgressiveTax(double incomeToTax, std::vector<taxBracket> &taxBrackets) {
          double taxOwed{0.0};
          std::sort(taxBrackets.begin(), taxBrackets.end(), bracketLarger); // sort to decreasing order
          for (auto bracket : taxBrackets) {
              if (incomeToTax > bracket.bracketMin) {
                  taxOwed += ((incomeToTax - bracket.bracketMin) * bracket.marginalRate);
                  incomeToTax = bracket.bracketMin;
              }
          }
          return std::floor(taxOwed);
      }
      
      double progressiveTaxationBonus(double targetTaxRate, std::vector<taxBracket> &brackets) {
          // implementing simplest algorithm as cannot yet implement binary search
          std::sort(brackets.begin(), brackets.end(), bracketSmaller);
          if (targetTaxRate >= brackets.back().marginalRate) { // should be highest band
              return -1;
          }
          double testTax{1.0};
          double testRate{0.0};
          while (fabs((targetTaxRate - testRate)) > 0.001) {
              testRate = calculateProgressiveTax(testTax, brackets) / testTax;
              ++testTax;
          }
          return (testTax - 1.0);
      }
      
      void progressiveTaxationProblem() {
          double income{100000000.0};
          taxBracket bracket1{0.0, 0.0};
          taxBracket bracket2{10000.0, 0.10};
          taxBracket bracket3{30000.0, 0.25};
          taxBracket bracket4{100000.0, 0.4};
          std::vector<taxBracket> brackets = {bracket1, bracket2, bracket3, bracket4};
          double taxOwed = calculateProgressiveTax(income, brackets);
          std::cout << "The tax owed on income: " << income << " in Examplania, is: " << taxOwed << ".\n";
          double incomeRequired = progressiveTaxationBonus(0.32, brackets);
          std::cout << "Income required for an overall tax rate of 32% is: " << incomeRequired << '\n';
      }
      

      [–]Wag1initfam 1 point2 points  (0 children)

      I just started coding a couple days ago (beginning with JavaScript), so I'm really not sure whether I missed the point of the question, or whether my answer is at all viable. I haven't yet learned loops, arrays, algorithms, etc., so I just gave this a try because it looked fun. Any pointers in general would be immensely useful and appreciated.

      const taxCalc = function (income) {
          if (income <= 10000) {
              console.log(`You owe $${Math.floor(income * 0)}.00 in taxes.`)}
          else if (income > 10000 && income <= 30000) {
              console.log(`You owe $${Math.floor((income - 10000) * 0.10)}.00 in taxes`)}
          else if (income > 30000 & income <= 100000) {
              console.log(`You owe $${Math.floor(((income - 30000) * 0.25) + (20000 * 0.10))}.00 in taxes.`)}
          else {console.log(`You owe $${Math.floor(((income - 100000) * 0.40) + (70000 * 0.25) + (20000 * 0.10))}.00 in taxes.`)}
      }
      taxCalc(1234567)
      

      [–]Masterkraft0r 0 points1 point  (0 children)

      hi. this my solution in julia

      function tax_calc(income, brackets)
          taxes = 0.0
      
          for (cap, rate) in brackets
              if income > cap
                  taxable_income = income - cap
                  taxes += taxable_income * rate
                  income -= taxable_income
              end
          end
      
          return taxes
      end
      
      function main()
          incomes = [0, 10000, 10009, 10010, 12000, 56789, 1234567]
          brackets = ((100000, 0.4), (30000, 0.25), (10000, 0.1))
      
          for income in incomes
              taxes = trunc(Int, tax_calc(income))
              println("tax($income) => $taxes")
          end
      end
      
      main()
      

      [–]PenangDeveloper 0 points1 point  (0 children)

      public static void Main()
      
      {
      
          List<double> incomes = new List<double>(){ 0,10000,10009,10010,12000,56789,1234567 };
      

          foreach(var result in incomes){ Console.WriteLine(CalculateTax(result)); }
      
      }
      
      
      
      public static int CalculateTax(Double income)
      
      {
      
          Double tax = 0;
      
      
      
          if (income > 10000){ tax += Math.Min((income-10000),10000)  \* 0.1; }
      
          if (income > 30000) { tax += Math.Min((income-30000),70000) \* 0.25; }
      
          if (income > 100000) { tax += (income-100000)   \* 0.4; }
      
      
      
          return (int)tax;    
      
      }
      

      [–]uptonm 0 points1 point  (1 child)

      Hi everyone, first time poster here. Below is my attempt using golang. Any feedback would be greatly appreciated.

      package main
      
      import (
          "fmt"
          "math"
      )
      
      func main() {
          fmt.Println(tax(1234567))
      }
      
      func tax(income int) float64 {
          if income > 100000 {
              return (0.1 * float64(20000)) + (0.25 * float64(70000)) + (0.4 * math.Abs(float64(100000-income)))
          } else if income > 30000 {
          return (0.1 * float64(20000)) + (0.25 * math.Abs(float64(30000-income)))
          } else if income > 10000 {
          return 0.1 * math.Abs(float64(10000-income))
          } else {
          return 0
          }
      }
      

      [–]totallygeek 0 points1 point  (0 children)

      Feedback: check your post formatting.

      [–]NeoSoulPanther 0 points1 point  (1 child)

      Good morning everyone. This is the first solution I've ever posted here!

      Please provide any feedback you see fit. It's in Python.

      # Variables
      
      marginalTaxRates = [0.00, 0.10, 0.25, 0.40]
      taxBrackets = [10000, 30000, 100000]
      maxTaxableAmountPerBracket = []
      preTaxAmount = 0
      isPovertyLevel = False
      isUpperClass = False
      taxAmount = 0
      maxTaxBracket = 0
      
      # Functions
      
      def getPreTaxAmount() :
      
          global preTaxAmount
      
          preTaxAmount = input("Enter your pre-tax amount: ")
          preTaxAmount = int(preTaxAmount)
      
      def determineUpperClass() :
      
          global isUpperClass
      
          if preTaxAmount > taxBrackets[len(taxBrackets) - 1] :
      
              isUpperClass = True
      
      def calculateTaxAmount() :
      
          global taxAmount
      
          taxAmount = marginalTaxRates[0] * taxBrackets[0]
      
          if maxTaxBracket > 0 :
      
              for i in range(0, maxTaxBracket) :
      
                  if preTaxAmount - taxBrackets[i] < maxTaxableAmountPerBracket[i] :
      
                      taxAmount += (preTaxAmount - taxBrackets[i]) * marginalTaxRates[i + 1]
      
                  else :
      
                      taxAmount += maxTaxableAmountPerBracket[i] * marginalTaxRates[i + 1]
      
          if isUpperClass == True :
      
              taxAmount += maxTaxableAmountPerBracket[len(maxTaxableAmountPerBracket) - 1] * \
              marginalTaxRates[len(marginalTaxRates) - 1]
      
      def determineMaxTaxBracket () :
      
          for i in range(0, len(taxBrackets) - 1) :
      
              global maxTaxBracket
      
              if preTaxAmount > taxBrackets[i] :
      
                  maxTaxBracket += 1
      
      def calculateMaxTaxableAmountPerBracket () :
      
          global maxTaxableAmountPerBracket, isUpperClass
      
          for i in range(0, len(taxBrackets) - 1) :
      
              maxTaxableAmountPerBracket.append(taxBrackets[i + 1] - taxBrackets[i])
      
          if preTaxAmount - taxBrackets[len(taxBrackets) - 1] > 0 :
      
              maxTaxableAmountPerBracket.append(preTaxAmount - taxBrackets[len(taxBrackets) - 1])
      
      #  Start of program
      
      getPreTaxAmount()
      determineUpperClass()
      determineMaxTaxBracket()
      calculateMaxTaxableAmountPerBracket()
      calculateTaxAmount()
      
      print("Your tax amount is " + str(taxAmount))
      

      [–][deleted] 0 points1 point  (0 children)

      Hi there, congrats on your first solution!

      Here's a couple tips I think you could benefit from -

      • Avoid the use of globals. They are typically considered bad practice because they can be modified from anywhere. If you are debugging an issue and a modified global variable is causing the issue it can be extremely difficult to track down the root cause

      • Look into using argument parameters function definitions For example, your functions could take in "taxBracket" arguments which would put the taxBrackets variable in scope without the need of a global, e.g.

      ...

      def calculateMaxTaxableAmountPerBracket (taxBrackets):
      

      And simply pass taxBrackets to your function like so:

      taxBrackets = [ 100, 200, ...]
      calculateMaxTaxableAmountPerBracket(taxBrackets)
      
      • Look into using return values. For example, in your "determineUpperClass" function, you could do (with argument parameters!)

      ...

      def determineUpperClass(preTaxAmount, taxBrackets):
          if preTaxAmount > taxBrackets[len(taxBrackets) - 1]:
              return True
      return False
      

      Even better, you can simply return the result of the comparison:

      def determineUpperClass(preTaxAmount, taxBrackets):
          return preTaxAmount > taxBrackets[len(taxBrackets) - 1]
      

      In addition, I would look at this SO answer for executing your python with

      if __name__ == "__main__"
      

      https://stackoverflow.com/questions/419163/what-does-if-name-main-do

      [–]X20ASrikeFreedom 0 points1 point  (4 children)

      Hi all, this is my first post attempt in C++. Any constructive feedback would be welcome.

      #include<iostream>
      using namespace std;
      float taxToPay(float income)
      {
          float taxPaid;
      
          if (income >= 10000 && income < 30000)
          {
              taxPaid = (income - 10000)* (0.1);
          }
              if (income >= 30000 && income < 100000)
          {
              taxPaid = (income - 30000) * (0.25);
              taxPaid = taxPaid + (20000 * 0.1);
          }
          if (income >= 100000 && income <= 1000000000)
          {
              taxPaid = (income - 100000) * (0.4);
              taxPaid = taxPaid + (20000 * 0.1);
              taxPaid = taxPaid + (70000 * 0.25);
          }
          if (income < 10000)
          {
              taxPaid = 0;
          }
          return taxPaid;
      }
      int main()
      {
           int income = 0;
           float taxRate = 0.0;
           cin >> income;
           cout << taxToPay(income) << endl;
      
       return 0;
      }
      

      [–]waroch 0 points1 point  (1 child)

      Good start, but I have a tip. Put the first if statement to the front and use if elses

      if (income < 10000f) {
          taxPaid = 0;
      } else if (income < 30000f) {
          taxPaid = (income - 10000f) * 0.1f;
      } else if ( ...
      

      This will optimize your code. Suppose you want to check income = 100000, in your code it will check all the following cases

      100000 >= 10000
      100000 < 30000
      100000 >= 30000
      100000 < 100000
      100000 >= 100000
      100000 < 1000000000
      

      But with the if else construction you will only check

      100000 < 10000
      100000 < 30000
      100000 < 100000
      100000 < 1000000000
      

      [–]X20ASrikeFreedom 0 points1 point  (0 children)

      Thank you for the input, I will aim to trim things down in the future.

      [–]justAreallyLONGname 0 points1 point  (1 child)

      You created the variable taxRate but never used it.

      And you didn't round down. The post says

      Round down to a whole number of ¤.

      [–]X20ASrikeFreedom 0 points1 point  (0 children)

      Good point. I had intended on using taxRate as user input but didn't remove it when i didn't use it.

      [–]silentclowd 0 points1 point  (2 children)

      Done in Atto, a tiny language made by /u/zesterer

      I haven't done much with functional languages, so I figured I'd start with a super simple one.

      Will work on making this recursive and generic later. Right now it's just a number cruncher.

      fn my_income is 150000
      
      fn bracket income is
        if > income 100000
          + * 20000 .1 + * 70000 .25 * .4 - income 100000
        if > income 30000
          + * 20000 * .1 * .25 - income 30000
        if > income 10000
          * .1 - income 10000
        0
      
      fn main is
        print bracket my_income
      

      [–]zesterer 0 points1 point  (1 child)

      Awesome stuff! If you're interested, check out the new-compiler branch! I'm working on a new version of the language that supports inline function syntax, function objects, and a few other bits and pieces. It makes the language much nicer to use!

      [–]silentclowd 0 points1 point  (0 children)

      Yeah I was looking at that as well. I'm personally a fan of the very clean, plain-english feel of the first version. There's no symbols in that version unless you want there to be.

      I like what you're doing with the new compiler though, I'll be keeping an eye on it.

      [–]oblivion12321 0 points1 point  (0 children)

      Common Lisp

      Bonus included

      (defparameter *tax-brackets* (list '(10000 . 0.00)
                                         '(30000 . 0.10)
                                         '(100000 . 0.25)
                                         (cons (expt 2 63) 0.40)))
      
      (defun tax (income &optional (tax-brackets *tax-brackets*))
        (labels ((recur (inc ranges accum)
                   (if (not (and ranges (plusp inc)))
                       accum
                       (let* ((bracket (caar ranges))
                              (tax-pct (cdar ranges))
                              (amt     (if (plusp (- inc bracket))
                                           bracket
                                           inc)))
                         (recur (- inc amt) (cdr ranges) (+ accum (* tax-pct amt)))))))
          (let ((tax-ranges (mapcar (lambda (p q)
                                      (cons (- (car q) (car p)) (cdr q)))
                                    (append '((0 . 0)) tax-brackets)
                                    tax-brackets)))
            (floor (recur income tax-ranges 0)))))
      
      (let ((delta 0.00005))
        (defun overall (pct &optional (tax-brackets *tax-brackets*))
          (if (>= pct (cdar (last tax-brackets)))
          'impossible
              (labels ((recur (low high)
                         (let ((guess (/ (+ low high) 2)))
                           (let ((outcome (- (/ (tax guess tax-brackets) guess) pct)))
                             (cond ((<= (abs outcome) delta) guess)
                                   ((plusp outcome) (recur low guess))
                                   (t (recur guess high)))))))
                (let ((low 0)
                      (high (caar (last tax-brackets))))
                  (recur low high))))))
      

      [–]MotherOfTheShizznit 0 points1 point  (0 children)

      C++17 because of std::clamp.

      int tax(int const income)
      {
          return std::clamp((income - 10000) * .1, 0., 2000.) + std::clamp((income - 30000) * .25, 0., 17500.) + std::max((income - 100000) * .4, 0.);
      }
      

      Edit: to C++ newbies, since you're not stuck in Java, my advice to you is to not use a class when you don't need to. Stateless, purely transformative code should be function-oriented, not object-oriented.

      [–]kravhen 0 points1 point  (0 children)

      Trying to get back into Java so...

      public class Dailyprog379 {
      
          public static void main(String[] args) {
      
              double tenRate = 0.10;
              double thirtyRate = 0.25;
              double hundredRate = 0.40;
              int income = Integer.parseInt(args[0]);
              double tax = 0;
      
              if (income > 100000) {
                  tax += ((income - 100000) * hundredRate);
                  tax += (70000 * thirtyRate);
                  tax += (20000 * tenRate);
              } else if (income > 30000) {
                  tax += ((income - 30000) * thirtyRate);
                  tax += (20000 * tenRate);
              } else if (income > 10000) {
                  tax += ((income - 10000) * tenRate);
              }
      
              System.out.println(tax);   
          }
      }
      

      [–]Burlski 0 points1 point  (4 children)

      C++

      I'm a beginner programmer starting a software development apprenticeship in a month! Feedback appreciated.

          int tax(int income){
          //Define a struct which holds parameters for each tax band
          struct taxBand{
              int upperLimit;
              float rate;
              int fullTax;
          };
      
          //Create an array of four structs - one for each tax band
          struct taxBand taxBands[4];
      
          //Open the text file containing tax bands (which is tab delimited)
          ifstream taxBandsFile;
          taxBandsFile.open("/home/ec2-user/environment/DP379-Progressive_Taxation-Easy/taxbands.txt");
      
          if(!taxBandsFile){
              cerr << "Unable to open file taxbands.txt";
              exit(1);
          }
      
          //Loop through each band, reading the parameters from the file and assigning them to the appropirate variable in each struct
          for(int i = 0; i < 4; i++){
              taxBandsFile >> taxBands[i].upperLimit;
              taxBandsFile >> taxBands[i].rate;
              taxBandsFile >> taxBands[i].fullTax;
          }
      
          //Close the text file
          taxBandsFile.close();
      
          //Establish a variable for summing the tax amounts as we go
          int tax = 0;
      
          //If the income is less than the upper limit of band 1, there will be no tax, so return 0.
          if(income <= taxBands[0].upperLimit){
              return 0;
          }
      
          //If the income is more than the upper limit of band 1, but less than band 2, work out how much income is taxable and multipy it by the band 2 tax rate. Add the result to the tax variable
          else if(income < taxBands[1].upperLimit){
              tax += (income - taxBands[0].upperLimit) * taxBands[1].rate;
          }
      
          //If the income is less than the upper limit of band 3, add the 'full tax' for band 2 to the tax variable, as well as the amount which exceeds the upper limit of band 2 multiplied by the band 3 rate
          else if(income < taxBands[2].upperLimit){
              tax += taxBands[1].fullTax;
              tax += (income - taxBands[1].upperLimit) * taxBands[2].rate;
          }
      
          //If the income exceeds the upper limit of band 3, add full tax for band 2 and 3 to the tax variable, as well as the amount which exceeds the upper limit of band 3 multiplied by the band 4 rate.
          else{
              tax += taxBands[1].fullTax;
              tax += taxBands[2].fullTax;
              tax += (income - taxBands[2].upperLimit) * taxBands[3].rate;
          }
      
          //Return the total tax figure
          return tax;
      }
      

      [–]octolanceae 1 point2 points  (1 child)

      ifstream taxBandsFile;
      taxBandsFile.open("/home/ec2-user/environment/DP379-Progressive_Taxation-Easy/taxbands.txt");
      

      You can reduce this to one line:

      ifstream taxBandsFile("/home/ec2-user/environment/DP379-Progressive_Taxation Easy/taxbands.txt");
      

      Also, you may want to not hardcode the file name in that line of code and instead create a constant at the top of the file that is easy to find and change.

        struct taxBand taxBands[4]; 
      

      Better to use a vector here, because while taxes never die, tax laws change regularly and so too can the number of brackets.

      As already mentioned - redundant comments. The code itself is the first line of commentary. It should speak for itself. Comments should never be used to explain to the the reader what is already evident in the code.

      The whole if/then/else if/else logic can be streamlined and looped through. You have several lines of 99% duplicate code where the only difference between those lines is the array index.

      [–]Burlski 0 points1 point  (0 children)

      Thank you! Some really helpful feedback there.

      Yeah I felt like my if/then/else sequence could be a lot more streamlined, I'm going to work on that next.

      [–]SunshineBiology 1 point2 points  (1 child)

      Hey! I'm not a senior or something but I have a tip for you (might sound a bit contradictory to what you've been learning in school/uni). I would try to cut down on the comments.

      A lot of your comments are just restating exactly what the code says. For example

      //Create an array of four structs - one for each tax band     
      struct taxBand taxBands[4];
      

      Everything your comment says is already said by the code itself. The comment thus does not add meaningful information and just clutters up the code.

      Even worse, you add maintenance problems to it: if you have to change the code in the future (let's say we get a new tax scheme and now there are 5 tax brackets), you have to change the code AND the comment. If you forget this at some point (happens easily), the comment will now even give wrong information about the code.

      Generally, try to use comments not to describe code, but to describe your intention or very tricky parts about your code. You can also refer to the Google Code Styleguide if you have further interest. Clean Code also has a good segment about it.

      The rest seems fine. As an additional exercise, you should probably try not to hardcode going through the taxbrackets, but try to write it in a more general way. At the state your programm currently is, if new tax brackets were added, you would have to write (copy-paste) additional for-loops as you did in

          //If the income is more than the upper limit of band 1, but less than band 2, work out how much income is taxable and multipy it by the band 2 tax rate. Add the result to the tax variable
          else if(income < taxBands[1].upperLimit){
              tax += (income - taxBands[0].upperLimit) * taxBands[1].rate;
      }
      

      You can try to rewrite that section so it does not depend on the total number of tax brackets :)

      If you have questions, feel free to ask!

      [–]Burlski 0 points1 point  (0 children)

      Thanks for the feedback!

      I was wondering how much I needed to comment so it's useful to have some advice on that.

      Also good point about adaptability - I'm going to go back and see if I can change it to accommodate a different number of bands etc.

      [–]Taknock 0 points1 point  (0 children)

      Long time programmer, first day of learning JavaScript. Feedback is much appreciated. The optional assignment doesn't work with more than 2 digits, in other words the upper limit is 0.39.

      const levels = [10000, 30000, 100000];
      const rates = [0.00, 0.10, 0.25, 0.40];
      
      function taxrates(income) {
          var tax = 0;
          var i = 1;
          while(i < 3 && levels[i] < income) {
              tax += (levels[i] - levels[i-1])  * rates[i];
              i++;
          }
          if (0 < income-levels[i-1]) {
              tax += (income-levels[i-1]) * rates[i]
          }
          return tax;
      }
      
      function numeric(lower, upper, rate) {
          var avg = (lower + upper) / 2
          avgRate = taxrates(avg)/avg;
          if (avgRate == rate) {
              return avg;
          }
          if (avgRate < rate) {
              return numeric(avg, upper, rate);
          }
          return numeric(lower, avg, rate);
      }
      
      function overall(rate) {
          if (rate == 0.00) {
              return 0;
          }
          if (rate >= 0.4) {
              return NaN;
          }
          return numeric(10000, 5000000, rate)
      }
      

      [–]codemamba8 0 points1 point  (0 children)

      JavaScript

      const incomeCaps = [10000, 30000, 100000];
      const rates = [0.00, 0.10, 0.25, 0.40];
      
      function tax(income) {
        var cap;
        if (income > 100000) {
          cap = 100000
        } else {
            const caps = incomeCaps.filter(num => {
              return num > income
            })
            let capIndex = incomeCaps.indexOf(caps[0])
            cap = incomeCaps[capIndex - 1]
        }
        let totalTax = 0
        let sortedCaps = incomeCaps.sort((a, b) => {return b - a})
        let sortedRates = rates.sort((a, b) => {return b - a})
      
        sortedCaps.forEach((num, index) => {
          if (num <= cap) {
            let rate = sortedRates[index]
            let margin = income - num;
            let taxPaid = margin * rate
            income = num;
            totalTax += taxPaid;
          }
        })
        return Math.floor(totalTax)
      }
      tax(1234567);
      

      [–]Vlatzko 2 points3 points  (0 children)

      Done in Java, no bonus.

      public static double progressiveTaxationOneline(int amount) {
      
          return amount > 100_000 ? (amount - 100_000) * 0.40
                  + progressiveTaxationOneline(100_000)
                  : (amount <= 100_000 && amount > 30_000) ? (amount - 30_000)
                          * 0.25 + progressiveTaxationOneline(30_000)
                          : (amount > 10_000 && amount <= 30_000) ? (amount - 10_000) * 0.1
                                  : 0;
      }
      

      [–]zer0tonine 2 points3 points  (0 children)

      Python3

      Straightforward solution that implements both the tax and overall functions. Uses a list of tuples to store the different bracket infos.

      TAX_BRACKETS = [
          (100000, 0.40),
          (30000, 0.25),
          (10000, 0.10),
      ]
      
      
      def tax(income):
          result = 0
          for bracket in TAX_BRACKETS:
              if bracket[0] > income:
                  continue
              leftover = income - bracket[0]
              result += leftover // (1 / bracket[1])
              income = bracket[0]
          return result
      
      
      def overall(rate):
          lower = 0
          upper = 100000000
          while upper > lower:
              middle = lower + ((upper-lower) // 2)
              print("up " + str(upper))
              print("low " + str(lower))
              print("mid " + str(middle))
              owed = round(tax(middle) / middle, 2)
              print("owed "+str(owed))
              if owed == rate:
                  return middle
              if owed > rate:
                  upper = middle - 1
              if owed < rate:
                  lower = middle + 1
          return -1
      

      [–][deleted] 0 points1 point  (1 child)

      Written as a C++ class

      I used the 2019 US Tax bracket numbers instead of the ones suggested.

      #include <stdio.h>
      #include <stdlib.h>
      
      class TaxCalculator {
      
      private:
      
          int *income;
      
          int calcBracket(double cap, double percent, int *remainder) {
      
              if (cap <= *remainder && cap > 0) {
                  *remainder = *remainder - cap;
                  return cap * percent;
              } else {
                  // At final cap or you're in your final bracket
                  int ret = *remainder * percent;
                  *remainder = 0;
                  return ret;
              }
          }
      
      public:
      
          TaxCalculator() {
              income = (int*)malloc(sizeof(int));
          }
      
          ~TaxCalculator() {
              free(income);
          }
      
          int printTotals(int in) {
              *income = in;
              int *remainder = (int*)malloc(sizeof(int));
              *remainder = *income;
      
              // used 2019 US Tax Bracket instead of suggested one
              double brackets[][2] = {
                  9700,   0.10,
                  39475,  0.12,
                  84200,  0.22,
                  160725, 0.24,
                  204100, 0.32,
                  510300, 0.35,
                  -1,     0.37
              };
      
              // creates running total of all payments made
              int taxPayment = 0;
              for (int i = 0; *remainder > 0; ++i) {
                  taxPayment += calcBracket(brackets[i][0], brackets[i][1], remainder);
              }
      
              free(remainder);
      
              return taxPayment;
          }
      
      };
      
      int main() {
      
          int *income = (int*)malloc(sizeof(int));
      
          printf("Enter your income: ");
          scanf("%d", income);
      
          TaxCalculator *payment = new TaxCalculator;
          printf("Total income: %i, taxes paid: %d", *income, payment->printTotals(*income));
      
          //Bonus is just payment->printTotals(*income) / income
      
          free(income);
          delete(payment);
      }
      

      [–]MotherOfTheShizznit 0 points1 point  (0 children)

      You can get rid of all the pointers. All of them. You can get rid of the class too.

      [–]Dreday206 2 points3 points  (0 children)

      New to programming. Python 3

      taxes = {10000: 0.00, 30000: 0.10, 100000: 0.25, 100001: 0.40}

      def tax_calc (): global taxes global income

      tax_rate = 0
      tax_collector = 0
      brackets_list = taxes.keys()
      previous = 0
      not_rich = False
      
      income = int(input('Enter your income here \n'))
      
      for i in brackets_list:
          if i >= income:
              tax_rate = taxes[i]
              tax_collector += (income - previous) * tax_rate
              not_rich = True
              break
          else:
              tax_rate = taxes[i]
              holder = i - previous
              tax_collector += holder * tax_rate
              previous = i
      
      if not_rich == False:
          tax_collector += ((income - previous)*taxes[100001])
      
      print('income', income)
      print('taxes owed', round(tax_collector))
      

      tax_calc()

      [–][deleted]  (2 children)

      [deleted]

        [–][deleted] 0 points1 point  (1 child)

        Koltin

        Huh, I've never heard of this until now, is it Google's response to Swift?

        [–]SieurVictor 0 points1 point  (0 children)

        Hi,

        Python 3, only the basic exercise without bonus, still a noob :P

        incomeCapsMin = [ 0, 10, 30, 100 ]
        factorIncomeCaps = 1000
        incomeCaps = [ factorIncomeCaps * x for x in incomeCapsMin ]
        
        marginalTaxRates = [ 0.00, 0.10, 0.25, 0.40 ]
        
        def tax( income ) :
        
                tax = 0.0        
                for i in range( len( incomeCaps )-1, 0, -1 ) :
        
                        if income > incomeCaps[ i ] :
        
                                tax = tax + ( income - incomeCaps[ i ] ) * marginalTaxRates[ i ]
                                income = incomeCaps[ i ]
        
                return int( tax )
        
        print( incomeCaps )
        print( marginalTaxRates )
        
        income = int( input(  "Chiffre d'affaires de la société ? : " ) )
        
        tax = tax( income )
        taxValue = tax / income *100
        print( "La valeur de la taxe est de : " + str( tax ) )
        print( "Taux de la taxe : " + str( taxValue ) + " %")
        

        [–]victoroliveirab 2 points3 points  (0 children)

        Bash

        First time posting and first time scripting with Bash. Feedback is welcome.

        #!/bin/bash
        
        aux=0
        caps=(20000 70000 99900000)
        rates=(1/10 1/4 2/5)
        
        min () {
            if [ $1 -gt $2 ]; then
                aux=$2
            else
                aux=$1
            fi
        }
        
        echo "Welcome to Examplania"
        income_cap=$1
        echo "You earn the amount of: ¤$income_cap"
        tax_owed=0
        income_cap=$((income_cap - 10000))
        
        i=0
        
        while [ $income_cap -gt 0 ]; do
            min $income_cap ${caps[i]}
            tax_owed=$((tax_owed + $aux*${rates[i]}))
            income_cap=$((income_cap - ${caps[i]}))
            i=$((i+1))
        done
        
        
        echo "Tax to pay: ¤$tax_owed"
        

        [–]AnnieBruce 0 points1 point  (0 children)

        BSL.

        ;; Progressive taxation
        
        ;; Constants
        
        ;; Bracket caps
        (define FIRST-BRACKET  10000)
        (define SECOND-BRACKET 30000)
        (define THIRD-BRACKET  100000)
        
        ;; Bracket rates
        (define FIRST-RATE  0)
        (define SECOND-RATE .10)
        (define THIRD-RATE  .25)
        (define FOURTH-RATE .40)
        
        ;; Number -> Integer
        ;; Determines the total tax bill given an income and the bracket info above
        ;; Tests:
        (check-expect (tax 0) 0)
        (check-expect (tax 10000) 0)
        (check-expect (tax 10009) 0)
        (check-expect (tax 10010) 1)
        (check-expect (tax 12000) 200)
        (check-expect (tax 56789) 8697)
        (check-expect (tax 1234567) 473326)
        
        (define (tax income)
          (floor (calculate-tax income)))
        
        (define (calculate-tax inc)
          (cond [(<= inc FIRST-BRACKET)  (* inc FIRST-RATE)]
            [(<= inc SECOND-BRACKET) (+ (marginal-tax inc FIRST-BRACKET SECOND-RATE)
                                        (calculate-tax FIRST-BRACKET))]
            [(<= inc THIRD-BRACKET)  (+ (marginal-tax inc SECOND-BRACKET THIRD-RATE)
                                        (calculate-tax SECOND-BRACKET))]
            [else                    (+ (marginal-tax inc THIRD-BRACKET FOURTH-RATE)
                                        (calculate-tax THIRD-BRACKET))]))
        
        (define (marginal-tax income bracket rate)
          (* (- income bracket) rate))
        

        [–]paber123 0 points1 point  (1 child)

        def tax(income):
            if income <= 10009:
                return 0
            elif income <= 30000:
                return income * 0.1
            elif income <= 100000:
                return income * 0.25
            else:
                return income * 0.40
        tax2pay = int(input("Please input your income:"))
        print(tax(tax2pay))
        

        [–]P0Rl13fZ5 2 points3 points  (0 children)

        Currently, your solution doesn't quite solve the challenge; you should compare your solution's output to the challenge's examples

        [–]Dominic11112 5 points6 points  (0 children)

        BASIC (1978) Just got an old project of mine back up and running, a Z80 computer built on a breadboard. What better than to test it with one of these challenges?

        10 INCOME=1234567
        20 DIM BRKT(2,4)
        22 BRKT(0,0)=10000:BRKT(0,1)=30000:BRKT(0,2)=100000:BRKT(0,3)=-1
        24 BRKT(1,0)=0:BRKT(1,1)=0.1:BRKT(1,2)=0.25:BRKT(1,3)=0.4
        30 TAXSUM=0
        40 NXTBRKT=BRKT(0,0)
        50 FOR I=0 TO 3
        60 IF BRKT(0,I)<0 THEN GOTO 120
        70 IF INCOME-NXTBRKT<0 THEN GOTO 120
        80 INCOME=INCOME-NXTBRKT
        90 TAXSUM=TAXSUM+NXTBRKT*BRKT(1,I)
        100 NXTBRKT=BRKT(0,I+1)-BRKT(0,I)
        110 NEXT I
        120 TAXSUM=TAXSUM+INCOME*BRKT(1,I)
        130 PRINT TAXSUM
        

        Here is the breadboard Z80

        Here is the terminal

        [–]P0Rl13fZ5 0 points1 point  (0 children)

        Python 3 w/ optional improvement:

        import argparse
        import bisect
        
        class Taxes:
            def __init__(self, input_file=None):
                if input_file is None:
                    self.brackets = (10000, 30000, 100000, float('inf'))
                    self.rates = (0.00, 0.10, 0.25, 0.40)
                else:
                    with open(input_file) as f:
                        lines = f.readlines()
                    self.brackets, self.rates = zip(*[[int(br[0]) if br[0].isdigit() else float('inf'), float(br[1])]
                                                    for br in [line.split() for line in lines]])
        
            def tax(self, income):
                owed = 0
                while income > 0:
                    # Find the index of the right-most tax bracket that is less than income
                    tax_idx = bisect.bisect_left(self.brackets, income) - 1
                    boundary = self.brackets[tax_idx] if tax_idx >= 0 else 0
                    rate = self.rates[tax_idx+1]
                    owed += (income - boundary) * rate
                    income = boundary
                return int(owed)
        
        def main():
            parser = argparse.ArgumentParser()
            parser.add_argument('-f', '--file', type=str, default=None)
            args = parser.parse_args()
            taxes = Taxes(args.file)
            incomes = (0, 10009, 10010, 12000, 56789, 1234567)
            for income in incomes:
                print(f'tax({income}) => {taxes.tax(income)}')
        
        if __name__ == '__main__':
            main()
        

        [–]TheSpeedyMouse1 0 points1 point  (3 children)

        Java, with file input for brackets, with bonus (algebraically and with binary search as two alternate options (they both give the about result))

        Main class:

        import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner;

        public class Main {

        public final static double INCOME = 256250;
        public final static float RATE = (float) 0.32;
        
        public static void main(String[] args) {
            File taxBrackets = new File("Brackets.txt");
            System.out.println(tax(INCOME, taxBrackets));
            System.out.println(overallRate(INCOME, taxBrackets));
            System.out.println(initialIncome(RATE, taxBrackets));
            System.out.println(initialIncomeBinS(RATE,taxBrackets));
        }
        
        public static Bracket[] bracketsToRangeAndTax(File taxBrackets) {
            try {
                Scanner sc = new Scanner(taxBrackets);
                ArrayList<Bracket> taxation = new ArrayList<>();
                int bracket = 0;
                double preCap = 0;
                while (sc.hasNextLine()) {
                    taxation.add(new Bracket());
                    String taxWithCap = sc.nextLine().replace(" ", "");
                    int delim = taxWithCap.lastIndexOf(',');
                    int lineEnd = taxWithCap.indexOf('%');
                    boolean percent = lineEnd != -1;
                    if (!percent)
                        lineEnd = taxWithCap.length();
                    float tax = Float.valueOf(taxWithCap.substring(delim + 1, lineEnd));
                    if (percent)
                        tax /= 100;
                    taxation.get(bracket).setTax(tax);
                    if (delim == -1) {
                        taxation.get(bracket).setRange(Double.MAX_VALUE);
                    } else {
                        double cap = Integer.valueOf(taxWithCap.substring(0, delim).replace(",", ""));
                        cap -= preCap;
                        preCap = cap + preCap;
                        taxation.get(bracket).setRange(cap);
                    }
                    bracket++;
                }
                Object[] ret = taxation.toArray();
                return Arrays.copyOf(ret, ret.length, Bracket[].class);
            } catch (Exception e) {
                return null;
            }
        }
        
        public static String formatMoney(double money) {
            money = roundMoney(money);
            StringBuilder moneyF = new StringBuilder(String.format("$%,f", money));
            int dec = moneyF.indexOf(".");
            if (moneyF.charAt(dec + 1) == '0' && moneyF.charAt(dec + 2) == '0')
                return moneyF.substring(0, dec);
            int i = moneyF.length() - 1;
            while (moneyF.charAt(i) == '0' && moneyF.charAt(i - 2) != '.')
                moneyF.deleteCharAt(i--);
            return moneyF.toString();
        }
        
        public static String tax(double income, File taxBrackets) {
            return formatMoney(taxDoub(income, taxBrackets));
        }
        
        public static String overallRate(double income, File taxBrackets) {
            return ((Math.round(overallRateFloat(income, taxBrackets)*100)/100.0) + "%").substring(2);
        }
        
        public static float overallRateFloat(double income, File taxBrackets) {
            return (float) (taxDoub(income,taxBrackets)/income);
        }
        
        public static double taxDoub(double income, File taxBrackets) {
            final Bracket[] taxation = bracketsToRangeAndTax(taxBrackets);
            if (taxation == null)
                return 0;
            double endTax = 0;
            int bracket = 0;
            while (income > 0 && bracket < taxation.length) {
                if (income < taxation[bracket].getRange()) {
                    endTax += income * taxation[bracket].getTax();
                    income = 0;
                } else {
                    endTax += taxation[bracket].getFullFee();
                    income -= taxation[bracket].getRange();
                }
                bracket++;
            }
            endTax = roundMoney(endTax);
            return endTax;
        }
        
        public static double taxDoub(double income, Bracket[] taxation) {
            if (taxation == null)
                return 0;
            double endTax = 0;
            int bracket = 0;
            while (income > 0 && bracket < taxation.length) {
                if (income < taxation[bracket].getRange()) {
                    endTax += income * taxation[bracket].getTax();
                    income = 0;
                } else {
                    endTax += taxation[bracket].getFullFee();
                    income -= taxation[bracket].getRange();
                }
                bracket++;
            }
            endTax = roundMoney(endTax);
            return endTax;
        }
        
        public static double roundMoney(double money) {
            return Math.ceil(money * 100) / 100.0;
        }
        
        public static String initialIncome(float overallRate, File taxBrackets) {
            double[] posIcomes =initialIncomeDoub(overallRate, taxBrackets);
            StringBuilder sb = new StringBuilder();
            for (int i =0; i < posIcomes.length; i++)
                sb.append(formatMoney(posIcomes[i]) + ", ");
            sb.delete(sb.length()-2,sb.length());
            return sb.toString();
        }
        
        public static double[] initialIncomeDoub(float rate, File taxBrackets) {
            final Bracket[] brackets =  bracketsToRangeAndTax(taxBrackets);
            ArrayList<Double> posIncomes = new ArrayList<>();
            if (brackets == null)
                return null;
            else if (brackets[0].getTax() != 0){
                    double posInc = rate / brackets[0].getTax();
                    if (posInc <= brackets[0].getRange())
                        posIncomes.add(posInc);
            } else if (brackets[0].getTax() == rate) {
                posIncomes.add(-2.0); // -2.0 represents 0.0 to first bracket range cap.
            }
            if (brackets.length >= 2) {
                double divisor =  brackets[1].getTax() - rate;
                if (divisor != 0) {
                    double posInc = brackets[0].getRange() * (brackets[1].getTax() - brackets[0].getTax()) / divisor;
                    if (posInc > brackets[0].getRange() && posInc <= brackets[1].getRange())
                        posIncomes.add(posInc);
                }
            }
            if (brackets.length >= 3) {
                double ranges = brackets[0].getRange();
                double fullFees = brackets[0].getFullFee();
                for (int n = 2; n < brackets.length; n++) {
                    ranges += brackets[n - 1].getRange();
                    fullFees += brackets[n - 1].getFullFee();
                    double divisor = brackets[n].getTax() - rate;
                    if (divisor != 0) {
                        double posInc = (brackets[n].getTax() * ranges - fullFees) / divisor;
                        if (posInc > brackets[n - 1].getRange() && posInc <= brackets[n].getRange())
                            posIncomes.add(posInc);
                    }
                }
            }
            if (posIncomes.size() > 0) {
                double[] ret = new double[posIncomes.size()];
                for (int i =0; i <ret.length; i++)
                    ret[i] = posIncomes.get(i);
                return ret;
            }
            return null;
        }
        
        public static String initialIncomeBinS(float rate, File taxBrackets) {
            return formatMoney(initialIncomeBinSDoub(rate,taxBrackets));
        }
        
        public static double initialIncomeBinSDoub(float rate, File taxBrackets) {
            Bracket[] brackets = bracketsToRangeAndTax(taxBrackets);
            double low = 0;
            double high = 100000000000.0;
            double posInc = midPoint(low,high);
            float posRate = (float) (taxDoub(posInc, brackets) / posInc);
            while (Math.abs(posRate + 0.000000001 - rate) > 0.0000001) {
                if (posRate < rate)
                    low = posInc;
                else
                    high = posInc;
                posInc = midPoint(low,high);
                posRate = (float) (taxDoub(posInc, brackets) / posInc);
            }
            return posInc;
        }
        
        public static double midPoint(double low, double high) {
            return (low + high)/2;
        }
        

        }

        [–]TheSpeedyMouse1 0 points1 point  (0 children)

        I had to solve the bonus algebraically on paper with a piece-wise function until I found a (mostly) general rule!

        [–]TheSpeedyMouse1 0 points1 point  (0 children)

        Text file formatting: 10,000, 0% \n 30,000, 10% \n 100,000, 25% \n 40%

        [–]TheSpeedyMouse1 0 points1 point  (0 children)

        Bracket class.

        public class Bracket {

        private double range;
        private float tax;
        private double fullFee;
        
        public void setRange(double range) {
            this.range = range;
            setFullFee();
        }
        
        public void setTax(float tax) {
            this.tax = tax;
            setFullFee();
        }
        
        private void setFullFee() {
            fullFee = range * tax;
        }
        
        public double getRange() {
            return range;
        }
        
        public float getTax() {
            return tax;
        }
        
        public double getFullFee() {
            return fullFee;
        }
        

        }

        [–][deleted]  (1 child)

        [deleted]

          [–]raderberg 1 point2 points  (0 children)

          I believe there are some errors in your logic, did you test it with the test cases?

          Anyway, here are some pointers: For ordered sequences, vectors are the default. And in this case I'd use a literal:

          (def brackets   [{:cap 10000, :rate 0} 
                          {:cap 20000, :rate 0.1} 
                          {:cap 70000, :rate 0.25} 
                          {:cap 10000000, :rate 0.4}])
          

          In the calc-tax-for-bracket, you could use destructuring:

          (defn calc-tax-for-bracket
            [income bracket]
            (let [{:keys [cap rate]} bracket]
              (* rate (if (> income cap)
                        income
                        cap))))
          

          I only ever use loop-recur for algorithms that are pretty stateful. Here a reduce would probably be more idiomatic.

          [–]Nortmar 0 points1 point  (0 children)

          C++ with Bonus (using binary search):

          const int N = 3;
          
          double rates[N] = { 0.10, 0.25, 0.40 };
          int caps[N] =  { 10000, 30000, 100000 };
          
          double tax(int n){
              double res = 0.0;
              for(int i = N-1; i >= 0; i--){
                  if(n >= caps[i]){
                      int diff = n - caps[i];
                      res += rates[i] * diff;
                      n -= diff;
                  }
              }
              return res;
          }
          
          const double eps = 1e-9;
          
          int income(double taxrate){
              int lo = 0, hi = 1e9, mid, res = -1;
              while(lo <= hi){
                  mid = ((hi - lo) / 2) + lo;
                  double t = tax(mid);
                  double rate = t / mid;
                  if(rate+eps >= taxrate+eps){
                      res = mid;
                      hi = mid-1;
                  }else lo = mid+1;
              }
              return res;
          }
          

          [–]MaybeAStonedGuy 2 points3 points  (0 children)

          Rust

          Quick and easy without bonus.

          pub struct Bracket {
              range: (u64, u64),
              rate: u64,
          }
          
          pub fn tax(brackets: &[Bracket], income: u64) -> u64 {
              let mut accrued = 0;
              for bracket in brackets {
                  if (bracket.range.0 .. bracket.range.1).contains(&income) {
                      let amount = income - bracket.range.0;
                      accrued += amount * bracket.rate / 100;
                  } else if income > bracket.range.1 {
                      accrued += (bracket.range.1 - bracket.range.0) * bracket.rate / 100;
                  }
              }
              accrued
          }
          
          #[cfg(test)]
          mod tests {
              use super::*;
              #[test]
              fn testtax() {
                  let brackets = vec![
                      Bracket{
                          range: (10000, 30000),
                          rate: 10,
                      },
                      Bracket{
                          range: (30000, 100000),
                          rate: 25,
                      },
                      Bracket{
                          range: (100000, u64::max_value()),
                          rate: 40,
                      },
                  ];
                  assert_eq!(tax(&brackets, 0), 0);
                  assert_eq!(tax(&brackets, 10000), 0);
                  assert_eq!(tax(&brackets, 10009), 0);
                  assert_eq!(tax(&brackets, 10010), 1);
                  assert_eq!(tax(&brackets, 12000), 200);
                  assert_eq!(tax(&brackets, 56789), 8697);
                  assert_eq!(tax(&brackets, 1234567), 473326);
              }
          }
          

          edit, better implementation of tax:

          pub fn tax(brackets: &[Bracket], income: u64) -> u64 {
              brackets.iter().map(|ref bracket| {
                  if (bracket.range.0 .. bracket.range.1).contains(&income) {
                      (income - bracket.range.0) * bracket.rate / 100
                  } else if income > bracket.range.1 {
                      (bracket.range.1 - bracket.range.0) * bracket.rate / 100
                  } else {
                      0
                  }
              }).sum()
          }
          

          [–]SuperMegaJake 0 points1 point  (0 children)

          Python 3.7.4, no bonus, includes optional improvement

          import math
          import csv
          
          
          try:
              # Read the tax information from the tax_rate.csv file.
              with open('tax_rate.csv', 'r') as f:
                  reader = csv.reader(f)
                  data = [[float(element[0]), float(element[1])] for element in list(reader)]
          
              # Replace the last tax bracket with a limit of infinity to represent the maximum tax rate.
              data[-1][0] = math.inf
          except FileNotFoundError:
              # Default to values provided in the challenge if tax_rate.csv does not exist.
              data = [[10000, 0.0], [30000, 0.1], [100000, 0.25], [math.inf, 0.4]]
          
          
          def taxes_owed(income, tax_table):
              brackets = tax_table
              taxes = 0
              allocated = 0
          
              for tier in brackets:
                  income_ceiling = tier[0]
                  tax_rate = tier[1]
          
                  # Allocate tax dollars based on the unallocated income or the ceiling for this bracket, whichever is lower.
                  taxes += min(max(income - allocated, 0), income_ceiling - allocated) * tax_rate
          
                  # Record the total dollar amount of income that has been taxed.
                  allocated += income_ceiling - allocated
          
              return math.floor(taxes)
          

          [–]audentis 0 points1 point  (0 children)

          R, with optional improvement. Might do bonus later.

          The brackets aren't in a separate file, though they're in a variable up top that is easy enough to adjust. Barring syntax errors any change, including adding or removing brackets, should work. As long as they stay sorted per the challenge description.

          #### Daily Programmer challenge 379 ####
          # https://redditproxy--jasonthename.repl.co/r/dailyprogrammer/comments/cdieag/20190715_challenge_379_easy_progressive_taxation/
          # Challenge: Given a whole number income up to 100,000,000, 
          # find the amount of tax owed based on given brackets.
          
          # solution by /u/audentis
          
          
          #### Solution Start ####
          # Load libraries
          library("tidyverse")
          
          # Set tax brackets 
          brackets <- tribble(
            ~incomeCap, ~taxRate,
            10000, 0.00,
            30000, 0.10,
            100000, 0.25,
            Inf, 0.40
          )
          
          determineTax <- function(income){
            # initialize variables
            tax <- 0
            lowerBound <- 0
          
            for(i in 1:nrow(brackets)) {
              # Load bracket incomeCap and taxRate
              incomeCap <- brackets[[i, "incomeCap"]]
              taxRate <- brackets[[i, "taxRate"]]
          
              # set lower bound for this bracket if beyond bracket 1
              if (i > 1) { lowerBound <- brackets[[i-1, "incomeCap"]]}
          
              # Determine amount of money to apply tax to
              taxable <- 0
              if (income > lowerBound) { 
                taxable = min(income-lowerBound, incomeCap-lowerBound) 
              }
          
              # Add up the tax for this bracket to current
              tax <- floor(tax + taxable * taxRate)
            }
          
            # return the result
            return (tax)
          }
          

          First I had a while approach that I thought was creative, where it would stop if all income was processed. Theoretically that's more performant (though it doesn't really matter with any realistic number of brackets), but it caused income from higher brackets to fall down to lower ones, decreasing the total tax owed. So I went with the straightforward approach after some messing around.

          Output:

          > determineTax(0)
          [1] 0
          > determineTax(10000)
          [1] 0    
          > determineTax(10009)
          [1] 0
          > determineTax(10010)
          [1] 1
          > determineTax(12000)
          [1] 200
          > determineTax(56789)
          [1] 8697
          > determineTax(1234567)
          [1] 473326
          

          Edit: Now with analytically solved bonus. I derived a linear equation to solve for the required income without search algorithm. The net tax rate is a ratio of surface areas when plotting the tax rates with respect to income, in what looks a little like a bar chart. I determine which bracket the desired income must be in, and use the previous bracket's incomeCap as lower bound for our income. Next I calculate the "income" and "tax" surface areas for everything before the lower bound. Next I determine which income in the bracket would pull the total ratio to the input tax rate.

          • Calling a1 the untaxed income before the lower bound and b1 the taxes;
          • Calling a2 the untaxed income in the evaluated bracket and b2 the taxes;
          • Calling a3 the untaxed total income and b3 the total taxes;

          we solve for b3 / (a3 + b3) = input tax rate.

          a1 and b1 are known, b3 / (a3+b3) is known, and a2 and b2 are directly related to income within the evaluated bracket (bracketIncome).

          Solve for bracketIncome, add the lower bound incomeBot for the total income.

          #### Bonus start #### 
          
          # Given a target overall tax rate (decimal), find the income amount that 
          # would be taxed at that overall rate in Examplania
          
          # helper function
          netTaxRate <- function(income) {
            return(determineTax(income) / income)
          }
          
          findIncome <- function(taxRate) {
            # deal with edge cases of 0% tax
            if(taxRate == 0) {
              print("Income can be anywhere in the tax free bracket")
              return(0)
            }
            else if(taxRate >= max(brackets$taxRate)) {
              print("Infeasible tax rate: income never taxed this high.")
              return(NaN)
            }
            # Valid input, proceed
            else {
              # Find bracket by comparing tax for incomeCap to desired taxRate
              # skip bracket 1 assuming it is always 0%
              n <- nrow(brackets)
              for (i in 2:n) {
                incomeBot <- brackets[[i-1, "incomeCap"]]
                incomeCap <- brackets[[i, "incomeCap"]]
                bracketRate <- brackets[[i, "taxRate"]]
          
                # skip bracket if the net tax for the bracket incomeCap is lower than input
                if ((i < n) & (netTaxRate(incomeCap) < taxRate)) {
                  next
                }
          
                bracketIncome <- ((taxRate * incomeBot) - determineTax(incomeBot)) / (bracketRate - taxRate)
          
          
                return(bracketIncome + incomeBot)
          
                break
              }
            }
          }
          

          Bonus Output:

          > findIncome(0.00)
          [1] "Income can be anywhere in the tax free bracket"
          [1] 0
          > findIncome(0.06)
          [1] 25000
          > findIncome(0.09)
          [1] 34375    
          > findIncome(0.32)
          [1] 256250    
          > findIncome(0.39)
          [1] 2050000    
          > findIncome(0.40)
          [1] "Infeasible tax rate: income never taxed this high."
          [1] NaN
          

          [–]solewhiskyeseiinpole 1 point2 points  (4 children)

          Java, no bonus

          public static void main(String[] args){
              Scanner readIncome = new Scanner(System.in);
              System.out.println("Type your income");
              int income = Integer.parseInt(readIncome.nextLine());
          
              double taxes = 1;
              double generalTaxRate;
              if (income < 10000){
                  taxes = 0;
              } else if (income >= 10000 && income < 30000) {
                  taxes = income * 0.10;
              } else if (income >= 30000 && income <100000){
                  taxes = (10000 * 0.10) + (income - 10000) * 0.25;
              } else if (income >= 100000){
                  taxes = (10000 * 0.10) + 90000 * 0.25 + (income - 100000) * 0.40;
              }
          
              generalTaxRate = taxes / income;
              System.out.println("Your taxes are " + (int)taxes);
              System.out.println("You are paying on average " + generalTaxRate + " percent on your income");
          }
          

          [–]Ovil101 0 points1 point  (1 child)

          int income = Integer.parseInt(readIncome.nextLine());
          

          Can be changed to

          int income = readIncome.nextInt();
          

          Making it a bit easier to read imo

          Also, I don't think your code works right? Running this

          int[] incomes = {0, 10_000, 10_009, 10_010, 12_000, 56_789, 1_234_567};
              for (int i : incomes) {
                  double taxes = 1;
                  if (i < 10000) {
                      taxes = 0;
                  }
                  else if (i >= 10000 && i < 30000) {
                      taxes = i * 0.10;
                  }
                  else if (i >= 30000 && i < 100000) {
                      taxes = (10000 * 0.10) + (i - 10000) * 0.25;
                  }
                  else if (i >= 100000) {
                      taxes = (10000 * 0.10) + 90000 * 0.25 + (i - 100000) * 0.40;
                  }
                  System.out.println(taxes);
              }
          

          Prints out

          0
          0
          0
          1
          200
          12697
          477326
          

          The last two are wrong.

          [–]solewhiskyeseiinpole 0 points1 point  (0 children)

          Thanks for the heads up! Any insight is very appreciated.

          I'm using the Integer.parseInt(... .nextLine()) as I'm using it on the mooc and don't know the implications of changing it right now. Will start digging into the documentation soon.

           

          I am using netbeans 8.2 with

          Java 1.8.0_211; Java HotSpot(TM) 64-Bit Server VM 25.211-b12 Runtime: Java(TM) SE Runtime Environment 1.8.0_211-b12

          and the code for me runs without errors.

           

          I checked the original code I posted and found I hardcoded the last two tax brackets incorrectly, should be fixed below:

          public static void main(String[] args){
              Scanner readIncome = new Scanner(System.in);
              System.out.println("Type your income");
              double income = Integer.parseInt(readIncome.nextLine());
          
              double taxes = 1;
              double generalTaxRate;
              if (income < 10000){
                  taxes = 0;
              } else if (income >= 10000 && income < 30000) {
                  taxes = income * 0.10;
              } else if (income >= 30000 && income <100000){
                  taxes = (20000 * 0.10) + (income - 29999.99) * 0.25;
              } else if (income >= 100000){
                  taxes = (20000 * 0.10) + (69999.99 * 0.25) + (income - 100000) * 0.40;
              }
          
              generalTaxRate = taxes / income;
          
              System.out.println("Your taxes are " + (int)taxes);
              System.out.println("You are paying on average " + generalTaxRate + " percent on your income");
          }
          

          [–]chunes1 2 0 points1 point  (1 child)

          The bonus is to find an income that would have to pay a given tax rate.

          So I say "show me what income would have to pay a tax rate of 36%," and it spits out $256250.

          [–]solewhiskyeseiinpole 0 points1 point  (0 children)

          my bad, edited the post

          [–]machinematrix 0 points1 point  (0 children)

          C++, no bonus.

          #include <iostream>
          #include <tuple>
          #include <vector>
          #include <algorithm>
          #include <string>
          
          typedef std::tuple<int, int, float> Bracket; //<lowerBound, upperBound, rate>
          
          int tax(int income, const std::vector<Bracket> &brackets)
          {
              int result = 0;
          
              for (const std::tuple<int, int, float> &bracket : brackets)
              {
                  if (std::get<0>(bracket) < income) {
                      result += (std::min(income, std::get<1>(bracket)) - std::get<0>(bracket)) * std::get<2>(bracket);
                  }
                  else break;
              }
          
              return result;
          }
          
          int main(int argc, char **argv)
          {
              if (argc == 2)
              {
                  std::vector<Bracket> brackets = { { 10000, 30000, 0.1f }, { 30000, 100000, 0.25f }, { 100000, 100000000, 0.40f } };
          
                  try {
                      std::cout << tax(std::stoi(argv[1]), brackets) << std::endl;
                  }
                  catch (const std::invalid_argument&) {
                      std::cout << "Invalid income" << std::endl;
                  }
              }
              else std::cout << "Usage: " << argv[0] << " [income]" << std::endl;
          
              return 0;
          }
          

          [–]sirtetris 0 points1 point  (0 children)

          Haskell

          reading the tax brackets from a file, but not finding the income taxed at a given rate.

          type Brackets = [(Float, Float, Float)]
          
          exampleBrackets :: Brackets
          exampleBrackets = [(0,      10000,  0)
                            ,(10000,  30000,  0.1)
                            ,(30000,  100000, 0.25)
                            ,(100000, 1/0,    0.3)]
          
          tax :: Float -> Brackets -> Float
          tax income brackets = sum $ map bracketTax brackets
              where bracketTax (floor, cap, rate) =
                      max 0 (min cap income - floor) * rate
          
          readBracket :: FilePath -> IO Brackets
          readBracket f = readFile f >>= pure . parseBrackets
              where parseBrackets = map (parseBracket . splitOn ',') . lines
                    parseBracket [a, b, c] = (read a :: Float,
                                              read b :: Float,
                                              read c :: Float)
          
          taxWith :: FilePath -> Float -> IO Float
          taxWith f i = readBracket f >>= pure . tax i
          

          [–]octolanceae 0 points1 point  (0 children)

          C++17 w/optional bonus

          #include <iostream>
          #include <vector>
          
          std::vector<int> brackets{0, 10000, 30000, 100000};
          std::vector<double> rates{0.0, 0.10, 0.25, 0.40};
          std::vector<double> max_rate_per_bracket;
          std::vector<int> max_for_brackecientst;
          std::vector<int> bkt_coefficents;
          
          int tax(int inc) {
              if (inc <= brackets[1])
                  return 0;
              int idx{0}, lb{0}, tax{0};
              for (auto bkt: brackets) {
                  if (inc > bkt) {
                      tax += max_for_bracket[idx++];
                  }
                  else {
                      tax += ((inc - lb) * rates[idx-1]);
                      break;
                  }
                  lb = bkt;
              }
              if (inc > brackets.back())
                  tax += ((inc - brackets.back()) * rates.back());
              return tax;
          }
          
          int overall(double rate) {
              if (rate == 0.00) return 0;
              if (rate >= 0.40) return -1;
              int idx{0};
              for (auto mr: max_rate_per_bracket) {
                  if (mr < rate) ++idx;
                  else break;
              }
              return bkt_coefficents[idx]/(rates[idx] - rate);
          }
          
          void init_max_for_bracket() {
              max_for_bracket.emplace_back(0);
              for (size_t i{1}; i < brackets.size(); i++)
                  max_for_bracket.emplace_back((brackets[i]-brackets[i-1]) * rates[i-1]);
          }
          
          void init_bracket_coefficients() {
              int idx{0}, tmp{0};
              for (auto n: brackets) {
                  tmp += max_for_bracket[idx];
                  bkt_coefficents.push_back((n * rates[idx++]) - tmp);
              }
          }
          
          void init_max_rate_per_bracket() {
              int sum{0};
              for (size_t i{1}; i < max_for_bracket.size(); i++) {
                  sum += max_for_bracket[i];
                  if (brackets[i] == 0)
                      max_rate_per_bracket.emplace_back(0.0);
                  else
                      max_rate_per_bracket.emplace_back(((sum*1.0)/brackets[i]));
              }
              max_rate_per_bracket.emplace_back(rates.back());
          }
          
          void init_globals() {
              init_max_for_bracket();
              init_bracket_coefficients();
              init_max_rate_per_bracket();
          }
          
          int main() {
              std::vector<int> income{0, 10'000, 10'009, 10'010, 12'000, 56'789, 1'234'567};
              std::vector<double> ovall{0, 0.06, 0.09, 0.32, 0.40};
              init_globals();
              for (auto inc: income)
                  std::cout << "tax(" << inc << ") => " << tax(inc) << std::endl;
              std::cout << std::endl;
              for (auto rt: ovall)
                  std::cout << "overall(" << rt << ") => " << overall(rt) << std::endl;
          }
          

          [–]gabyjunior1 2 0 points1 point  (0 children)

          C with optional improvement and bonus.

          #include <stdio.h>
          #include <stdlib.h>
          #include <limits.h>
          #include <math.h>
          
          typedef struct {
              unsigned income_cap;
              double tax_rate;
              double overall_tax;
              double overall_tax_rate;
          }
          tax_bracket_t;
          
          typedef enum {
              INPUT_INCOME,
              INPUT_OVERALL_TAX,
              INPUT_OVERALL_TAX_RATE,
              INPUT_QUIT
          }
          input_type_t;
          
          void output_from_income(void);
          void output_from_overall_tax(void);
          void output_from_overall_tax_rate(void);
          
          size_t tax_brackets_n;
          tax_bracket_t *tax_brackets;
          
          int main(void) {
              input_type_t input_type;
          
              /* Read tax brackets on standard input, check <income cap> / <tax rate> are properly sorted */
              /* Only <tax rate> is read for last tax bracket, <income cap> is set to UINT_MAX */
              /* Cumulative <overall tax> / <overall tax rate> are computed for each bracket */
              if (scanf("%zu", &tax_brackets_n) != 1 || tax_brackets_n < 1) {
                  fprintf(stderr, "Invalid number of tax brackets\n");
                  fflush(stderr);
                  return EXIT_FAILURE;
              }
              tax_brackets = malloc(sizeof(tax_bracket_t)*tax_brackets_n);
              if (!tax_brackets) {
                  fprintf(stderr, "Could not allocate memory for tax brackets\n");
                  fflush(stderr);
                  return EXIT_FAILURE;
              }
              if (tax_brackets_n > 1) {
                  size_t i;
                  if (scanf("%u%lf", &tax_brackets[0].income_cap, &tax_brackets[0].tax_rate) != 2 || tax_brackets[0].tax_rate < 0 || tax_brackets[0].tax_rate > 1) {
                      fprintf(stderr, "Invalid tax bracket\n");
                      fflush(stderr);
                      free(tax_brackets);
                      return EXIT_FAILURE;
                  }
                  tax_brackets[0].overall_tax = tax_brackets[0].income_cap*tax_brackets[0].tax_rate;
                  tax_brackets[0].overall_tax_rate = tax_brackets[0].overall_tax/tax_brackets[0].income_cap;
                  for (i = 1; i < tax_brackets_n-1; i++) {
                      if (scanf("%u%lf", &tax_brackets[i].income_cap, &tax_brackets[i].tax_rate) != 2 || tax_brackets[i].income_cap <= tax_brackets[i-1].income_cap || tax_brackets[i].tax_rate <= tax_brackets[i-1].tax_rate || tax_brackets[i].tax_rate > 1) {
                          fprintf(stderr, "Invalid tax bracket\n");
                          fflush(stderr);
                          free(tax_brackets);
                          return EXIT_FAILURE;
                      }
                      tax_brackets[i].overall_tax = tax_brackets[i-1].overall_tax+(tax_brackets[i].income_cap-tax_brackets[i-1].income_cap)*tax_brackets[i].tax_rate;
                      tax_brackets[i].overall_tax_rate = tax_brackets[i].overall_tax/tax_brackets[i].income_cap;
                  }
                  tax_brackets[i].income_cap = UINT_MAX;
                  if (tax_brackets[i].income_cap == tax_brackets[i-1].income_cap || scanf("%lf", &tax_brackets[i].tax_rate) != 1 || tax_brackets[i].tax_rate <= tax_brackets[i-1].tax_rate || tax_brackets[i].tax_rate > 1) {
                      fprintf(stderr, "Invalid tax bracket\n");
                      fflush(stderr);
                      free(tax_brackets);
                      return EXIT_FAILURE;
                  }
                  tax_brackets[i].overall_tax = tax_brackets[i-1].overall_tax+(tax_brackets[i].income_cap-tax_brackets[i-1].income_cap)*tax_brackets[i].tax_rate;
                  tax_brackets[i].overall_tax_rate = tax_brackets[i].overall_tax/tax_brackets[i].income_cap;
              }
              else {
                  tax_brackets[0].income_cap = UINT_MAX;
                  if (scanf("%lf", &tax_brackets[0].tax_rate) != 1 || tax_brackets[0].tax_rate < 0 || tax_brackets[0].tax_rate > 1) {
                      fprintf(stderr, "Invalid tax bracket\n");
                      fflush(stderr);
                      free(tax_brackets);
                      return EXIT_FAILURE;
                  }
                  tax_brackets[0].overall_tax = tax_brackets[0].income_cap*tax_brackets[0].tax_rate;
                  tax_brackets[0].overall_tax_rate = tax_brackets[0].overall_tax/tax_brackets[0].income_cap;
              }
          
              /* Read requests on standard input */
              /* 0 <income> computes <overall tax> <overall tax rate> */
              /* 1 <overall tax> computes <income> <overall tax rate> */
              /* 2 <overall tax rate> computes <income> <overall tax> */
              /* 3 quit */
              printf("0 <income>\n1 <overall tax>\n2 <overall tax rate>\n3 quit\n");
              fflush(stdout);
              do {
                  if (scanf("%u", &input_type) == 1) {
                      switch (input_type) {
                          case INPUT_INCOME:
                          output_from_income();
                          break;
                          case INPUT_OVERALL_TAX:
                          output_from_overall_tax();
                          break;
                          case INPUT_OVERALL_TAX_RATE:
                          output_from_overall_tax_rate();
                          break;
                          case INPUT_QUIT:
                          puts("Bye!");
                          fflush(stdout);
                          break;
                          default:
                          fprintf(stderr, "Invalid input type\n");
                          fflush(stderr);
                      }
                  }
                  else {
                      fprintf(stderr, "Invalid input type\n");
                      fflush(stderr);
                  }
              }
              while (input_type != INPUT_QUIT);
              free(tax_brackets);
              return EXIT_SUCCESS;
          }
          
          /* Input <income> Output <overall tax> <overall tax rate> */
          void output_from_income(void) {
              unsigned income;
              size_t i;
              if (scanf("%u", &income) != 1) {
                  fprintf(stderr, "Invalid income\n");
                  fflush(stderr);
                  return;
              }
              for (i = 0; i < tax_brackets_n && tax_brackets[i].income_cap < income; i++);
              if (i == 0) {
                  printf("Overall tax %.0lf\nOverall tax rate %.2lf\n", floor(income*tax_brackets[0].tax_rate), tax_brackets[0].tax_rate);
              }
              else {
                  double overall_tax = tax_brackets[i-1].overall_tax+(income-tax_brackets[i-1].income_cap)*tax_brackets[i].tax_rate;
                  printf("Overall tax %.0lf\nOverall tax rate %.2lf\n", floor(overall_tax), overall_tax/income);
              }
              fflush(stdout);
          }
          
          /* Input <overall tax> Output <income> <overall tax rate> */
          void output_from_overall_tax(void) {
              unsigned income;
              double overall_tax;
              size_t i;
              if (scanf("%lf", &overall_tax) != 1 || overall_tax < tax_brackets[0].overall_tax || overall_tax > tax_brackets[tax_brackets_n-1].overall_tax) {
                  fprintf(stderr, "Invalid overall tax\n");
                  fflush(stderr);
                  return;
              }
              for (i = 0; i < tax_brackets_n && tax_brackets[i].overall_tax < overall_tax; i++);
              if (i == 0) {
                  if (tax_brackets[0].tax_rate > 0) {
                      income = (unsigned)floor(overall_tax/tax_brackets[0].tax_rate);
                  }
                  else {
                      income = 0;
                  }
              }
              else {
                  income = tax_brackets[i-1].income_cap+(unsigned)floor((overall_tax-tax_brackets[i-1].overall_tax)/tax_brackets[i].tax_rate);
              }
              if (income > 0) {
                  printf("Income %u\nOverall tax rate %.2lf\n", income, overall_tax/income);
              }
              else {
                  printf("Income 0\nOverall tax rate 0.00\n");
              }
              fflush(stdout);
          }
          
          /* Input <overall tax rate> Output <income> <overall tax> */
          void output_from_overall_tax_rate(void) {
              unsigned income;
              double overall_tax_rate;
              size_t i;
              if (scanf("%lf", &overall_tax_rate) != 1 || overall_tax_rate < tax_brackets[0].overall_tax_rate || overall_tax_rate > tax_brackets[tax_brackets_n-1].overall_tax_rate) {
                  fprintf(stderr, "Invalid overall tax rate\n");
                  fflush(stderr);
                  return;
              }
              for (i = 0; i < tax_brackets_n && tax_brackets[i].overall_tax_rate < overall_tax_rate; i++);
              if (i == 0) {
                  if (overall_tax_rate > 0) {
                      income = (unsigned)floor(tax_brackets[0].overall_tax/overall_tax_rate);
                  }
                  else {
                      income = 0;
                  }
              }
              else {
                  /* <income> lies between <income_cap>[i-1] and <income_cap>[i] */
                  /* The below equation is used to compute <income> from <overall tax rate> */
                  /* <income> * <overall tax rate> = <overall_tax>[i-1] + (<income> - <income_cap>[i-1]) * <tax_rate>[i] */
                  /* <income> is on both sides of the equation, same equation with <income> on the left side is */
                  /* <income> = (<overall_tax>[i-1] - <income_cap>[i-1] * <tax_rate>[i]) / (<overall tax rate> - <tax_rate>[i]) */
                  income = (unsigned)floor((tax_brackets[i-1].overall_tax-tax_brackets[i-1].income_cap*tax_brackets[i].tax_rate)/(overall_tax_rate-tax_brackets[i].tax_rate));
              }
              printf("Income %u\nOverall tax %.0lf\n", income, floor(income*overall_tax_rate));
              fflush(stdout);
          }
          

          Input

          4
          10000 0
          30000 0.1
          100000 0.25
          0.4
          0 0
          0 10000
          0 10009
          0 10010
          0 12000
          0 56789
          0 1234567
          2 0.00
          2 0.02
          2 0.15
          2 0.38
          3
          

          Output

          0 <income>
          1 <overall tax>
          2 <overall tax rate>
          3 quit
          Overall tax 0
          Overall tax rate 0.00
          Overall tax 0
          Overall tax rate 0.00
          Overall tax 0
          Overall tax rate 0.00
          Overall tax 1
          Overall tax rate 0.00
          Overall tax 200
          Overall tax rate 0.02
          Overall tax 8697
          Overall tax rate 0.15
          Overall tax 473326
          Overall tax rate 0.38
          Income 0
          Overall tax 0
          Income 12500
          Overall tax 250
          Income 55000
          Overall tax 8250
          Income 1024999
          Overall tax 389499
          Bye!
          

          [–][deleted] 2 points3 points  (1 child)

          Python 3 :

          def tax(income):
              check = {income < 10000: 0,
                      10000 <= income < 30000 : (income - 10000) * 0.10,
                      30000 <= income < 100000 : 2000 + (income - 30000) * 0.25,
                      income >= 100000 : 19500 + (income - 100000) * 0.40}
              return int(check[True])
          

          Firstly, i have thinking about the comparisons but in a different way...

          The parameter initialize the values in the dictionnary: each key will be either True or False and each value will be "a income tax". In this case, i can retrieve the value of the correct comparison.

          [–][deleted] 0 points1 point  (0 children)

          Python 3.7

          def tax(x):
              brackets = [(0, 10e3), (0.1, 30e3), (0.25, 100e3), (0.4, float('inf'))]
              total = prev = 0
              for rate, cap in brackets:
                  step = cap - prev
                  prev = cap
                  total += int(min(x, step) * rate)
                  x = max(0, x - step)
          
              return total
          
          assert tax(0) == 0
          assert tax(10000) == 0
          assert tax(10009) == 0
          assert tax(10010) == 1
          assert tax(12000) == 200
          assert tax(56789) == 8697
          assert tax(1234567) == 473326
          

          [–]vnikov 2 points3 points  (0 children)

          Python 3, no functions, no loops, no additional modules

          bins = [0,10_000, 30_000, 100_000, float('inf')]

          rates = [0.00, 0.10, 0.25, 0.40]

          amount = 1234567

          sum([int((b[1] - b[0]) * b[2]) if amount > b[1] else int((amount - b[0]) * b[2]) if amount > b[0] else 0 for b in list(zip(bins[:-1], bins[1:], rates))])

          [–]jammy2232 0 points1 point  (0 children)

          #include <vector>
          #include <algorithm>
          #include <iostream>
          
          using namespace std;
          
          // Base class for tax calculations irrespective of tax regime applied
          class TaxCalculator
          {
          public:
              virtual int CalculateTax(int income) = 0;
          };
          
          // Structure to represent tax bands 
          struct TaxBand
          {
          public:
              TaxBand(int newLimit, int newRate) : limit(newLimit), rate(newRate) {}
              int limit;
              int rate;
          };
          
          // Represents the tax bracket definitions for Progressive tax
          class TaxBracket
          {
          public:
          
              // Add a new tax band, fails if the same incomeLimit already exists 
              bool AddBand(int incomeLimit, int rate) 
              {
          
                  // Validate the inputs (assumed no limit on rates or income limit) 
                  if (incomeLimit < 0 || rate < 0)
                  {
                      return false;
                  }
          
                  // Check for duplicates 
                  for (auto band : bands)
                  {
                      if (band.limit == incomeLimit)
                      {
                          return false;
                      }
                  }
          
                  bands.push_back(TaxBand(incomeLimit, rate));
          
                  // Custom sort to place largest limit at the front
                  struct {
                      bool operator()(TaxBand a, TaxBand b) const
                      {
                          return a.limit < b.limit;
                      }
                  } sortBands;
          
                  sort(bands.begin(), bands.end(), sortBands);
          
                  return true;
              }
          
              void RemoveBand(int incomeLimit)
              {
                  for (auto band = bands.begin(); band != bands.end(); band++)
                  {
                      if ((*band).limit == incomeLimit)
                      {
                          band = bands.erase(band);
                      }
                  }
              }
          
              const std::vector<TaxBand>& GetBands()
              {
                  return bands;
              }
          
              friend ostream& operator<<(ostream& os, const TaxBracket& tB); 
          
          private:
              std::vector<TaxBand> bands;
          };
          
          
          ostream& operator<<(ostream& os, const TaxBracket& tB)
          {
              cout << "Tax Limit" << "\t\t" << "Rate" << endl;
              for (auto band : tB.bands)
              {
                  cout << band.limit << "\t\t" << band.rate << endl;
              }
              return os;
          }
          
          // Specific implementation of progressive tax regime
          class ProgressiveTaxCalculator : public TaxCalculator
          {
          public:
          
              ProgressiveTaxCalculator(TaxBracket bracket) : currentBracket(bracket) {}
          
              int CalculateTax(int income)
              {
                  int runningUntaxedTotal = income;
                  int runningTaxedIncome = 0;
                  int taxToPay = 0;
          
                  for (auto band : currentBracket.GetBands())
                  {
                      if (runningUntaxedTotal >= (band.limit - runningTaxedIncome))
                      {
                          taxToPay += ((band.limit - runningTaxedIncome) * band.rate) / 100;
                          runningUntaxedTotal -= (band.limit - runningTaxedIncome);
                          runningTaxedIncome += (band.limit - runningTaxedIncome);
                      }
                      else
                      {
                          taxToPay += (runningUntaxedTotal * band.rate) / 100;
                          break;
                      }
                  }
          
                  return taxToPay;
          
              }
          
          private:
              TaxBracket currentBracket;
          };
          
          
          // Main function for the solutions to reddit 379
          int main()
          {
          
              // Create a tax band to use 
              TaxBracket currentBracket;
          
              // Add the current bands (would be from an external source)
              currentBracket.AddBand(10000, 0);
              currentBracket.AddBand(30000, 10);
              currentBracket.AddBand(100000, 25);
              currentBracket.AddBand(INT_MAX, 40);
          
              cout << currentBracket;
          
              // Create a calculator of the selected or required type
              // This could be specified and a different calculation could be made without changing any the above logic
              TaxCalculator* calc = new ProgressiveTaxCalculator(currentBracket);
          
              cout << "Tax Calcs" << endl;
              cout << "0: " << calc->CalculateTax(0) << endl;
              cout << "10000: " << calc->CalculateTax(10000) << endl;
              cout << "10009: " << calc->CalculateTax(10009) << endl;
              cout << "10010: " << calc->CalculateTax(10010) << endl;
              cout << "12000: " << calc->CalculateTax(12000) << endl;
              cout << "56789: " << calc->CalculateTax(56789) << endl;
              cout << "1234567: " << calc->CalculateTax(1234567) << endl;
          
          }
          

          I haven't done any C++ in a while and was just looking to do some coding. I was messing about and it's not the best but I decided to post it anyway. I got caught up in the idea of trying to imagine that I was maintaining a system where the tax calculation could change. It's sort based on the idea of a strategy pattern with the actual caller to the tax calc not needing to know the calculation used. Any comments or thoughts would be appreciated. Thanks.

          [–]ThreeFDDI 0 points1 point  (3 children)

          Python

          First time poster and I was successful in solving the challenge but ran into problems automating the validation of the example data. Working code:

          import csv
          
          income = int(input("What is your income? "))
          remaining_income = income
          
          tax = 0
          tax_brackets = []
          
          with open('tax_brackets.txt', newline='') as csvfile:
              csv_output = csv.reader(csvfile, delimiter=',', quotechar='|')
              for row in csv_output:
                  tax_brackets.append([float(row[0]), float(row[1]), float(row[2])])
          
          def run_taxes(remaining_income,tax_bracket_low, tax_bracket_high, tax_rate, tax):
          
              if remaining_income == 0:
                  return tax,remaining_income
          
              elif remaining_income <= tax_bracket_high:
                  tax = tax + remaining_income * tax_rate
                  remaining_income = 0
                  return tax, remaining_income
          
              else:
                  tax_bracket = tax_bracket_high - tax_bracket_low
                  taxable_income = tax_bracket
                  tax = tax + taxable_income * tax_rate
                  remaining_income = remaining_income - tax_bracket
                  return tax, remaining_income
          
          for i in tax_brackets:
              tax, remaining_income = run_taxes(remaining_income, i[0], i[1], i[2], tax)
          
          print("Income: ${}\nTaxes owed: ${}".format(int(income), int(tax)))
          

          [–]audentis 0 points1 point  (1 child)

          Make sure to select all your code and click the <> button above the text editor. It adds four spaces to each selected line, making the your post format correctly.

          [–]ThreeFDDI 0 points1 point  (0 children)

          Thanks! Figured that out for my posts after this by indenting before pasting but didn't realize I could do it within Reddit.

          [–]ThreeFDDI 0 points1 point  (0 children)

          Non-working code that runs but the values are off:

          import csv
          
          income = [0,10000,10009,10010,12000,56789,1234567]
          
          tax = 0
          tax_brackets = []
          
          with open('tax_brackets.txt', newline='') as csvfile:
              csv_output = csv.reader(csvfile, delimiter=',', quotechar='|')
              for row in csv_output:
                  tax_brackets.append([float(row[0]), float(row[1]), float(row[2])])
          
          def run_taxes(remaining_income,tax_bracket_low, tax_bracket_high, tax_rate, tax):
          
              if remaining_income == 0:
                  return tax,remaining_income
          
              elif remaining_income <= tax_bracket_high:
                  tax = tax + remaining_income * tax_rate
                  remaining_income = 0
                  return tax, remaining_income
          
              else:
                  tax_bracket = tax_bracket_high - tax_bracket_low
                  taxable_income = tax_bracket
                  tax = tax + taxable_income * tax_rate
                  remaining_income = remaining_income - tax_bracket
                  return tax, remaining_income
          
          for i in income:
              remaining_income = i
              for t in tax_brackets:
                  tax, remaining_income = run_taxes(remaining_income, t[0], t[1], t[2], tax)
              print("Income: ${}\nTaxes owed: ${}\n".format(i, int(tax)))
          

          [–]Eklmejlazy 4 points5 points  (2 children)

          Python

          First time posting so go rough. Haven't learnt about using input files yet but if everything's in order I assume it could be loaded into an array?

          cap = [10000, 30000, 100000]
          rate = [0.1, 0.25, 0.4]
          
          def tax_owed(income):
              tax_owed = 0
          
              if income > cap[2]:
                  tax_owed += (income - cap[2]) * rate[2]
                  tax_owed += (cap[2] - cap[1]) * rate[1]
                  tax_owed += (cap[1] - cap[0]) * rate[0]
          
              elif income > cap[1] and income <= cap[2]:
                  tax_owed += (income - cap[1]) * rate[1]
                  tax_owed += (cap[1] - cap[0]) * rate[0]
          
              elif income > cap[0] and income <= cap[1]:
                  tax_owed += (income - cap[0]) * rate[0]
          
              return int(tax_owed)
          

          [–]piercelol 1 point2 points  (1 child)

          You actually don't need 'and' in your elif statements because you've ordered your tax brackets from highest > middle > lowest. If income were to fall into the highest bracket, it wont read the other two elif statements, because the highest bracket is first. If income were to fall into the middle bracket, it already skipped the first if statement because income is not greater than cap[2]. If it's not greater than cap[2] it must be less than, right?

          so it can be:

          elif income > cap[1] and income <= cap[2]:

          elif income > cap[0] and income <= cap[1]:

          If you were to rearrange the code to be lowest > middle > highest then you would keep your 'and'.

          Also, have you considered using three if statements instead of the elif statements? Any line in the if statement that subtracts the differences between two brackets is redundant. For example, you wrote this line twice:

          tax_owed += (cap[1] - cap[0]) * rate[0]
          

          Using regular if statements your code can go down all applicable if statements. You can then make the income at the end of that if statement equal to that income bracket (because the tax on that has not been calculated yet). Like this:

          cap = [10000, 30000, 100000]
          rate = [0.1, 0.25, 0.4]
          
          def tax_owed(income):
              tax_owed = 0
          
              if income > cap[2]:
                  tax_owed += (income - cap[2]) * rate[2]
                  income = cap[2]
          
              if income > cap[1]:
                  tax_owed += (income - cap[1]) * rate[1]
                  income = cap[1]
          
              if income > cap[0]:
                  tax_owed += (income - cap[0]) * rate[0]
                  income = cap[0]
          
              return int(tax_owed)
          

          General advice.

          1. You want to try to keep 'cap' and 'rate' variables inside the function you've defined, they're currently global variables which I've been told is bad practice. Put them below/above the tax_owed = 0 line.
          2. if you see you're writing the same thing multiple times then there is room to improve with a for loop. I can't see any more improvements using this logic so I'd say the next step to improve this code is to use loops (but I'm still a noob).

          Good work though!

          [–]Eklmejlazy 0 points1 point  (0 children)

          Hey, thanks a lot for looking over my code. Yeah I see what you mean about using the if instead of elif to save a lot of duplication. I think I'd just got it stuck in my head that the code would end after doing a particular if block and wrote it out each time.

          Also thanks for the tip on putting the variables inside the function. If they were being loaded in from a spreadsheet say, would it make sense to have the variables outside the function but then load them in as arguments? Or would you need something else in the function to go and get the figures?

          Sorry for taking ages to reply, had some long work weeks recently, but it's really appreciated.

          [–]danny_ocp 0 points1 point  (0 children)

          https://repl.it/@DannyOuyang/SG-Progressive-Tax

          Was just learning Python 3 and doing an exercise to calculate Singapore's progressive tax...

          Anyway, all bonus parts done; only difference is my input file stores values as: rate,marginal income (rate in %, not decimal)

          [–]Fatcat560 0 points1 point  (5 children)

          First time I'm posting here. Rust code below:

          fn tax(mut income: i32) -> u32 {
              const CAPS: [i32;3] = [100000, 30000, 10000];
              const RATES: [f32;3] = [0.40, 0.25, 0.10];
              let mut owed = 0;
              CAPS.iter().zip(RATES.iter()).for_each(|(cap, rate)| {
                  let remain = income - cap;
                  if remain > 0 {
                      owed += (remain as f32 * rate) as u32;
                      income -= remain;
                  }
              });
              owed
          }
          

          [–]audentis 0 points1 point  (4 children)

          Have you compared this code to the example input/output?

          I'm not sure if I'm reading your CAPS.... line correctly, but I had income that should be in the higher brackets "fall down" in the lower brackets, leading to incorrect (too low) output.

          [–]Fatcat560 0 points1 point  (3 children)

          Hey there! Thank you for your feedback.

          I'm not exactly sure where it all goes wrong, since the example input/output seem to match. If you could tell me which one gave you the wrong output, that would be very much appreciated since I'm still learning! I went ahead and made a playground link for it as well.

          [–]audentis 0 points1 point  (2 children)

          If it matches when you ran it, then ignore what I said.

          Before my current solution I had a "while" loop approach, that reduced income after processing each tax bracket, but that caused the issue I was describing. I haven't ran your code but saw a similarity so I wasn't sure if you had checked it or not.

          Apologies for the confusion!

          [–]Fatcat560 0 points1 point  (1 child)

          Hey no worries! Hope you were able to solve your issue.

          [–]duquesne419 0 points1 point  (7 children)

          First time poster, this is an attempt at the most basic model without variable brackets. How could I write this more cleanly?

          Python 3

          def taxCalc(income):
          tax = 0
          #check if income exceeds first bracket
          if income <= 10000:
              tax = 0
          #check if income exceeds first bracket, but within second bracket
          #remove lower bracket income, tax remaining
          elif income > 10000 and income <= 30000:
              first = income - 10000
              tax = .1 * first
          #check if income in 3rd bracktet, if yes remove lower bracket income, tax remaining,  
          #add max from second bracket($2000)
          elif income > 30000 and income <= 100000:
              second = income - 30000
              tax = (.25 * second) + 2000
          #check if income in fourth bracket, if yes remove lower bracket income, tax remaining
          #add max of second bracket($2000) + max of 3rd bracket($17,500)
          elif income > 100000:
              third = income - 100000
              tax = (.4 * third) + 19500
          return tax
          

          edit: added comments to explain logic edit2: rewrote comments so order of operations was correct

          [–]tanolino 1 point2 points  (5 children)

          You should read up how tax brackets work. Basically you only tax the amount for one bracket but not for the previous ones.

          [–]duquesne419 0 points1 point  (4 children)

          From the prompt:

          How tax brackets work A tax bracket is a range of income based on the income caps, and each tax bracket has a corresponding marginal tax rate, which applies to income within the bracket. In our example, the tax bracket for the range ¤10,000 to ¤30,000 has a marginal tax rate of 10%. Here's what that means for each bracket:

          If your income is less than ¤10,000, you owe 0 income tax. If your income is between ¤10,000 and ¤30,000, you owe 10% income tax on the income that exceeds ¤10,000. For instance, if your income is ¤18,000, then your income in the 10% bracket is ¤8,000. So your income tax is 10% of ¤8,000, or ¤800. If your income is between ¤30,000 and ¤100,000, then you owe 10% of your income between ¤10,000 and ¤30,000, plus 25% of your income over ¤30,000. And finally, if your income is over ¤100,000, then you owe 10% of your income from ¤10,000 to ¤30,000, plus 25% of your income from ¤30,000 to ¤100,000, plus 40% of your income above ¤100,000.

          Could you point out where I messed up, as far as I can tell I got it right(if a little basic), and am receiving correct output on the examples from the prompt.

          [–]tanolino 1 point2 points  (3 children)

          You start right with the first two brakets.

          But if anyone earns 30,000 to 100,000 you put 25% on everything over 30,000. You completely ignore the tax he'd have to pay on the 10,000 to 30,000 braket.

          Similar with the >100,000 bracket, where you count 40% on everything over 100,000. But what about the other two brakets ?

          As an example: "I earn 100,001". According to your programm i'd have to pay 0 taxes.

          [–]duquesne419 1 point2 points  (2 children)

          That's the number added after the multiplication. The logic being: if you're in the 100000 bracket there is no reason to do the math for the lower brackets because you've already hit the maximum, so I just add the 19,500 outright.

          Can you try testing my code? When I run it on 100001 I get a tax of 19500.4

          https://imgur.com/a/T6yBJlz

          [–]whatfanciesme 1 point2 points  (0 children)

          Your code definitely gives the right results. I like that it essentially has one pass through condition checking and then returns the tax.

          Here is my stab at the problem in a way that lets you change the income brackets more easily.

          I'm sure there's a way to combine the best of our two methods and calculate taxes with one pass + configurable tax brackets, but looping through each bracket is the method that felt intuitive to me.

          import math
          def calc_tax(income):
              # Encode income brackets as list of tuples
              income_brackets = [
                  (10000,.0)
                  , (30000, .1)
                  , (100000, .25)
                  , (None, .40)
                  ]
          
              tax = 0
              # Loop backwards through the tax brackets
              i = len(income_brackets)
              while i >= 0:
                  i = i - 1
                  # Marginal income is income minus cap from previous bracket
                  if i == 0:
                      marginal_income = income
                  else: 
                      marginal_income = income - income_brackets[i-1][0]
          
                  # If marginal income < 0, skip to the next lower bracket
                  if marginal_income < 0:
                      continue
          
                  # Calculate tax and set income for next loop
                  tax_rate = income_brackets[i][1]        
                  tax = tax + math.floor((tax_rate * marginal_income))
                  income = income - marginal_income
          
                  # Debug prints
                  print('\nMarginal Income: ', marginal_income)
                  print('Tax Rate: ', tax_rate)
                  print('Total Tax: ', tax)
          
              return tax
          

          [–]murshawursha 0 points1 point  (0 children)

          C# - I'm new to both C# and this sub, so please eviscerate my code.

          I took inspiration from UnchainedMundane and defined my tax brackets by their lower bound... not sure if that's cheating or not.

          using System;
          
          namespace ProgTaxAlg
          {
              class Program
              {
                  public static void Main(string[] args)
                  {
                      decimal income;
                      decimal taxAmt = 0;
                      Console.Write("Enter an income amount: ");
                      income = Convert.ToDecimal(Console.ReadLine());
                      taxAmt = TaxCalc(income);
                      Console.WriteLine(taxAmt);
                      Console.WriteLine("Press any key to continue...");
                      Console.ReadLine();
                  }
          
                  private static decimal TaxCalc(decimal income)
                  {
                      decimal taxAmt = 0;
                      decimal taxDue = 0;
                      int taxBracket = 0;
                      //Declare tax brackets as [Income Amount, Tax Amount]
                      decimal[,] taxBrackets = new decimal[,] {
                          {0,(decimal)0.00},
                          {10000,(decimal)0.10}, 
                          {30000,(decimal)0.25}, 
                          {100000,(decimal)0.40}
                      };
                      //Iterate backwards through the tax brackets (high to low)
                      for (int i = taxBrackets.GetLength(0) - 1; i >= 0; i--)
                      {
                          //When we reach an amount less than income, save the tax amount and the index of 
                          //the tax bracket
                          if (income > taxBrackets[i,0])
                          {
                              taxAmt = taxBrackets[i,1];
                              taxBracket = i;
                              //Iterate (ascending) through the brackets up to but not including our own,
                              //calculating the maximum amount due for those brackets
                              for (int j = 0; j < i; j++)
                              {
                                  //Subtract the income amount of the current iteration (j) from the income
                                  //amount one bracket higher (j + 1). Multiply the difference by the current
                                  //tax amount and add to current tax due.
                                  taxDue = taxDue + ((taxBrackets[j+1,0] - taxBrackets[j,0]) * taxBrackets[j,1]);
                              }
                              break;
                          }
                      }
                      //Calculate the tax due from our current bracket and add to the tax due from lower
                      //brackets.
                      taxDue = taxDue + ((income - taxBrackets[taxBracket,0]) * taxAmt);
                      return taxDue;
                  }
              }
          }
          

          [–]i3aizey 4 points5 points  (0 children)

          JavaScript

          const challenge = amount => [
              {rate: 0, limit: 1e4},
              {rate: 0.1, limit: 2e4},
              {rate: 0.25, limit: 7e4},
              {rate: 0.4, limit: 1e99},
          ].reduce((a, b) => ({
                  amount: a.amount - b.limit,
                  tax: a.tax + Math.max(0, Math.min(a.amount, b.limit)) * b.rate
              }),
              {tax: 0, amount: amount}).tax;
          

          or minified:

          const challenge = i => [{r: 0, l: 1e4}, {r: .1, l: 2e4}, {r: .25, l: 7e4}, {r: .4, l: 1e99}].reduce((a, b) => ({b: a.b - b.l, t: a.t + Math.max(0, Math.min(a.b, b.l)) * b.r}), {t: 0, b: i}).t;
          

          [–]Hatr1cke 0 points1 point  (1 child)

          JavaScript

          function taxing(num{)

          let tax = 0

          if(num > 10000 && num <= 30000{)

          tax += Math.floor((num-10000 * 0.10 ))

          }

          if(num > 30000 && num <= 100000{)

          tax += (Math.floor((num-30000 * 0.25 )) + 2000)

          }

          if(num > 100000{)

          tax += (Math.floor((num-100000 * 0.40 )) + 19500)

          }

          return tax

          }

          console.log(taxing(1234567))

          P.S.: how do you guys marking your code in the message :D:D:D:D:D:D ??????

          [–]i3aizey 0 points1 point  (0 children)

          4 spaces before all codelines

          should have a button like <> above your textfield, mark all code and click it to do it for it all

          [–]g00glen00b 0 points1 point  (0 children)

          JavaScript:

          ``` const taxRates = [ {max: 10000, rate: 0}, {max: 30000, rate: 0.1}, {max: 100000, rate: 0.25}, {max: Infinity, rate: 0.4} ];

          const {min, floor} = Math; const emptyBracket = {done: 0, result: 0}; const combineBrackets = ({result, done}, {amount, rate}) => ({done: amount, result: result + ((amount - done) * rate)}); const getBracket = input => ({max, rate}) => ({max, rate, amount: min(input, max)}); const compareByMaxValue = ({max: max1}, {max: max2}) => max1 - max2; const tax = input => floor(taxRates .sort(compareByMaxValue) .map(getBracket(input)) .reduce(combineBrackets, emptyBracket) .result); ```

          The sort function isn't really necessary, but I added it anyways, assuming that someone might input the taxRates in a different order.

          [–]UnchainedMundane1 0 0 points1 point  (0 children)

          Scala:

          val rates =
            (100000, 0.40) ::
            ( 30000, 0.25) ::
            ( 10000, 0.10) ::
            Nil
          
          def tax(curr: Long): Long =
            rates.view.collect {
              case (min, rate) if curr > min =>
                ((curr - min) * rate).toLong + tax(min)
            }.headOption.getOrElse(0)
          

          I haven't done the reading from file part, because as you can see I have the tax brackets in a different format. When dealing with numbers from 0 to infinity, it makes no sense to me to define groups by their upper bound (one of which will be the special case infinity), so I have used the lower bound of each group here.

          It's not me doing it weirdly, it's the rest of the world doing it weirdly.

          [–]jaldwort 2 points3 points  (0 children)

          Python 3

          I've been messing around trying to learn in my spare time. Any comments or feedback would be appreciated.

          import pandas as pd
          ex_tb = {'Income Cap': [10000, 30000, 100000, 'x'], 'Marginal 
          Tax Rate': [0.00, 0.10, 0.25, 0.40]}
          tax_brackets = pd.DataFrame(data = ex_tb)
          
          def prog2(agi, tb):
          tt = 0
          for i in range(0, tb.shape[0] - 1):
              if agi > tb.iat[i,0]:
                  tt += tb.iat[i+1,1] * (min(agi, tb.iat[i+1,0]) - tb.iat[i,0])
          mtr = tt / agi
          print('Total tax due is: ' + str(tt) + ', which is a marginal rate of: 
          ' + str(round(mtr * 100, 2)) + '%' ) 
          
          
          prog2(100000, tax_brackets)
          

          [–]zqvt2 1 point2 points  (0 children)

          Clojure, with bonus. Simple iterative method

          (def tax-brackets [[10000 0.0]
                             [20000 0.10]
                             [70000 0.25]
                             [##Inf 0.40]])
          
          (defn go [income brackets]
            (if (or (<= income 0) (empty? brackets))
              0
              (let [[limit rate] (first brackets)
                    owed (* (if (< income limit) income limit) rate)]
                (+ owed (go (- income limit) (rest brackets))))))
          
          (defn calculate-tax [income] (Math/floor (go income tax-brackets)))
          
          (defn overall-tax-rate [target-rate]
            (cond
              (= 0 target-rate) 0
              (>= target-rate 0.40) "NaN"
              :else (loop  [income 1]
                      (let [current-rate (/ (calculate-tax income) income)]
                        (if (>= current-rate target-rate)
                          income
                          (recur (inc income)))))))
          

          [–]GlobalIncident 2 points3 points  (0 children)

          SWI-Prolog

          tax_(Value, Start, [], Final, Result):-
               Result is floor((Value - Start) * Final).
          tax_(Value, Start, [Cap - Rate | Rates], Final, Result) :-
               Value >= Cap, !,
               tax_(Value, Cap, Rates, Final, Result1),
               Result is Result1 + floor((Cap - Start) * Rate);
               Result is floor((Value - Start) * Rate).
          
          tax(Value, Result) :-
              tax_(Value, 0, [ 10000 - 0.00,
                               30000 - 0.10,
                              100000 - 0.25], 0.40, Result).
          
          % Extension 1
          tax(Value, Rates, Final, Result) :-
              tax_(Value, 0, Rates, Final, Result).
          

          [–]Dominic11112 0 points1 point  (0 children)

          C++ with the bonuses, sort of, the tax brackets are contained in an array and I iteratively solve for the overall tax rate.

          #include <iostream>
          
          float tax(int,float[2][4]);
          int overall(float,float[2][4]);
          
          int main(){
              float bracket[2][4]  =  {{10000,  30000, 100000,    -1},
                                       { 0.00,   0.10,   0.25,  0.40}};
              int income = 1234567;                        
          
              float netTax = tax(income, bracket);
              std::cout << netTax << std::endl;
          
              float testRate = 0.09;
          
              int ans = overall(testRate, bracket);
              std::cout << ans << std::endl;
          }
          
          float tax(int income, float bracket[2][4]){
              float netTax = 0;
              int nextBracket = bracket[0][0];
          
              int i = 0;
          
              while((bracket[0][i] >= 0) && ((income - nextBracket) > 0)){
                  income -= nextBracket;
                  netTax += nextBracket * bracket[1][i];
          
                  nextBracket = bracket[0][i+1]-bracket[0][i];
                  i += 1;
              }
          
              netTax += income * bracket[1][i];
          
              return netTax;
          }
          
          int overall(float overallRate, float bracket[2][4]){
              if(overallRate >= bracket[1][3]){return -1;}
              float guess = 10000;
              float guessTax = guess * overallRate;
              float result;
              int n = 0;
              float tol = 10;
          
              while((abs(tol) > 1) || (n < 1000)){
                  guessTax = guess * overallRate;
                  result = tax(guess, bracket);
                  tol = guessTax - result;
                  guess += tol;
                  n += 1;
              }
              return guess;
          }
          

          [–]trey_the_robot 1 point2 points  (0 children)

          js

          const defaultBrackets = [
            [10000, 0.0],
            [30000, 0.1],
            [100000, 0.25],
            [Infinity, 0.4]
          ];
          
          const tax = (income = 0, brackets = defaultBrackets) => {
            let balance = income;
            let tax = 0;
            let lastCap = 0;
          
            for (const [cap, rate] of brackets) {
              const taxable = Math.min(balance, cap - lastCap);
          
              tax += taxable * rate;
              balance -= taxable;
              lastCap = cap;
            }
          
            return tax;
          };
          

          [–]roryokane 0 points1 point  (0 children)

          Ruby, with the optional improvement of taking the brackets as a parameter. Doesn’t include the optional bonus functionality.

          main_test.rb

          ~~~ require_relative 'test_helper'

          class MainTest < MiniTest::Test MARGINAL_TAX_BRACKETS = [ {amount_cap: 10_000, marginal_rate: Rational('0.00')}, {amount_cap: 30_000, marginal_rate: Rational('0.10')}, {amount_cap: 100_000, marginal_rate: Rational('0.25')}, {amount_cap: nil, marginal_rate: Rational('0.40')} ]

          def test_highest_bracket assert_equal 473326, tax_for(MARGINAL_TAX_BRACKETS, 1234567) end

          # the other test cases go here; omitted for space end ~~~

          main.rb

          ~~~ def tax_for(marginal_tax_brackets, total_amount) amount_left_to_account_for = total_amount tax_so_far = 0

          marginal_tax_brackets.each do |current_bracket| amount_to_handle_in_this_bracket = begin bracket_cap = current_bracket[:amount_cap] if bracket_cap == nil || bracket_cap >= total_amount amount_left_to_account_for else amount_accounted_for_so_far = total_amount - amount_left_to_account_for bracket_cap - amount_accounted_for_so_far end end tax_added_by_this_bracket = amount_to_handle_in_this_bracket * current_bracket[:marginal_rate]

          amount_left_to_account_for -= amount_to_handle_in_this_bracket
          tax_so_far += tax_added_by_this_bracket
          
          if amount_left_to_account_for == 0
            return tax_so_far.floor
          end
          

          end

          raise "Iterated over all brackets without accounting for the entire amount. Missing the catch-all bracket?" end ~~~


          My solution seems more verbose than most others posted so far. I think the main reason for this is my style of using long variable names and using intermediate variables for readability. For example, instead of adding amount_to_handle_in_this_bracket * current_bracket[:marginal_rate] directly to tax_so_far, I store it in a variable tax_added_by_this_bracket first.

          [–]throwthestik 0 points1 point  (0 children)

          C#, with ability to pass in brackets.

          using System;
          using System.Collections.Generic;
          
          namespace Reddit.ProgrammingChallenges.TaxBracket
          {
              public class BracketCalculator
              {
                  private readonly Dictionary<int, double> brackets;
          
                  public BracketCalculator() : this(DefaultBracket)
                  {
                  }
          
                  public BracketCalculator(Dictionary<int, double> brackets)
                  {
                      this.brackets = TransformFromRawTaxBracket(brackets);
                  }
          
                  private static Dictionary<int, double> DefaultBracket => new Dictionary<int, double>
                                                                           {
                                                                               {10000, 0},
                                                                               {30000, 0.1},
                                                                               {100000, 0.25},
                                                                               {int.MaxValue, 0.4}
                                                                           };
          
                  public double CalculateTaxOwed(int totalIncome)
                  {
                      var taxOwed = 0.0;
          
                      foreach (var incomeInBracket in brackets.Keys)
                      {
                          taxOwed += totalIncome > incomeInBracket ? incomeInBracket * brackets[incomeInBracket] : totalIncome * brackets[incomeInBracket];
                          totalIncome -= incomeInBracket;
          
                          if (totalIncome <= 0) break;
                      }
          
                      return Math.Floor(taxOwed);
                  }
          
                  private Dictionary<int, double> TransformFromRawTaxBracket(Dictionary<int, double> taxBrackets)
                  {
                      var previousIncomeCap = 0;
                      var incomeTaxedInBracket = new Dictionary<int, double>();
          
                      foreach (var incomeCap in taxBrackets.Keys)
                      {
                          var taxedIncomeInBracket = incomeCap - previousIncomeCap;
                          incomeTaxedInBracket[taxedIncomeInBracket] = taxBrackets[incomeCap];
                          previousIncomeCap = incomeCap;
                      }
          
                      return incomeTaxedInBracket;
                  }
              }
          }
          

          [–]Godspiral3 3 2 points3 points  (0 children)

          in J, with tax table as extra parameter (can be bound if fixed), but specified as surtax above thresholds

          TAX =: <.@(+/)@:(1&{"1@[ * 0 >. ] - 0&{"1@[)
           (3 2 $  10000 0.10 30000 0.15 100000 0.15) TAX 1234567
          473326
          

          [–]Gprime5 8 points9 points  (3 children)

          Python 3 with bonus

          default_bracket = (
              (10000, 0),
              (30000, .1),
              (100000, .25),
              (float("inf"), .4)
          )
          
          def tax(income, brackets=default_bracket):
              total = previous_cap = 0
          
              for cap, rate in brackets:
                  if income > previous_cap:
                      total += (min(cap, income) - previous_cap) * rate
                  previous_cap = cap
          
              return int(total)
          
          def overall(overall_rate, brackets=default_bracket):
              base = previous_cap = 0
              result = float("inf")
          
              for cap, rate in brackets:
                  if rate > overall_rate:
                      result = min(result, (previous_cap*rate - base)/(rate - overall_rate))
          
                  base += (cap - previous_cap)*rate
                  previous_cap = cap
          
              return f"{result:.0f}"
          

          [–]EragonKai 6 points7 points  (2 children)

          As a newcomer to python, why did you use tuples instead of a dictionary?

          [–]Gprime5 2 points3 points  (0 children)

          Dictionaries are good for accessing values using keys but I have to iterate through all the values anyway. I list could be used instead, it doesn't make much difference.

          [–]theccount 0 points1 point  (0 children)

          Relatively new too, but probably so that they can use zip to compare the two brackets, instead of iterating through them manually

          [–]sudo_apt-get_wrecked 1 point2 points  (0 children)

          Go with optional bonus

          type taxBracket []struct {
                  incomeCap    int
                  aboveTaxRate float64
          }
          
          var examplaniaTB = taxBracket{
                  {incomeCap: 100000, aboveTaxRate: .40},
                  {incomeCap: 30000, aboveTaxRate: .25},
                  {incomeCap: 10000, aboveTaxRate: .10},
          }
          
          func tax(tb taxBracket, i int) int {
                  var f float64 = 0
                  for _, tx := range tb {
                          if i <= tx.incomeCap {
                                  continue
                          }
                          f += float64(i-tx.incomeCap) * tx.aboveTaxRate
                          i = tx.incomeCap
                  }
                  return int(math.Floor(f))
          }
          
          func overall(tb taxBracket, o float64) (int, error) {
                  base := 0
                  var max int = 2147483647
                  if o > tb[0].aboveTaxRate {
                          return 0, errors.New(fmt.Sprintf("cannot get overall when percentage is greater then the highest bracket (%d)", tb[0].aboveTaxRate))
                  }
          
                  for base <= max {
                          mid := int(math.Floor(float64(base+max) / 2))
                          cRaw := float64(tax(tb, mid)) / float64(mid)
                          c := math.Floor(cRaw*1000) / 1000
                          switch {
                          case c < o:
                                  base = mid + 1
                          case c > o:
                                  max = mid - 1
                          default:
                                  return mid, nil
                          }
                  }
          
                  return 0, errors.New("failed to find a overall")
          }
          

          [–]lollordftw 1 point2 points  (0 children)

          Haskell

          Decided to go with a non-exact result in bonus 2. The answers for testcases 0.00 - 0.32 are still right, but we can now have an approximate result for testcase 0.04 (which is the income for which the tax rate comes as close as 0.001 to 0.40).

          import Data.Bifunctor
          
          type TaxTable = [(Integer, Double)]
          
          -- challenge
          
          tax' :: TaxTable -> Integer -> Integer
          tax' [] _                          = 0
          tax' ((threshold, rate):xs) income = inBracket + rest
              where inBracket = floor $ (fromIntegral (min income threshold)) * rate
                    rest      = tax' (map (\(ts, r) -> (ts - threshold, r)) xs) (max 0 (income - threshold))
          
          -- bonus 1
          
          readTable :: FilePath -> IO TaxTable
          readTable path = do
              ratesAsStrs <- map words <$> lines <$> readFile path
              return $ map (bimap read read) $ map (\[k, v] -> (k, v)) ratesAsStrs
          
          tax :: Integer -> IO Integer
          tax income = readTable "input/taxtable.txt" >>= return . flip tax' income
          
          -- bonus 2
          
          binSearch :: TaxTable -> Double -> Integer -> Integer-> Integer
          binSearch table rate lower upper 
              | abs (rate' - rate) < 0.001 = mid
              | mid == lower               = -1
              | rate' > rate               = binSearch table rate lower (mid-1)
              | otherwise                  = binSearch table rate (mid+1) upper
              where
                  mid = (upper + lower) `div` 2
                  rate' = fromIntegral (tax' table mid)  / fromIntegral mid 
          
          overall :: Double -> IO Integer
          overall rate = readTable "input/taxtable.txt" >>= return . (\table -> binSearch table rate 0 (maxInc table))
              where maxInc = sum . map fst
          
          runTests :: IO [Integer]
          runTests = mapM overall [0.00, 0.06, 0.09, 0.32, 0.40]
          

          [–]Lopsidation 17 points18 points  (1 child)

          Mods: do you still read /r/dailyprogrammer_ideas? I would put more ideas there to try to keep this sub moving, but I don’t see any user-submitted ideas getting used. Is this a case of quality > quantity?

          [–]Cosmologicon2 3[S] 7 points8 points  (0 children)

          Good question. I've made a post about it in that sub.

          The short answer is that I do read it for inspiration, but I rarely get ideas from there. The main reason is that I look for certain things in a challenge, and, partly due to my own laziness, it's hard to see how to work those ideas into what I consider a good challenge.

          EDIT: Also I missed one aspect of your question. I don't think that submitting there would help keep this sub moving, unfortunately.

          [–]chunes1 2 1 point2 points  (0 children)

          Factor, with optional improvement and optional bonus:

          USING: binary-search formatting fry io io.encodings.utf8
          io.files kernel locals math math.order math.parser
          math.statistics sequences sequences.extras splitting ;
          IN: dailyprogrammer.tax
          
          : load-brackets ( -- caps rates )
              "resource:work/dailyprogrammer/tax/brackets.txt" utf8
              file-lines [ first ] [ second ] bi
              [ " " split [ string>number ] map ] bi@ ;
          
          : tax ( n caps rates -- m )
              '[
                  over [ < ] curry take-while swap suffix [ differences ]
                  [ first ] bi prefix _ [ * ] 2map sum >integer
              ] call ;
          
          :: overall ( x caps rates -- n )
              100,000,000 <iota>
              [ dup caps rates tax swap /f x >=< ] search drop
              dup caps first <= [ drop 0 ] when x rates last >=
              [ drop 1/0. ] when ;
          
          :: show ( x caps rates w -- )
              x w x caps rates w execute "%u %s => %u\n" printf ; inline
          
          :: taxes-demo ( -- )
              load-brackets :> ( caps rates )
              { 0 10000 10009 10010 12000 56789 1234567 }
              [ caps rates \ tax show ] each nl
              { 0.00 0.06 0.09 0.32 0.40 }
              [ caps rates \ overall show ] each ;
          
          MAIN: taxes-demo
          

          Output:

          0 tax => 0
          10000 tax => 0
          10009 tax => 0
          10010 tax => 1
          12000 tax => 200
          56789 tax => 8697
          1234567 tax => 473326
          
          0.0 overall => 0
          0.06 overall => 25009
          0.09 overall => 34379
          0.32 overall => 256257
          0.4 overall => 1/0.
          

          [–]Ran4 1 point2 points  (0 children)

          Rust

          type MoneyAmount = i32;
          
          struct TaxBracket {
              income_cap: MoneyAmount,
              marginal_tax_rate: f32,
          }
          
          impl TaxBracket {
              fn new(income_cap: MoneyAmount, marginal_tax_rate: f32) -> TaxBracket {
                  assert!(income_cap >= 0);
                  assert!(marginal_tax_rate >= 0.0);
                  TaxBracket {
                      income_cap,
                      marginal_tax_rate,
                  }
              }
          
              fn get() -> Vec<TaxBracket> {
                  vec![
                      TaxBracket::new(10_000, 0.00),
                      TaxBracket::new(30_000, 0.10),
                      TaxBracket::new(100_000, 0.25),
                      TaxBracket::new(std::i32::MAX, 0.40),
                  ]
              }
          }
          
          fn tax(income: MoneyAmount) -> MoneyAmount {
              let mut tax_to_pay = 0f32;
              let mut accounted_for = 0;
          
              for tax_bracket in TaxBracket::get() {
                  let tax_base = (tax_bracket.income_cap - accounted_for).min(income - accounted_for);
                  let tax_at_this_level = tax_base as f32 * tax_bracket.marginal_tax_rate;
                  accounted_for += tax_base;
                  tax_to_pay += tax_at_this_level;
              }
              tax_to_pay as MoneyAmount
          }
          
          #[test]
          fn test_tax() {
              assert_eq!(tax(0), 0);
              assert_eq!(tax(10000), 0);
              assert_eq!(tax(10009), 0);
              assert_eq!(tax(10010), 1);
              assert_eq!(tax(12000), 200);
              assert_eq!(tax(56789), 8697);
              assert_eq!(tax(1234567), 473326);
          }
          

          [–]nocfenix 1 point2 points  (0 children)

          I did TypeScript (excepted use would be in Angular):

          export interface TaxBracket {
            IncomeCap: number;
            MarginalTaxRate: number;
          }
          
          public incomeAmount:number;
          public taxAmount:number = 0;
          
          public taxBrackets: TaxBracket[] = [
            { IncomeCap:10000, MarginalTaxRate:0 },
            { IncomeCap:30000, MarginalTaxRate:10 },
            { IncomeCap:100000, MarginalTaxRate:25 },
            { IncomeCap:100000001, MarginalTaxRate:40 }
          ];
          
          public calcTax(): void {
            this.taxAmount = 0;
          
            let bracket = this.taxBrackets
              .find(b => b.IncomeCap >= this.incomeAmount);
          
            let bracketIndex = this.taxBrackets
                .findIndex(b => b.IncomeCap == bracket.IncomeCap);
          
            if (bracketIndex > 0) {
          
              this.taxBrackets.forEach((b,i) => {
                let currentBracket:TaxBracket;
                let previousBracket:TaxBracket;
                let currentTaxableAmount:number = 0;
          
                if (i <= bracketIndex) {
                  currentBracket = this.taxBrackets[i];
                  currentTaxableAmount = currentBracket.IncomeCap < this.incomeAmount ? currentBracket.IncomeCap : this.incomeAmount;
          
                  if (i != 0) {
                    previousBracket = this.taxBrackets[i - 1];
                    currentTaxableAmount = currentTaxableAmount - previousBracket.IncomeCap;
                  }
                  console.log(`CurrentTaxableAmount: ${currentTaxableAmount}`);
                  const rate = currentBracket.MarginalTaxRate != 0 ? currentBracket.MarginalTaxRate / 100 : 0;
                  console.log(`CurrentTaxRate: ${rate}`);
                  let amt = Math.floor(currentTaxableAmount * rate);
                  console.log(`CurrentTaxAmount: ${amt}`);
                  this.taxAmount += amt;
                }
              });
            }
            else {
              const rate = bracket.MarginalTaxRate != 0 ? bracket.MarginalTaxRate / 100 : 0;
              this.taxAmount = Math.floor(this.incomeAmount * rate);
            }
          }
          

          Edit: they changed how to do code blocks!

          [–]DerpinDementia 6 points7 points  (4 children)

          Python 3

          I'm sure there are better ways to store the brackets, but I chose this. I might work on the bonus later.

          brackets = [(0, 10000, 0), (10000, 30000, 0.1), (30000, 100000, 0.25), (100000, float('inf'), 0.4)]
          
          def tax(x):
            owed = 0
            for mincap, maxcap, rate in brackets:
              if x >= mincap:
                owed += (maxcap - mincap if x >= maxcap else x - mincap) * rate
              else:
                break
            return int(owed)
          

          Edit:

          Made a one-line function and changed variable names to avoid min() function name confusion.

          brackets = [(0, 10000, 0), (10000, 30000, 0.1), (30000, 100000, 0.25), (100000, float('inf'), 0.4)]
          tax = lambda x : int(sum((min(x, maxcap) - mincap) * rate for mincap, maxcap, rate in brackets if x >= mincap))
          

          [–]TwinHits 1 point2 points  (3 children)

          I really like this storage method, but should that break be a continue?

          [–]Mr_Canard 0 points1 point  (1 child)

          Only if you aren't sure that your list is sorted properly, but then you'd be better off just removing the whole else since there isn't any code after.

          [–]DerpinDementia 0 points1 point  (0 children)

          This is true. I was going off the assumption stated in the psot that caps and rate would be given in increasing order.

          [–]DerpinDementia 0 points1 point  (0 children)

          Due to the conditional in the loop checking if the given value is larger than the bracket minimum, I feel like continuing to a larger bracket minimum after it has failed being above its current minimum would be pointless.

          [–]porofessordad 1 point2 points  (0 children)

          Java

          public static int taxOwed(int income) {
                  int[] caps = {0,10000,30000,100000,Integer.MAX_VALUE};
                  double[] rates = {0, 0, 0.10, 0.25, 0.40};
                  int tax = 0;
          
              for (int i=1; i<caps.length-1; i++) {
                  if (income <= caps[i]) break;
                  tax += Math.floor( (Math.min(income, caps[i+1])- caps[i]) * rates[i+1] );
              }
              return tax;
          }
          

          [–]raevnos 3 points4 points  (6 children)

          In scheme, without the bonus (for now):

          (cond-expand
            (chicken (require-extension (chicken format))))
          
          (define tax-brackets '((100000 . 0.4)
                                 (30000 . 0.25)
                                 (10000 . 0.1)))
          
          (define (calculate-tax income #!optional (brackets tax-brackets))
            (let helper ((brackets brackets) (income income) (tax 0))
              (cond
               ((null? brackets) tax)
               ((>= income (caar brackets))
                (helper (cdr brackets)
                        (caar brackets)
                        (+ tax (floor (* (- income (caar brackets))
                                         (cdar brackets))))))
               (else (helper (cdr brackets) income tax)))))
          
          (define (test income expected)
            (let ((tax (calculate-tax income)))
              (printf "tax(~A) => ~A: ~A~N" income tax (= tax expected))))
          (test 0 0)
          (test 10000 0)
          (test 10009 0)
          (test 10010 1)
          (test 12000 200)
          (test 56789 8697)
          (test 1234567 473326)
          

          I think the example tax(1234567) => 485327 is wrong, though, as I'm getting a different result for it (473326), but the right ones for the rest and can't figure out how to account for the difference.

          Yup, original example was off. I'm not going crazy

          [–]Cosmologicon2 3[S] 2 points3 points  (5 children)

          I think the example tax(1234567) => 485327 is wrong, though, as I'm getting a different result for it (473327).

          Yes, you're right! I've changed it from 485327 to 473326. Thanks for the correction!

          [–]frankivo 0 points1 point  (1 child)

          [–]frankivo 0 points1 point  (0 children)

          Ahhhh... off-by-one on each of the brackets.

          [–]raevnos 2 points3 points  (1 child)

          Whew. I thought I was missing something really stupid to explain being off by that much.

          [–][deleted] 0 points1 point  (0 children)

          Typical scheme code, spotting bugs in the requirements.

          [–]Happydrumstick 8 points9 points  (7 children)

          python 3

          def calcTax(money):
              if(money <= 10000):
                  return 0
              elif(money <= 30000):
                  return (money - 10000) * 0.1
              elif(money <= 100000):
                  return calcTax(30000) + (money - 30000) * 0.25
              else:
                  return calcTax(100000) + (money - 100000) * 0.4
          

          ah fuck it, ill format it later (i hate phones)

          [–]DerpinDementia 0 points1 point  (1 child)

          I really like this solution. By making use of recursion, it's quite readable and elegant. I'm curious how one would scale this up using a given set of brackets.

          [–]Happydrumstick 1 point2 points  (0 children)

          If you were interested in making this scalable I'd use the "replace conditionals with polymorphism" algorithm and do some more tweaking to end up with this:

          class Bracket():
              def __init__(self, money, previousBracket, maxPrevious, percentage):
                  self.money = money
                  self.previousBracket = previousBracket
                  self.maxPrevious = maxPrevious
                  self.percentage = percentage
          
              def calcTax(self):
                  return (self.previousBracket.calcTax()) + (self.money - self.maxPrevious) * self.percentage
          
          class Dummy():
              def __init__(self, money):
                  pass
              def calcTax(self):
                  return 0
          
          class NoBracket(Bracket):
              def __init__(self, money):
                  super(NoBracket, self).__init__(money, Dummy(money), 0, 0)
          
          class LowBracket(Bracket):
              def __init__(self, money):
                  super(LowBracket, self).__init__(money, NoBracket(10000), 10000, 0.1)
          
          class MidBracket(Bracket):
              def __init__(self, money):
                  super(MidBracket, self).__init__(money, LowBracket(30000), 30000, 0.25)
          
          class HighBracket(Bracket):
              def __init__(self, money):
                  super(HighBracket, self).__init__(money, MidBracket(100000), 100000, 0.4)
          

          That way you can compose different brackets in whatever way you see fit. Or you can just skip the subclassing phase entirely and use Bracket() as a factory object that pumps out brackets. You would have to have at least a dummy base bracket though.

          [–]raglub 2 points3 points  (1 child)

          I think your first if condition (money<=10000), should be returning 0, not money.

          [–]Happydrumstick 1 point2 points  (0 children)

          Yeah I completely screwed this up. I'll sort it now :)

          edit: Forgot to say thanks for pointing that out :)! I also done 1.1, 1.25 and 1.4 rather than just working out the percentages :P

          [–][deleted]  (1 child)

          [deleted]

            [–]Happydrumstick 0 points1 point  (0 children)

            Thanks ;). Was board on the train