Is this status change allowed
Orders move through a fixed lifecycle, and a support tool needs to know whether a requested change is one the system permits.
- The only moves allowed are: placed to paid, placed to cancelled, paid to packed, paid to refunded, packed to shipped, shipped to delivered.
- Everything else is refused, including staying where you are.
- Unknown status names are refused.
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.
Where you start
public bool CanTransition(string current, string target) {
}
Worked examples
| Call | Result |
|---|---|
CanTransition("placed", "paid") | true |
CanTransition("paid", "refunded") | true |
CanTransition("shipped", "delivered") | true |
CanTransition("placed", "shipped") | false |
Hint
A set of allowed pairs is easier to get right than a nest of ifs.
Reference solution in C#
public bool CanTransition(string current, string target) {
var allowed = new HashSet<string> {
"placed>paid", "placed>cancelled", "paid>packed", "paid>refunded", "packed>shipped", "shipped>delivered" };
return allowed.Contains(current + ">" + target);
}