Code

Round-Robin Refueling

You could use a list as a queue, but list.pop(0) is slow: every remaining item must shuffle one step left, O(n) per pop. A deque pops from the left in O(1).

from collections import deque

q = deque(["Ada", "Bo"])
q.append("Cy")        # join the back
name = q.popleft()    # serve the front -> "Ada"

Round robin

Schedulers often serve jobs in round-robin order: each job gets a small turn; if it isn't finished, it goes to the back of the line. Operating systems share the CPU between programs this way.

The Lantern Keeper has one fuel pump and a line of lanterns. Each turn, the front lantern gets up to per_turn liters. If it still needs fuel, it rejoins the back of the queue.

Assignment

Complete refuel_order(lanterns, per_turn):

  1. lanterns is a list of (name, liters_needed) tuples.
  2. Put them in a deque. While it isn't empty, popleft() one.
  3. Subtract per_turn from what it needs. If it still needs more than 0, append it back (with the new amount). Otherwise add its name to the finished list.
  4. Return finished, the names in the order they were filled.