Project 10 of 15, for Neil
Task Scheduler
A pit-stop scheduler for Neil's kart on race day: the most urgent job always next, first come first served on a tie, on a heap built by hand from a list.
Sign up free to open itLog in
Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilIt's race day at the kart track, and between sessions the jobs come in faster than I can do them.
- NeilA loose steering bolt can't wait, but a cracked number plate can wait all day.
- NeilIf two are just as urgent they get done in the order they came in, which is only fair.
- PaulThen you want a scheduler that keeps the most urgent job on top, whatever comes in.
A task scheduler for Neil's pit crew. Jobs on the kart come in all race day with a priority: 1 is the most urgent. Neil always does the most urgent one waiting, and of two with the same priority, the one that came in first. In scheduler.py, by hand, with no heapq:
push(heap, item)adds an item to a heap kept in a list, with the smallest item always atheap[0].pop(heap)removes the smallest item and hands it back. On an empty heap it raisesIndexError.TaskScheduler, built on yourpushandpop:add(job, priority)schedules a job,take()hands back the one to do next (raisingIndexErrorif there isn't one), andlen(scheduler)says how many are waiting.
Both push and pop should take O(log n) steps, so a thousand jobs waiting cost about ten swaps a time. The main block replays Neil's race day from day.txt.
Fixing the loose steering bolt Fixing the brake pads Fixing the tyre pressures Fixing the fuel leak Fixing the seat bolts Fixing the chain tension Still waiting: 1
What you learn
What's in it
- Smallest on top
- Smallest on top
- A tree kept in a list
- A 0 comes in
- Taking the top
- Which child?
- Most urgent, then first come
- Comparing tuples
- How Paul keeps his jobs board
- Fix Riya's version
- Make the task scheduler
- Ship it