fromsortedcontainersimportSortedListclassTodoList:def__init__(self):self.i=1self.tasks=defaultdict(SortedList)defaddTask(self,userId:int,taskDescription:str,dueDate:int,tags:List[str])->int:taskId=self.iself.i+=1self.tasks[userId].add([dueDate,taskDescription,set(tags),taskId,False])returntaskIddefgetAllTasks(self,userId:int)->List[str]:return[x[1]forxinself.tasks[userId]ifnotx[4]]defgetTasksForTag(self,userId:int,tag:str)->List[str]:return[x[1]forxinself.tasks[userId]ifnotx[4]andtaginx[2]]defcompleteTask(self,userId:int,taskId:int)->None:fortaskinself.tasks[userId]:iftask[3]==taskId:task[4]=Truebreak# Your TodoList object will be instantiated and called as such:# obj = TodoList()# param_1 = obj.addTask(userId,taskDescription,dueDate,tags)# param_2 = obj.getAllTasks(userId)# param_3 = obj.getTasksForTag(userId,tag)# obj.completeTask(userId,taskId)
classTask{inttaskId;StringtaskName;intdueDate;Set<String>tags;booleanfinish;publicTask(inttaskId,StringtaskName,intdueDate,Set<String>tags){this.taskId=taskId;this.taskName=taskName;this.dueDate=dueDate;this.tags=tags;}}classTodoList{privateinti=1;privateMap<Integer,TreeSet<Task>>tasks=newHashMap<>();publicTodoList(){}publicintaddTask(intuserId,StringtaskDescription,intdueDate,List<String>tags){Tasktask=newTask(i++,taskDescription,dueDate,newHashSet<>(tags));tasks.computeIfAbsent(userId,k->newTreeSet<>(Comparator.comparingInt(a->a.dueDate))).add(task);returntask.taskId;}publicList<String>getAllTasks(intuserId){List<String>ans=newArrayList<>();if(tasks.containsKey(userId)){for(Tasktask:tasks.get(userId)){if(!task.finish){ans.add(task.taskName);}}}returnans;}publicList<String>getTasksForTag(intuserId,Stringtag){List<String>ans=newArrayList<>();if(tasks.containsKey(userId)){for(Tasktask:tasks.get(userId)){if(task.tags.contains(tag)&&!task.finish){ans.add(task.taskName);}}}returnans;}publicvoidcompleteTask(intuserId,inttaskId){if(tasks.containsKey(userId)){for(Tasktask:tasks.get(userId)){if(task.taskId==taskId){task.finish=true;break;}}}}}/** * Your TodoList object will be instantiated and called as such: * TodoList obj = new TodoList(); * int param_1 = obj.addTask(userId,taskDescription,dueDate,tags); * List<String> param_2 = obj.getAllTasks(userId); * List<String> param_3 = obj.getTasksForTag(userId,tag); * obj.completeTask(userId,taskId); */
usestd::collections::{HashMap,HashSet};#[derive(Clone)]structTask{task_id:i32,description:String,tags:HashSet<String>,due_date:i32,}structTodoList{/// The global task idid:i32,/// The mapping from `user_id` to `task`user_map:HashMap<i32,Vec<Task>>,}implTodoList{fnnew()->Self{Self{id:1,user_map:HashMap::new(),}}fnadd_task(&mutself,user_id:i32,task_description:String,due_date:i32,tags:Vec<String>,)->i32{ifself.user_map.contains_key(&user_id){// Just add the taskself.user_map.get_mut(&user_id).unwrap().push(Task{task_id:self.id,description:task_description,tags:tags.into_iter().collect::<HashSet<String>>(),due_date,});// Increase the global idself.id+=1;returnself.id-1;}// Otherwise, create a new userself.user_map.insert(user_id,vec![Task{task_id:self.id,description:task_description,tags:tags.into_iter().collect::<HashSet<String>>(),due_date,}],);self.id+=1;self.id-1}fnget_all_tasks(&self,user_id:i32)->Vec<String>{if!self.user_map.contains_key(&user_id)||self.user_map.get(&user_id).unwrap().is_empty(){returnvec![];}// Get the task vectorletmutret_vec=(*self.user_map.get(&user_id).unwrap()).clone();// Sort by due dateret_vec.sort_by(|lhs,rhs|lhs.due_date.cmp(&rhs.due_date));// Return the description vectorret_vec.into_iter().map(|x|x.description).collect()}fnget_tasks_for_tag(&self,user_id:i32,tag:String)->Vec<String>{if!self.user_map.contains_key(&user_id)||self.user_map.get(&user_id).unwrap().is_empty(){returnvec![];}// Get the task vectorletmutret_vec=(*self.user_map.get(&user_id).unwrap()).clone();// Sort by due dateret_vec.sort_by(|lhs,rhs|lhs.due_date.cmp(&rhs.due_date));// Return the description vectorret_vec.into_iter().filter(|x|x.tags.contains(&tag)).map(|x|x.description).collect()}fncomplete_task(&mutself,user_id:i32,task_id:i32){if!self.user_map.contains_key(&user_id)||self.user_map.get(&user_id).unwrap().is_empty(){return;}self.user_map.get_mut(&user_id).unwrap().retain(|x|(*x).task_id!=task_id);}}