25. Task Mananger#
The goal is to develop a task manager that allows students to practice working with lists, tuples, and loops in Python. The project is divided into several exercises, each focusing on a specific aspect of task management.
A task is a string, and a task list is, for now, just a list of strings.
25.1. Basic operations#
25.1.1. Create a Task List#
We start by implementing a function to create a task list. This function will initialize a list with a series of default tasks.
["Learn the while loop", "Learn the list", "Learn the for loop"]
Even though this function is simple, it helps keep the source code organized. You can add the following function to your code:
def create_task_list():
return ["Learn the while loop", "Learn the list", "Learn the for loop"]
25.1.2. Add Task#
A task is a string. Write a function add_task that takes two parameters: the task list and the task to be added. Test your function with the following instructions:
tasks = create_task_list()
add_task(tasks, "Finish this course")
print(tasks)
The expected output is:
['Learn the while loop', 'Learn the list', 'Learn the for loop', 'Finish this course']
Hint: The append
method allows us to add an element to the end of a list. This element can be of any type.
def add_task(l, s):
pass
25.1.3. Display Task List#
Write a function print_tasks
to display the tasks from the oldest to the newest. This function takes one parameter, the task list. Test your function with the following instructions:
tasks = create_task_list()
print_tasks(tasks)
print_tasks([])
The excepted output is
TO DO:
- Learn the while loop
- Learn the list
- Learn the for loop
TO DO:
*** Nothing ! \o/ ***
def print_tasks(l):
pass
25.1.4. Remove the Oldest Task#
Write a function remove_oldest
to remove the oldest task from the list.
Test your function with the following instructions:
tasks = create_task_list()
remove_oldest(tasks)
print_tasks(tasks)
remove_oldest(tasks)
remove_oldest(tasks)
print_tasks(tasks)
Expected result is
TO DO:
- Learn the list
- Learn the for loop
TO DO:
*** Nothing ! \o/ ***
Hint: remember, new tasks are added to the beggining. Is it always possible to remove a task?
def remove_oldest(l):
pass
25.1.5. Filtering tasks#
We want a function filter_tasks
to create a new list of tasks that contain a specific substring. Test your function with the following instructions:
tasks = create_task_list()
new_tasks = filter_tasks(tasks, "the for")
print(tasks)
print(new_tasks)
The expected result is
['Learn the while loop', 'Learn the list', 'Learn the for loop']
['Learn the for loop']
Hint: We want to create a new list, so the original list should not be modified.
def filter_tasks(l, substr):
pass
25.1.6. A main#
Combine all the above functions inside a single main
function. Remove all executable instructions from your code except for the call to the main
function.
def main():
tasks = create_task_list()
add_task(tasks, "Finish this course")
remove_oldest(tasks)
print(tasks)
new_tasks = filter_tasks(tasks, "Learn")
print(new_tasks)
main()
The excepted result is
TO DO:
- Learn the list
- Learn the for loop
- Finish this course
TO DO:
- Learn the list
- Learn the for loop
25.2. Task Priority#
We will now introduce task priorities. Each task will be associated with an integer priority, where 1
is the highest priority, and 3
is the lowest. Now, the task list will store tuples: (priority, task)
.
Since changing the structure of our task lists will affect the previous functions, we will adapt them one by one. The use of function in our code make it easy.
First, update the create_task_list
function to include priorities.
def create_task_list():
return [(1, "Learn the while loop"), (3, "Learn the list"), (2, "Learn the for loop")]
Test your code by calling the main
function. The expected result will be incorrect at first:
TO DO:
- (3, 'Learn the list')
- (2, 'Learn the for loop')
- Finish this course
TO DO:
*** Nothing ! \o/ ***
The tasks now show their priorities, but it doesn’t look nice. Also, the new task “Finish this course” has no priority, and the filter_tasks
function doesn’t work as expected. The only working function is remove_oldest
.
25.2.1. Adapt the Print Function#
We will start by adapting the print_tasks
function to handle tasks with priorities. Update the main
function to focus on printing tasks:
def main():
tasks = create_task_list()
#add_task(tasks, "Finish this course")
#remove_oldest(tasks)
print_tasks(tasks)
#new_tasks = filter_tasks(tasks, "Learn")
#print_tasks(new_tasks)
main()
The excepted result is
TO DO:
- (1, 'Learn the while loop')
- (3, 'Learn the list')
- (2, 'Learn the for loop')
You can now adapt the print_tasks
function in order to the tasks in a nicer way.
TO DO:
1. Learn the while loop
2. Learn the for loop
3. Learn the list
Hint : You can access the elements of a tuple (with []
) and use the sorted()
function to sort tasks by priority.
def print_tasks(l):
pass
25.2.2. Add a New Task#
Now update the add_task
function to accept a third parameter, the priority of the task. You can test your function.
def main():
tasks = create_task_list()
add_task(tasks, "Finish this course", 3)
#remove_oldest(tasks)
print_tasks(tasks)
#new_tasks = filter_tasks(tasks, "Learn")
#print_tasks(new_tasks)
main()
The expected result is
TO DO:
1. Learn the while loop
2. Learn the for loop
3. Finish this course
3. Learn the list
We can then see that the remove_oldest
function is working without problem.
The excepted result is
TO DO:
2. Learn the for loop
3. Finish this course
3. Learn the list
def add_task(l, s, p):
pass
25.2.3. Filter#
The final update is to adapt the filter_tasks
function to work with tasks that include priorities. Test your function with the following code:
def main():
tasks = create_task_list()
add_task(tasks, "Finish this course", 3)
remove_oldest(tasks)
print_tasks(tasks)
new_tasks = filter_tasks(tasks, "Learn")
print_tasks(new_tasks)
main()
The excepted result is
TO DO:
2. Learn the for loop
3. Finish this course
3. Learn the list
TO DO:
2. Learn the for loop
3. Learn the list
def filter_tasks(l, substr):
pass
25.3. Advanced operations#
25.3.1. Removing Task by Priority#
We want to create a function to remove task depending on their priority. We propose the following function. You can add it to your code and test it. Do you understand why there is an IndexError
? Propose a better version of this function.
def remove_priority(l: list, p: int):
for i in range(len(l)):
if l[i][0] == p:
l.pop(i)
def main():
tasks = create_task_list()
add_task(tasks, "Finish this course", 3)
print_tasks(tasks)
remove_priority(tasks, 3)
print_tasks(tasks)
main()
Excepted result:
TO DO:
1. Learn the while loop
2. Learn the for loop
3. Finish this course
3. Learn the list
TO DO:
1. Learn the while loop
2. Learn the for loop
def remove_priority(l, p):
pass