Drill

ProblemsC# › billing

Payment method surcharge

easybillingHash mapsMathC#

Certain payment methods carry a surcharge. Look up the percentage and apply it to the subtotal.

SurchargeByMethod(surchargeRates: map<string, int>, method: string, subtotal: int) → 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

public int SurchargeByMethod(Dictionary<string, int> surchargeRates, string method, int subtotal) {
    
}

Worked examples

CallResult
SurchargeByMethod(new Dictionary<string, int> { { "visa", 150 }, { "amex", 300 } }, "amex", 10000)300
SurchargeByMethod(new Dictionary<string, int> { { "visa", 150 } }, "paypal", 5000)0
SurchargeByMethod(new Dictionary<string, int> { { "visa", 150 } }, "visa", 8000)120
SurchargeByMethod(new Dictionary<string, int> { }, "visa", 1000)0

Hint

Map lookup, then a single integer division by ten thousand.

Reference solution in C#
public int SurchargeByMethod(Dictionary<string, int> surchargeRates, string method, int subtotal) {
    int rate = surchargeRates.ContainsKey(method) ? surchargeRates[method] : 0;
    return subtotal * rate / 10000;
}

The same problem in another language

More billing problems in C#