← back to unit
Challenge
# stacks advanced
The Switch Yard
Train cars 1..n arrive IN ORDER, and the yard has one dead-end side track (a stack!). Each arriving car may go straight out or wait on the side track; side-track cars can leave only from the top. Can you send them out in the wanted order? 🚃
You get the wanted departure order (a shuffle of 1..n). Print possible or impossible.
Example:
Input: wanted = [4, 5, 3, 2, 1]
Output: possible
Cars 1, 2, 3, 4 arrive and wait on the side track. Car 4 leaves straight from the top, car 5 arrives and leaves immediately, then 3, 2, 1 come off the side track in that order — exactly matching the wanted order.
💡 need a hint?
stk-switch-yard.py🔒 given lines are locked — write your code in between
loading...