
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):
lanternsis a list of(name, liters_needed)tuples.- Put them in a
deque. While it isn't empty,popleft()one. - Subtract
per_turnfrom what it needs. If it still needs more than 0,appendit back (with the new amount). Otherwise add its name to thefinishedlist. - Return
finished, the names in the order they were filled.
Complete refuel_order(lanterns, per_turn):
lanternsis a list of(name, liters_needed)tuples.- Put them in a
deque. While it isn't empty,popleft()one. - Subtract
per_turnfrom what it needs. If it still needs more than 0,appendit back (with the new amount). Otherwise add its name to thefinishedlist. - Return
finished, the names in the order they were filled.
main.py Python sleeping
Press Run to try your code, then Check to test it.