#43 · Stacks and queues · Beginner · 40 XP · about 8 min
Players stand in a circle and pass a potato around. The player holding it passes it k times, each time to the next player in the circle. Whoever holds it after the last pass leaves the game, and the player after them starts the next round holding the potato. Rounds go on until nobody is left.
Write hot_potato(names, k) that returns the names in the order the players leave, so the last name is the winner. The first name in names holds the potato at the start, and passing follows the list order, wrapping from the end back to the start.
hot_potato(["Ana", "Ben", "Cai", "Dev"], 2) # returns ['Cai', 'Ben', 'Dev', 'Ana'] hot_potato(["Ana", "Ben", "Cai"], 0) # returns ['Ana', 'Ben', 'Cai'] hot_potato(["Solo"], 5) # returns ['Solo']
In the first example Ana passes to Ben, who passes to Cai: Cai leaves. Dev starts the next round and passes to Ana, who passes to Ben: Ben leaves. Then Dev passes to Ana and Ana back to Dev, so Dev leaves and Ana wins.
With k equal to 0 the player holding the potato leaves straight away. Do not change the list you are given.
Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.