Drill

ProblemsC++ › orders

Is this status change allowed

mediumordersHash mapsC++

Orders move through a fixed lifecycle, and a support tool needs to know whether a requested change is one the system permits.

canTransition(current: string, target: string) → bool

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

bool canTransition(std::string current, std::string target) {
    
}

Worked examples

CallResult
canTransition(std::string("placed"), std::string("paid"))true
canTransition(std::string("paid"), std::string("refunded"))true
canTransition(std::string("shipped"), std::string("delivered"))true
canTransition(std::string("placed"), std::string("shipped"))false

Hint

A set of allowed pairs is easier to get right than a nest of ifs.

Reference solution in C++
bool canTransition(std::string current, std::string target) {
    std::set<string> allowed{"placed>paid", "placed>cancelled", "paid>packed", "paid>refunded", "packed>shipped", "shipped>delivered"};
    return allowed.count(current + ">" + target) > 0;
}

The same problem in another language

More orders problems in C++