Drill

ProblemsJava › billing

Look up the tax rate for a region

easybillingHash mapsJava

A billing system stores tax rates by region code. Look up the rate; an unknown region is not taxed.

regionTax(regionRates: map<string, int>, region: string) → int

Java 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.

Solve it in Python →

Where you start

int regionTax(Map<String, Integer> regionRates, String region) {
    
}

Worked examples

CallResult
regionTax(Main.<String, Integer>mp("US", 700, "EU", 1900, "TR", 1800), "EU")1900
regionTax(Main.<String, Integer>mp("US", 700), "UK")0
regionTax(Main.<String, Integer>mp(), "US")0
regionTax(Main.<String, Integer>mp("DE", 1900, "FR", 2000), "DE")1900

Hint

Map lookup with a default.

Reference solution in Java
int regionTax(Map<String, Integer> regionRates, String region) {
    Integer r = regionRates.get(region);
    return r == null ? 0 : r;
}

The same problem in another language

More billing problems in Java