Look up the tax rate for a region
A billing system stores tax rates by region code. Look up the rate; an unknown region is not taxed.
- Rates are stored as integer percentages in minor units.
- A region not present in the map returns 0.
RegionTax(regionRates: map<string, int>, region: string) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int RegionTax(Dictionary<string, int> regionRates, string region) {
}
Worked examples
| Call | Result |
|---|---|
RegionTax(new Dictionary<string, int> { { "US", 700 }, { "EU", 1900 }, { "TR", 1800 } }, "EU") | 1900 |
RegionTax(new Dictionary<string, int> { { "US", 700 } }, "UK") | 0 |
RegionTax(new Dictionary<string, int> { }, "US") | 0 |
RegionTax(new Dictionary<string, int> { { "DE", 1900 }, { "FR", 2000 } }, "DE") | 1900 |
Hint
Map lookup with a default.
Reference solution in C#
public int RegionTax(Dictionary<string, int> regionRates, string region) {
return regionRates.ContainsKey(region) ? regionRates[region] : 0;
}