Editor

Priority To-Do List

#260 · Heaps · Beginner · 45 XP · about 10 min

Your to-do list sorts itself. Every task gets a priority number, and 1 is the most urgent. Build a TodoList class:

  • TodoList() makes an empty list.
  • add(task, priority) adds a task (a string) with its priority (a positive whole number).
  • next_task() removes and returns the most urgent task: the one with the smallest priority number. When several tasks share that priority, the one added first comes out first. On an empty list it returns None.
  • count() returns how many tasks are still waiting.

Examples

todo = TodoList()
todo.add('pay rent', 1)
todo.add('water plants', 3)
todo.add('call mum', 1)
todo.next_task()   # returns 'pay rent'
todo.next_task()   # returns 'call mum'    (same priority, added later)
todo.count()       # returns 1

Note

Tasks with the same priority leave in the order they were added, not in alphabetical order. The same task can be added twice; each one is a separate entry.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.