Drill

ProblemsGo › orders

Is this status change allowed

mediumordersHash mapsGo

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

Go 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

func canTransition(current string, target string) bool {
	
}

Worked examples

CallResult
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 Go
func canTransition(current string, target string) bool {
	allowed := map[string]bool{
		"placed>paid": true, "placed>cancelled": true, "paid>packed": true,
		"paid>refunded": true, "packed>shipped": true, "shipped>delivered": true,
	}
	return allowed[current+">"+target]
}

The same problem in another language

More orders problems in Go