Skip to content

feat: add rust solution to lc problem: No.2590 #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions solution/2500-2599/2590.Design a Todo List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,105 @@ class TodoList {
*/
```

### **Rust**

```rust
use std::collections::{HashMap, HashSet};

#[derive(Clone)]
struct Task {
task_id: i32,
description: String,
tags: HashSet<String>,
due_date: i32,
}

struct TodoList {
/// The global task id
id: i32,
/// The mapping from `user_id` to `task`
user_map: HashMap<i32, Vec<Task>>,
}

impl TodoList {
fn new() -> Self {
Self {
id: 1,
user_map: HashMap::new(),
}
}

fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec<String>) -> i32 {
if self.user_map.contains_key(&user_id) {
// Just add the task
self.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 id
self.id += 1;
return self.id - 1;
}
// Otherwise, create a new user
self.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
}

fn get_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() {
return vec![];
}
// Get the task vector
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
// Sort by due date
ret_vec.sort_by(|lhs, rhs| {
lhs.due_date.cmp(&rhs.due_date)
});
// Return the description vector
ret_vec
.into_iter()
.map(|x| x.description)
.collect()
}

fn get_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() {
return vec![];
}
// Get the task vector
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
// Sort by due date
ret_vec.sort_by(|lhs, rhs| {
lhs.due_date.cmp(&rhs.due_date)
});
// Return the description vector
ret_vec
.into_iter()
.filter(|x| x.tags.contains(&tag))
.map(|x| x.description)
.collect()
}

fn complete_task(&mut self, 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);
}
}
```

### **C++**

```cpp
Expand Down
99 changes: 99 additions & 0 deletions solution/2500-2599/2590.Design a Todo List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,105 @@ class TodoList {
*/
```

### **Rust**

```rust
use std::collections::{HashMap, HashSet};

#[derive(Clone)]
struct Task {
task_id: i32,
description: String,
tags: HashSet<String>,
due_date: i32,
}

struct TodoList {
/// The global task id
id: i32,
/// The mapping from `user_id` to `task`
user_map: HashMap<i32, Vec<Task>>,
}

impl TodoList {
fn new() -> Self {
Self {
id: 1,
user_map: HashMap::new(),
}
}

fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec<String>) -> i32 {
if self.user_map.contains_key(&user_id) {
// Just add the task
self.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 id
self.id += 1;
return self.id - 1;
}
// Otherwise, create a new user
self.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
}

fn get_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() {
return vec![];
}
// Get the task vector
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
// Sort by due date
ret_vec.sort_by(|lhs, rhs| {
lhs.due_date.cmp(&rhs.due_date)
});
// Return the description vector
ret_vec
.into_iter()
.map(|x| x.description)
.collect()
}

fn get_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() {
return vec![];
}
// Get the task vector
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
// Sort by due date
ret_vec.sort_by(|lhs, rhs| {
lhs.due_date.cmp(&rhs.due_date)
});
// Return the description vector
ret_vec
.into_iter()
.filter(|x| x.tags.contains(&tag))
.map(|x| x.description)
.collect()
}

fn complete_task(&mut self, 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);
}
}
```

### **C++**

```cpp
Expand Down
94 changes: 94 additions & 0 deletions solution/2500-2599/2590.Design a Todo List/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::collections::{HashMap, HashSet};

#[derive(Clone)]
struct Task {
task_id: i32,
description: String,
tags: HashSet<String>,
due_date: i32,
}

struct TodoList {
/// The global task id
id: i32,
/// The mapping from `user_id` to `task`
user_map: HashMap<i32, Vec<Task>>,
}

impl TodoList {
fn new() -> Self {
Self {
id: 1,
user_map: HashMap::new(),
}
}

fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec<String>) -> i32 {
if self.user_map.contains_key(&user_id) {
// Just add the task
self.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 id
self.id += 1;
return self.id - 1;
}
// Otherwise, create a new user
self.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
}

fn get_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() {
return vec![];
}
// Get the task vector
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
// Sort by due date
ret_vec.sort_by(|lhs, rhs| {
lhs.due_date.cmp(&rhs.due_date)
});
// Return the description vector
ret_vec
.into_iter()
.map(|x| x.description)
.collect()
}

fn get_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() {
return vec![];
}
// Get the task vector
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
// Sort by due date
ret_vec.sort_by(|lhs, rhs| {
lhs.due_date.cmp(&rhs.due_date)
});
// Return the description vector
ret_vec
.into_iter()
.filter(|x| x.tags.contains(&tag))
.map(|x| x.description)
.collect()
}

fn complete_task(&mut self, 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);
}
}