Drill

ProblemsC++ › logistics

Which zone price applies

easylogisticsHash mapsC++

A carrier prices by zone. Look up the per-kilogram rate for a zone; a zone with no entry means it is not served.

zoneRate(zoneRates: map<string, int>, zone: 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 zoneRate(std::map<std::string, int> zoneRates, std::string zone) {
    
}

Worked examples

CallResult
zoneRate(std::map<std::string, int>{{std::string("a"), 120}, {std::string("b"), 180}, {std::string("c"), 250}}, std::string("a"))120
zoneRate(std::map<std::string, int>{{std::string("a"), 120}, {std::string("b"), 180}}, std::string("c"))0
zoneRate(std::map<std::string, int>{}, std::string("a"))0
zoneRate(std::map<std::string, int>{{std::string("x"), 0}}, std::string("x"))0

Hint

Map lookup, then a default.

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

The same problem in another language

More logistics problems in C++