β back to unit
Medium
# queues medium
The Snake
The arcade snake is a deque wearing a costume: eating grows a new head; moving grows a head AND drops the tail. π
You get commands β eat:x (new head x) or move:x (new head x, tail vanishes). The snake starts as just start. Print the body head to tail, comma-separated.
Example:
Input: start = "a1", commands = ["eat:a2", "move:a3", "move:a4"]
Output: a4,a3
eat:a2 grows a new head without losing the tail (a2, a1). move:a3 adds a new head AND drops the tail (a3, a2). move:a4 does the same (a4, a3).
π‘ need a hint?
q-the-snake.pyπ given lines are locked β write your code in between
loading...