Skip to content

Commit 7e7a63e

Browse files
committed
feat: more examples and exercises from chapter 9
1 parent b8461aa commit 7e7a63e

6 files changed

Lines changed: 128 additions & 0 deletions

File tree

chapter-9/01_stack.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Stack
2+
def initialize
3+
@data = []
4+
end
5+
6+
def push(element)
7+
@data << (element)
8+
end
9+
10+
def pop
11+
@data.pop
12+
end
13+
14+
def read
15+
@data.last
16+
end
17+
end

chapter-9/01_stack.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export interface IArrayLike<T> {
2+
push(element: T): void;
3+
pop(): T | undefined;
4+
read(): T | undefined;
5+
}
6+
7+
export class Stack<T> implements IArrayLike<T> {
8+
private data: T[] = [];
9+
10+
push(element: T): void {
11+
this.data.push(element);
12+
}
13+
14+
pop(): T | undefined {
15+
return this.data.pop();
16+
}
17+
18+
read(): T | undefined {
19+
return this.data[this.data.length - 1];
20+
}
21+
}

chapter-9/02_linter.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require_relative '01_stack'
2+
3+
class Linter
4+
def initialize
5+
@stack = Stack.new
6+
end
7+
8+
def lint(text)
9+
text.each_char do |char|
10+
if is_opening_brace?(char)
11+
@stack.push(char)
12+
elsif is_closing_brace?(char)
13+
popped_opening_brace = @stack.pop
14+
if !popped_opening_brace
15+
return "#{char} doesn't have a matching opening brace"
16+
end
17+
if is_not_a_match(popped_opening_brace, char)
18+
return "#{char} has mismatched opening brace"
19+
end
20+
end
21+
end
22+
if @stack.read
23+
return "#{@stack.read} doesn't have a matching closing brace"
24+
end
25+
return true
26+
end
27+
28+
private
29+
30+
def is_opening_brace?(char)
31+
['(', '[', '{'].include?(char)
32+
end
33+
34+
def is_closing_brace?(char)
35+
[')', ']', '}'].include?(char)
36+
end
37+
38+
def is_not_a_match(opening_brace, closing_brace)
39+
closing_brace != {"(" => ")", "[" => "]", "{" => "}" }[opening_brace]
40+
end
41+
end

chapter-9/03_queue.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Queue
2+
def initialize
3+
@data = []
4+
end
5+
6+
def enqueue(el)
7+
@data << el
8+
end
9+
10+
def dequeue
11+
@data.shift
12+
end
13+
14+
def read
15+
@data.first
16+
end
17+
end

chapter-9/03_queue.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { IArrayLike } from "./01_stack";
2+
3+
export class Queue<T> implements IArrayLike<T> {
4+
5+
private data: T[] = []
6+
7+
push(el: T) { // enqueue
8+
this.data.push(el)
9+
}
10+
11+
pop() { // dequeue
12+
return this.data.shift()
13+
}
14+
15+
read() {
16+
return this.data[0]
17+
}
18+
19+
}

chapter-9/ex4.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Stack } from "./01_stack";
2+
3+
function reverseString(str: string): string {
4+
const stack = new Stack<string>();
5+
for (const char of str) {
6+
stack.push(char);
7+
}
8+
let reversed = '';
9+
for (let i = 0; i < str.length; i++) {
10+
reversed += stack.pop()
11+
}
12+
return reversed;
13+
}

0 commit comments

Comments
 (0)