Which zone price applies
A carrier prices by zone. Look up the per-kilogram rate for a zone; a zone with no entry means it is not served.
- Rates are per kilogram, in minor units.
- A zone that is not in the table is unsupported: return 0.
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.
Where you start
int zoneRate(std::map<std::string, int> zoneRates, std::string zone) {
}
Worked examples
| Call | Result |
|---|---|
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;
}