Drill

ProblemsC++ › billing

Look up the tax rate for a region

easybillingHash mapsC++

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

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.

Solve it in Python →

Where you start

int regionTax(std::map<std::string, int> regionRates, std::string region) {
    
}

Worked examples

CallResult
regionTax(std::map<std::string, int>{{std::string("US"), 700}, {std::string("EU"), 1900}, {std::string("TR"), 1800}}, std::string("EU"))1900
regionTax(std::map<std::string, int>{{std::string("US"), 700}}, std::string("UK"))0
regionTax(std::map<std::string, int>{}, std::string("US"))0
regionTax(std::map<std::string, int>{{std::string("DE"), 1900}, {std::string("FR"), 2000}}, std::string("DE"))1900

Hint

Map lookup with a default.

Reference solution in C++
int regionTax(std::map<std::string, int> regionRates, std::string region) {
    auto it = regionRates.find(region);
    return it == regionRates.end() ? 0 : it->second;
}

The same problem in another language

More billing problems in C++