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

int surchargeByMethod(std::map<std::string, int> surchargeRates, std::string method, int subtotal) {
    
}

Worked examples

CallResult
surchargeByMethod(std::map<std::string, int>{{std::string("visa"), 150}, {std::string("amex"), 300}}, std::string("amex"), 10000)300
surchargeByMethod(std::map<std::string, int>{{std::string("visa"), 150}}, std::string("paypal"), 5000)0
surchargeByMethod(std::map<std::string, int>{{std::string("visa"), 150}}, std::string("visa"), 8000)120
surchargeByMethod(std::map<std::string, int>{}, std::string("visa"), 1000)0

Hint

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

Reference solution in C++
int surchargeByMethod(std::map<std::string, int> surchargeRates, std::string method, int subtotal) {
    auto it = surchargeRates.find(method);
    int rate = it == surchargeRates.end() ? 0 : it->second;
    return subtotal * rate / 10000;
}

The same problem in another language

More billing problems in C++