Skip to content

Commit ebe56f7

Browse files
committed
update
1 parent c0e0999 commit ebe56f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+789
-627
lines changed

Cargo.lock

Lines changed: 215 additions & 302 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ edition = "2021"
1111
[dependencies]
1212
argh = "0.1"
1313
indicatif = "0.16"
14-
console = "0.15"
14+
console = { version = "0.16", features = ["std"] }
1515
notify = "4.0"
1616
toml = "0.5"
1717
regex = "1.5"

exercises/algorithm/algorithm1.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
single linked list merge
33
This problem requires you to merge two ordered singly linked lists into one ordered singly linked list
44
*/
5-
// I AM NOT DONE
65

76
use std::fmt::{self, Display, Formatter};
87
use std::ptr::NonNull;
@@ -69,15 +68,39 @@ impl<T> LinkedList<T> {
6968
},
7069
}
7170
}
72-
pub fn merge(list_a:LinkedList<T>,list_b:LinkedList<T>) -> Self
73-
{
74-
//TODO
75-
Self {
76-
length: 0,
77-
start: None,
78-
end: None,
71+
}
72+
73+
impl<T: PartialOrd + Clone> LinkedList<T> {
74+
pub fn merge(mut list_a: LinkedList<T>, mut list_b:LinkedList<T>) -> Self
75+
{
76+
let mut list = LinkedList::new();
77+
let la = list_a.length;
78+
let lb = list_b.length;
79+
let mut pa = 0;
80+
let mut pb = 0;
81+
loop {
82+
if pa < la && pb < lb {
83+
let va = list_a.get(pa as i32).unwrap().clone();
84+
let vb = list_b.get(pb as i32).unwrap().clone();
85+
if va < vb {
86+
pa += 1;
87+
list.add(va);
88+
} else {
89+
pb += 1;
90+
list.add(vb);
91+
}
92+
} else if pa < la {
93+
list.add(list_a.get(pa as i32).unwrap().clone());
94+
pa += 1;
95+
} else if pb < lb {
96+
list.add(list_b.get(pb as i32).unwrap().clone());
97+
pb += 1;
98+
} else {
99+
break;
100+
}
79101
}
80-
}
102+
list
103+
}
81104
}
82105

83106
impl<T> Display for LinkedList<T>

exercises/algorithm/algorithm10.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
graph
33
This problem requires you to implement a basic graph functio
44
*/
5-
// I AM NOT DONE
65

76
use std::collections::{HashMap, HashSet};
87
use std::fmt;
@@ -29,16 +28,27 @@ impl Graph for UndirectedGraph {
2928
&self.adjacency_table
3029
}
3130
fn add_edge(&mut self, edge: (&str, &str, i32)) {
32-
//TODO
31+
self.add_node(edge.0);
32+
self.add_node(edge.1);
33+
if let Some(adj) = self.adjacency_table_mutable().get_mut(edge.0) {
34+
adj.push((edge.1.to_string(), edge.2));
35+
}
36+
if let Some(adj) = self.adjacency_table_mutable().get_mut(edge.1) {
37+
adj.push((edge.0.to_string(), edge.2));
38+
}
3339
}
3440
}
3541
pub trait Graph {
3642
fn new() -> Self;
3743
fn adjacency_table_mutable(&mut self) -> &mut HashMap<String, Vec<(String, i32)>>;
3844
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
3945
fn add_node(&mut self, node: &str) -> bool {
40-
//TODO
41-
true
46+
if self.contains(node) {
47+
false
48+
} else {
49+
self.adjacency_table_mutable().insert(node.to_string(), Vec::new());
50+
true
51+
}
4252
}
4353
fn add_edge(&mut self, edge: (&str, &str, i32)) {
4454
//TODO

exercises/algorithm/algorithm2.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
double linked list reverse
33
This problem requires you to reverse a doubly linked list
44
*/
5-
// I AM NOT DONE
65

76
use std::fmt::{self, Display, Formatter};
87
use std::ptr::NonNull;
@@ -72,8 +71,40 @@ impl<T> LinkedList<T> {
7271
},
7372
}
7473
}
75-
pub fn reverse(&mut self){
76-
// TODO
74+
pub fn reverse(&mut self){
75+
if self.length <= 1 {
76+
return;
77+
}
78+
79+
let mut v = vec![None];
80+
let mut cur = self.start;
81+
82+
loop {
83+
match cur {
84+
Some(node) => {
85+
v.push(Some(node));
86+
unsafe {
87+
cur = (*node.as_ptr()).next;
88+
}
89+
},
90+
None => {
91+
v.push(None);
92+
break;
93+
},
94+
}
95+
}
96+
97+
for idx in 0..v.len() {
98+
if let Some(node) = v[idx] {
99+
unsafe {
100+
(*node.as_ptr()).next = v[idx - 1];
101+
(*node.as_ptr()).prev = v[idx + 1];
102+
}
103+
}
104+
}
105+
106+
self.end = v[1];
107+
self.start = v[v.len() - 2];
77108
}
78109
}
79110

exercises/algorithm/algorithm3.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@
33
This problem requires you to implement a sorting algorithm
44
you can use bubble sorting, insertion sorting, heap sorting, etc.
55
*/
6-
// I AM NOT DONE
76

8-
fn sort<T>(array: &mut [T]){
9-
//TODO
7+
use std::mem::swap;
8+
9+
fn sort<T: PartialOrd + Clone>(array: &mut [T]){
10+
let n = array.len();
11+
for i in 1..=n {
12+
for j in 0..n - i {
13+
if array[j] > array[j + 1] {
14+
let tmp = array[j].clone();
15+
array[j] = array[j + 1].clone();
16+
array[j + 1] = tmp.clone();
17+
}
18+
}
19+
}
1020
}
1121
#[cfg(test)]
1222
mod tests {

exercises/algorithm/algorithm4.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
binary_search tree
33
This problem requires you to implement a basic interface for a binary tree
44
*/
5-
6-
//I AM NOT DONE
75
use std::cmp::Ordering;
86
use std::fmt::Debug;
97

@@ -50,13 +48,32 @@ where
5048

5149
// Insert a value into the BST
5250
fn insert(&mut self, value: T) {
53-
//TODO
51+
match &mut self.root {
52+
Some(root) => root.insert(value),
53+
None => self.root = Some(Box::new(TreeNode::new(value))),
54+
}
5455
}
5556

5657
// Search for a value in the BST
5758
fn search(&self, value: T) -> bool {
5859
//TODO
59-
true
60+
let mut cur = &self.root;
61+
62+
loop {
63+
match cur {
64+
Some(node) => {
65+
if node.value < value {
66+
cur = &node.left;
67+
} else if node.value > value {
68+
cur = &node.right;
69+
} else {
70+
return true;
71+
}
72+
},
73+
None => break,
74+
}
75+
}
76+
false
6077
}
6178
}
6279

@@ -66,7 +83,17 @@ where
6683
{
6784
// Insert a node into the tree
6885
fn insert(&mut self, value: T) {
69-
//TODO
86+
if self.value < value {
87+
match &mut self.left {
88+
Some(node) => node.insert(value),
89+
None => self.left = Some(Box::new(TreeNode::new(value))),
90+
}
91+
} else if self.value > value {
92+
match &mut self.right {
93+
Some(node) => node.insert(value),
94+
None => self.right = Some(Box::new(TreeNode::new(value))),
95+
}
96+
}
7097
}
7198
}
7299

exercises/algorithm/algorithm5.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
bfs
33
This problem requires you to implement a basic BFS algorithm
44
*/
5-
6-
//I AM NOT DONE
75
use std::collections::VecDeque;
86

97
// Define a graph
@@ -27,10 +25,24 @@ impl Graph {
2725

2826
// Perform a breadth-first search on the graph, return the order of visited nodes
2927
fn bfs_with_return(&self, start: usize) -> Vec<usize> {
30-
31-
//TODO
32-
3328
let mut visit_order = vec![];
29+
let mut vis = vec![false; self.adj.len()];
30+
31+
let mut q = VecDeque::new();
32+
q.push_back(start);
33+
vis[start] = true;
34+
35+
while !q.is_empty() {
36+
let cur = q.pop_front().unwrap();
37+
visit_order.push(cur);
38+
for to in &self.adj[cur] {
39+
if !vis[*to] {
40+
vis[*to] = true;
41+
q.push_back(*to);
42+
}
43+
}
44+
}
45+
3446
visit_order
3547
}
3648
}

exercises/algorithm/algorithm6.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
dfs
33
This problem requires you to implement a basic DFS traversal
44
*/
5-
6-
// I AM NOT DONE
75
use std::collections::HashSet;
86

97
struct Graph {
@@ -23,7 +21,16 @@ impl Graph {
2321
}
2422

2523
fn dfs_util(&self, v: usize, visited: &mut HashSet<usize>, visit_order: &mut Vec<usize>) {
26-
//TODO
24+
if visited.contains(&v) {
25+
return;
26+
} else {
27+
visited.insert(v);
28+
visit_order.push(v);
29+
}
30+
31+
for to in &self.adj[v] {
32+
self.dfs_util(*to, visited, visit_order);
33+
}
2734
}
2835

2936
// Perform a depth-first search on the graph, return the order of visited nodes

exercises/algorithm/algorithm7.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
stack
33
This question requires you to use a stack to achieve a bracket match
44
*/
5-
6-
// I AM NOT DONE
75
#[derive(Debug)]
86
struct Stack<T> {
97
size: usize,
@@ -31,8 +29,12 @@ impl<T> Stack<T> {
3129
self.size += 1;
3230
}
3331
fn pop(&mut self) -> Option<T> {
34-
// TODO
35-
None
32+
if self.size > 0 {
33+
self.size -= 1;
34+
self.data.pop()
35+
} else {
36+
None
37+
}
3638
}
3739
fn peek(&self) -> Option<&T> {
3840
if 0 == self.size {
@@ -101,8 +103,37 @@ impl<'a, T> Iterator for IterMut<'a, T> {
101103

102104
fn bracket_match(bracket: &str) -> bool
103105
{
104-
//TODO
105-
true
106+
let mut stk = Stack::new();
107+
let mut bracket = bracket.chars();
108+
109+
while let Some(ch) = bracket.next() {
110+
match ch {
111+
'(' => stk.push('('),
112+
'[' => stk.push('['),
113+
'{' => stk.push('{'),
114+
')' => {
115+
match stk.pop() {
116+
Some(ch) if ch == '(' => continue,
117+
_ => return false,
118+
}
119+
},
120+
']' => {
121+
match stk.pop() {
122+
Some(ch) if ch == '[' => continue,
123+
_ => return false,
124+
}
125+
},
126+
'}' => {
127+
match stk.pop() {
128+
Some(ch) if ch == '{' => continue,
129+
_ => return false,
130+
}
131+
},
132+
_ => continue,
133+
}
134+
}
135+
136+
stk.is_empty()
106137
}
107138

108139
#[cfg(test)]

0 commit comments

Comments
 (0)