Design a Todo List

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from typing import List
from collections import defaultdict

class TodoList:

    def __init__(self):
        self.tasks = {}  # Keeps track of tasks by task ID
        self.user_tasks = defaultdict(list)  # Keeps track of tasks for each user
        self.task_id_counter = 1  # Counter to assign unique task IDs

    def addTask(self, userId: int, taskDescription: str, dueDate: int, tags: List[str]) -> int:
        task = {
            'description': taskDescription,
            'dueDate': dueDate,
            'tags': set(tags),
            'completed': False
        }
        taskId = self.task_id_counter
        self.tasks[taskId] = task
        self.user_tasks[userId].append((taskId, dueDate))
        self.user_tasks[userId].sort(key=lambda x: x[1])  # Sort by due date
        self.task_id_counter += 1
        return taskId

    def getAllTasks(self, userId: int) -> List[str]:
        result = []
        for taskId, _ in self.user_tasks[userId]:
            if not self.tasks[taskId]['completed']:
                result.append(self.tasks[taskId]['description'])
        return result

    def getTasksForTag(self, userId: int, tag: str) -> List[str]:
        result = []
        for taskId, _ in self.user_tasks[userId]:
            task = self.tasks[taskId]
            if not task['completed'] and tag in task['tags']:
                result.append(task['description'])
        return result

    def completeTask(self, userId: int, taskId: int) -> None:
        # Check if the task belongs to the user and is not completed
        if taskId in [task[0] for task in self.user_tasks[userId]] and not self.tasks[taskId]['completed']:
            self.tasks[taskId]['completed'] = True