Gamified challenge · Stack
Wagons numbered 1 to n arrive on the incoming track in the order given by wagons. Between the tracks is a dead-end siding: P pushes the next incoming wagon into it, and O pops the siding's top wagon onto the outgoing track. The outgoing track must receive 1, 2, …, n in order.
Write shunt(wagons) that returns the moves as a string of P and O, or "IMPOSSIBLE" when it can't be done. For [3, 1, 2] that's "PPOPOO"; for [2, 3, 1] it's "IMPOSSIBLE".
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google