-
-
Save ryanheath/1235117 to your computer and use it in GitHub Desktop.
the tax thingy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public decimal GetTaxes(decimal salary) | |
{ | |
var taxBrackets = new[] | |
{ | |
new {UpperLimit = 5070m, Taxation = 0.10m}, | |
new {UpperLimit = 8660m, Taxation = 0.14m}, | |
new {UpperLimit = 14070m, Taxation = 0.23m}, | |
new {UpperLimit = 21240m, Taxation = 0.30m}, | |
new {UpperLimit = 40230m, Taxation = 0.33m}, | |
new {UpperLimit = decimal.MaxValue, Taxation = 0.45m}, | |
}; | |
var taxSlices = new[] {new {UpperLimit = 0m, Taxation = 0m}} | |
.Concat(taxBrackets) | |
.Zip(taxBrackets, | |
(previousBracket, taxBracket) => new | |
{ | |
Slice = taxBracket.UpperLimit - previousBracket.UpperLimit, | |
taxBracket.Taxation | |
}); | |
var tax = 0m; | |
foreach (var taxSlice in taxSlices) | |
{ | |
var taxedSlice = Math.Min(taxSlice.Slice, salary); | |
tax += taxedSlice * taxSlice.Taxation; | |
salary -= taxedSlice; | |
} | |
return tax; | |
} |
Added some Zip love, much nicer solution now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I could not resist to create a version that determine its own slices :)