Skip to content

Commit a1ecdaf

Browse files
author
build
committed
feat:exercises
1 parent 52ac41f commit a1ecdaf

File tree

2 files changed

+186
-142
lines changed

2 files changed

+186
-142
lines changed

exercises/algorithm/algorithm7.rs

Lines changed: 139 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,163 @@
11
/*
2-
stack
3-
This question requires you to use a stack to achieve a bracket match
2+
stack
3+
This question requires you to use a stack to achieve a bracket match
44
*/
55

6-
// I AM NOT DONE
76
#[derive(Debug)]
87
struct Stack<T> {
9-
size: usize,
10-
data: Vec<T>,
8+
size: usize,
9+
data: Vec<T>,
1110
}
1211
impl<T> Stack<T> {
13-
fn new() -> Self {
14-
Self {
15-
size: 0,
16-
data: Vec::new(),
17-
}
18-
}
19-
fn is_empty(&self) -> bool {
20-
0 == self.size
21-
}
22-
fn len(&self) -> usize {
23-
self.size
24-
}
25-
fn clear(&mut self) {
26-
self.size = 0;
27-
self.data.clear();
28-
}
29-
fn push(&mut self, val: T) {
30-
self.data.push(val);
31-
self.size += 1;
32-
}
33-
fn pop(&mut self) -> Option<T> {
34-
// TODO
35-
None
36-
}
37-
fn peek(&self) -> Option<&T> {
38-
if 0 == self.size {
39-
return None;
40-
}
41-
self.data.get(self.size - 1)
42-
}
43-
fn peek_mut(&mut self) -> Option<&mut T> {
44-
if 0 == self.size {
45-
return None;
46-
}
47-
self.data.get_mut(self.size - 1)
48-
}
49-
fn into_iter(self) -> IntoIter<T> {
50-
IntoIter(self)
51-
}
52-
fn iter(&self) -> Iter<T> {
53-
let mut iterator = Iter {
54-
stack: Vec::new()
55-
};
56-
for item in self.data.iter() {
57-
iterator.stack.push(item);
58-
}
59-
iterator
60-
}
61-
fn iter_mut(&mut self) -> IterMut<T> {
62-
let mut iterator = IterMut {
63-
stack: Vec::new()
64-
};
65-
for item in self.data.iter_mut() {
66-
iterator.stack.push(item);
67-
}
68-
iterator
69-
}
12+
fn new() -> Self {
13+
Stack {
14+
size: 0,
15+
data: Vec::new(),
16+
}
17+
}
18+
fn is_empty(&self) -> bool {
19+
0 == self.size
20+
}
21+
fn len(&self) -> usize {
22+
self.size
23+
}
24+
fn clear(&mut self) {
25+
self.size = 0;
26+
self.data.clear();
27+
}
28+
fn push(&mut self, val: T) {
29+
self.data.push(val);
30+
self.size += 1;
31+
}
32+
fn pop(&mut self) -> Option<T> {
33+
if self.is_empty() {
34+
return None;
35+
}
36+
37+
self.size -= 1;
38+
self.data.pop()
39+
}
40+
fn peek(&self) -> Option<&T> {
41+
if self.is_empty() {
42+
return None;
43+
}
44+
45+
self.data.last()
46+
}
47+
fn peek_mut(&mut self) -> Option<&mut T> {
48+
if self.is_empty() {
49+
return None;
50+
}
51+
52+
self.data.last_mut()
53+
}
54+
fn into_iter(self) -> IntoIter<T> {
55+
if self.is_empty() {
56+
IntoIter(Stack::new())
57+
} else {
58+
IntoIter(self)
59+
}
60+
}
61+
fn iter(&self) -> Iter<T> {
62+
Iter {
63+
stack: self.data.iter().collect(),
64+
}
65+
}
66+
fn iter_mut(&mut self) -> IterMut<T> {
67+
IterMut {
68+
stack: self.data.iter_mut().collect(),
69+
}
70+
}
7071
}
7172
struct IntoIter<T>(Stack<T>);
7273
impl<T: Clone> Iterator for IntoIter<T> {
73-
type Item = T;
74-
fn next(&mut self) -> Option<Self::Item> {
75-
if !self.0.is_empty() {
76-
self.0.size -= 1;self.0.data.pop()
77-
}
78-
else {
79-
None
80-
}
81-
}
74+
type Item = T;
75+
fn next(&mut self) -> Option<Self::Item> {
76+
if !self.0.is_empty() {
77+
self.0.size -= 1;
78+
self.0.data.pop()
79+
} else {
80+
None
81+
}
82+
}
8283
}
8384
struct Iter<'a, T: 'a> {
84-
stack: Vec<&'a T>,
85+
stack: Vec<&'a T>,
8586
}
8687
impl<'a, T> Iterator for Iter<'a, T> {
87-
type Item = &'a T;
88-
fn next(&mut self) -> Option<Self::Item> {
89-
self.stack.pop()
90-
}
88+
type Item = &'a T;
89+
fn next(&mut self) -> Option<Self::Item> {
90+
self.stack.pop()
91+
}
9192
}
9293
struct IterMut<'a, T: 'a> {
93-
stack: Vec<&'a mut T>,
94+
stack: Vec<&'a mut T>,
9495
}
9596
impl<'a, T> Iterator for IterMut<'a, T> {
96-
type Item = &'a mut T;
97-
fn next(&mut self) -> Option<Self::Item> {
98-
self.stack.pop()
99-
}
97+
type Item = &'a mut T;
98+
fn next(&mut self) -> Option<Self::Item> {
99+
self.stack.pop()
100+
}
100101
}
101102

102-
fn bracket_match(bracket: &str) -> bool
103-
{
104-
//TODO
105-
true
103+
fn bracket_match(bracket: &str) -> bool {
104+
//TODO
105+
let mut stack = Stack::new();
106+
for c in bracket.chars() {
107+
match c {
108+
'(' | '{' | '[' => stack.push(c),
109+
')' | '}' | ']' => {
110+
if stack.is_empty() {
111+
return false;
112+
}
113+
let top = stack.pop().unwrap();
114+
let bra = (top, c);
115+
match bra {
116+
('(', ')') => {}
117+
('{', '}') => {}
118+
('[', ']') => {},
119+
_ => return false,
120+
}
121+
}
122+
_ => {}
123+
}
124+
}
125+
126+
stack.is_empty()
106127
}
107128

108129
#[cfg(test)]
109130
mod tests {
110-
use super::*;
111-
112-
#[test]
113-
fn bracket_matching_1(){
114-
let s = "(2+3){func}[abc]";
115-
assert_eq!(bracket_match(s),true);
116-
}
117-
#[test]
118-
fn bracket_matching_2(){
119-
let s = "(2+3)*(3-1";
120-
assert_eq!(bracket_match(s),false);
121-
}
122-
#[test]
123-
fn bracket_matching_3(){
124-
let s = "{{([])}}";
125-
assert_eq!(bracket_match(s),true);
126-
}
127-
#[test]
128-
fn bracket_matching_4(){
129-
let s = "{{(}[)]}";
130-
assert_eq!(bracket_match(s),false);
131-
}
132-
#[test]
133-
fn bracket_matching_5(){
134-
let s = "[[[]]]]]]]]]";
135-
assert_eq!(bracket_match(s),false);
136-
}
137-
#[test]
138-
fn bracket_matching_6(){
139-
let s = "";
140-
assert_eq!(bracket_match(s),true);
141-
}
142-
}
131+
use super::*;
132+
133+
#[test]
134+
fn bracket_matching_1() {
135+
let s = "(2+3){func}[abc]";
136+
assert_eq!(bracket_match(s), true);
137+
}
138+
#[test]
139+
fn bracket_matching_2() {
140+
let s = "(2+3)*(3-1";
141+
assert_eq!(bracket_match(s), false);
142+
}
143+
#[test]
144+
fn bracket_matching_3() {
145+
let s = "{{([])}}";
146+
assert_eq!(bracket_match(s), true);
147+
}
148+
#[test]
149+
fn bracket_matching_4() {
150+
let s = "{{(}[)]}";
151+
assert_eq!(bracket_match(s), false);
152+
}
153+
#[test]
154+
fn bracket_matching_5() {
155+
let s = "[[[]]]]]]]]]";
156+
assert_eq!(bracket_match(s), false);
157+
}
158+
#[test]
159+
fn bracket_matching_6() {
160+
let s = "";
161+
assert_eq!(bracket_match(s), true);
162+
}
163+
}

exercises/algorithm/algorithm8.rs

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/*
2-
queue
3-
This question requires you to use queues to implement the functionality of the stac
2+
queue
3+
This question requires you to use queues to implement the functionality of the stac
44
*/
5-
// I AM NOT DONE
65

76
#[derive(Debug)]
87
pub struct Queue<T> {
@@ -52,41 +51,65 @@ impl<T> Default for Queue<T> {
5251
}
5352
}
5453

55-
pub struct myStack<T>
56-
{
57-
//TODO
58-
q1:Queue<T>,
59-
q2:Queue<T>
54+
pub struct myStack<T> {
55+
//TODO
56+
q1: Queue<T>,
57+
q2: Queue<T>,
58+
size: usize,
59+
use_q1_as_main: bool,
6060
}
6161
impl<T> myStack<T> {
6262
pub fn new() -> Self {
6363
Self {
64-
//TODO
65-
q1:Queue::<T>::new(),
66-
q2:Queue::<T>::new()
64+
//TODO
65+
q1: Queue::<T>::new(),
66+
q2: Queue::<T>::new(),
67+
size: 0,
68+
use_q1_as_main: true,
6769
}
6870
}
6971
pub fn push(&mut self, elem: T) {
70-
//TODO
72+
if self.use_q1_as_main {
73+
self.q1.enqueue(elem);
74+
} else {
75+
self.q2.enqueue(elem);
76+
}
77+
self.size += 1;
7178
}
7279
pub fn pop(&mut self) -> Result<T, &str> {
73-
//TODO
74-
Err("Stack is empty")
80+
if self.is_empty() {
81+
return Err("Stack is empty");
82+
}
83+
84+
if self.use_q1_as_main {
85+
while self.q1.size() > 1 {
86+
self.q2.enqueue(self.q1.dequeue().unwrap());
87+
}
88+
self.use_q1_as_main = false;
89+
self.size -= 1;
90+
self.q1.dequeue()
91+
} else {
92+
while self.q2.size() > 1 {
93+
self.q1.enqueue(self.q2.dequeue().unwrap());
94+
}
95+
self.use_q1_as_main = true;
96+
self.size -= 1;
97+
self.q2.dequeue()
98+
}
7599
}
76100
pub fn is_empty(&self) -> bool {
77-
//TODO
78-
true
101+
self.size == 0
79102
}
80103
}
81104

82105
#[cfg(test)]
83106
mod tests {
84-
use super::*;
85-
86-
#[test]
87-
fn test_queue(){
88-
let mut s = myStack::<i32>::new();
89-
assert_eq!(s.pop(), Err("Stack is empty"));
107+
use super::*;
108+
109+
#[test]
110+
fn test_queue() {
111+
let mut s = myStack::<i32>::new();
112+
assert_eq!(s.pop(), Err("Stack is empty"));
90113
s.push(1);
91114
s.push(2);
92115
s.push(3);
@@ -100,5 +123,5 @@ mod tests {
100123
assert_eq!(s.pop(), Ok(1));
101124
assert_eq!(s.pop(), Err("Stack is empty"));
102125
assert_eq!(s.is_empty(), true);
103-
}
104-
}
126+
}
127+
}

0 commit comments

Comments
 (0)