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
public int ZoneRate(Dictionary<string, int> zoneRates, string zone) {
}
Worked examples
| Call | Result |
|---|---|
ZoneRate(new Dictionary<string, int> { { "a", 120 }, { "b", 180 }, { "c", 250 } }, "a") | 120 |
ZoneRate(new Dictionary<string, int> { { "a", 120 }, { "b", 180 } }, "c") | 0 |
ZoneRate(new Dictionary<string, int> { }, "a") | 0 |
ZoneRate(new Dictionary<string, int> { { "x", 0 } }, "x") | 0 |
Hint
Map lookup, then a default.
Reference solution in C#
public int ZoneRate(Dictionary<string, int> zoneRates, string zone) {
return zoneRates.ContainsKey(zone) ? zoneRates[zone] : 0;
}