Drill

ProblemsPython › orders

Is this status change allowed

mediumordersPython

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

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

Solve it in the editor →

Where you start

def can_transition(current: str, target: str) -> bool:
    

Worked examples

CallResult
can_transition("placed", "paid")True
can_transition("paid", "refunded")True
can_transition("shipped", "delivered")True
can_transition("placed", "shipped")False

Hint

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

Reference solution in Python
def can_transition(current: str, target: str) -> bool:
    allowed = {'placed>paid', 'placed>cancelled', 'paid>packed', 'paid>refunded', 'packed>shipped', 'shipped>delivered'}
    return current + '>' + target in allowed

The same problem in another language

More orders problems in Python