Drill

ProblemsJava › logistics

Which zone price applies

easylogisticsHash mapsJava

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

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 zoneRate(Map<String, Integer> zoneRates, String zone) {
    
}

Worked examples

CallResult
zoneRate(Main.<String, Integer>mp("a", 120, "b", 180, "c", 250), "a")120
zoneRate(Main.<String, Integer>mp("a", 120, "b", 180), "c")0
zoneRate(Main.<String, Integer>mp(), "a")0
zoneRate(Main.<String, Integer>mp("x", 0), "x")0

Hint

Map lookup, then a default.

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

The same problem in another language

More logistics problems in Java