Overview
Schedule jobs fairly with a first-in, first-out queue: each job runs for a time slice and goes back in line until it's done, with deque instead of a slow list.pop(0).
- first in, first out
- deque.append and popleft in O(1)
- list.pop(0) shifts every item
Short Course
A queue serves items first in, first out. collections.deque adds and
removes at both ends in O(1). Trap: list.pop(0) shifts every remaining
item, so it costs O(n) each time.
from collections import deque
line = deque(["ana", "bo"])
line.append("cy") # joins at the back
line.popleft() # 'ana' is served first
Task
Write round_robin(jobs, slice_ms): jobs is a list of (name, ms). Each
turn the front job runs up to slice_ms; if it isn't finished, it goes to
the back. Return the names in the order they finish. Example:
round_robin([('a', 3), ('b', 1)], 2) → ['b', 'a'].
Test Suites
Pending
Next
Output