To-Do List by Priority
Implement a class TodoList that hands out tasks by priority. Write these methods:
TodoList()starts with no tasks.add_task(name, priority)puts a task on the list and returns how many tasks are on the list now. A higherprioritymeans more urgent.next_task()removes the most urgent task and returns its name. The most urgent task is the one with the highest priority, and among equal priorities the one that was added earliest. Returns""if the list is empty.peek_task()returns the name thatnext_taskwould return, without removing anything. Returns""if the list is empty.drop_task(name)removes the task callednameand returnstrue, or returnsfalseif no task with that name is on the list.
add_task is never called with the name of a task that is still on the list, but a name may be added again after its task is gone. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
TodoList()
add_task("mail", 2) returns 1
add_task("cook", 5) returns 2
add_task("gym", 5) returns 3
peek_task() returns "cook"
next_task() returns "cook"
drop_task("mail") returns true
drop_task("mail") returns false
next_task() returns "gym"
next_task() returns ""
cook and gym share the top priority 5; cook was added first, so it goes first. After mail is dropped, gym is next, and then the list is empty.
Constraints
1 <= priority <= 5nameis1to8lowercase letters.- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.