feat: working through all the algos i'll need
This commit is contained in:
parent
7101b99c35
commit
6f41e5760d
5 changed files with 264 additions and 27 deletions
|
|
@ -1,6 +1,55 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
dsa: [
|
dsa: [
|
||||||
|
"LinearSearchList",
|
||||||
|
"BinarySearchList",
|
||||||
|
"TwoCrystalBalls",
|
||||||
|
"BubbleSort",
|
||||||
|
"LinkedList",
|
||||||
"ArrayList",
|
"ArrayList",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Lists
|
||||||
|
LinkedList
|
||||||
|
* get length(): number;
|
||||||
|
* remove(item: T): T | undefined;
|
||||||
|
* removeAt(index: number): T | undefined;
|
||||||
|
* append(item: T): void;
|
||||||
|
* prepend(item: T): void;
|
||||||
|
* get(index: number): T | undefined;
|
||||||
|
|
||||||
|
Queue
|
||||||
|
Stack
|
||||||
|
|
||||||
|
ArrayList
|
||||||
|
* Same api as list
|
||||||
|
|
||||||
|
## Recursion
|
||||||
|
The Maze Problem
|
||||||
|
Quicksort
|
||||||
|
|
||||||
|
## Trees
|
||||||
|
Binary Tree Traversals
|
||||||
|
DFS: pre
|
||||||
|
DFS: in
|
||||||
|
DFS: post
|
||||||
|
|
||||||
|
BFS on Binary
|
||||||
|
|
||||||
|
### Practice Problem
|
||||||
|
- Compare Two Binary Trees
|
||||||
|
|
||||||
|
DFS: Binary Search Tree Traversal
|
||||||
|
|
||||||
|
RedBlack Trees
|
||||||
|
|
||||||
|
## Graphs
|
||||||
|
DFS
|
||||||
|
BFS
|
||||||
|
Dijkstra
|
||||||
|
|
||||||
|
## Map
|
||||||
|
Implement Map
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
221
scripts/dsa.js
221
scripts/dsa.js
|
|
@ -1,5 +1,66 @@
|
||||||
|
const length_property = {
|
||||||
|
getters: [{
|
||||||
|
name: "length",
|
||||||
|
return: "number",
|
||||||
|
prop_name: "_length",
|
||||||
|
}],
|
||||||
|
properties: [{
|
||||||
|
name: "_length",
|
||||||
|
type: "number",
|
||||||
|
scope: "private",
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
const list_interface = {
|
||||||
|
methods: [{
|
||||||
|
name: "prepend",
|
||||||
|
args: "item: T",
|
||||||
|
return: "void",
|
||||||
|
}, {
|
||||||
|
name: "insertAt",
|
||||||
|
args: "item: T, idx: number",
|
||||||
|
return: "void",
|
||||||
|
}, {
|
||||||
|
name: "append",
|
||||||
|
args: "item: T",
|
||||||
|
return: "void",
|
||||||
|
}, {
|
||||||
|
name: "remove",
|
||||||
|
args: "item: T",
|
||||||
|
return: "T | undefined",
|
||||||
|
}, {
|
||||||
|
name: "get",
|
||||||
|
args: "idx: number",
|
||||||
|
return: "T | undefined",
|
||||||
|
}, {
|
||||||
|
name: "removeAt",
|
||||||
|
args: "idx: number",
|
||||||
|
return: "T | undefined",
|
||||||
|
}],
|
||||||
|
...length_property,
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
Map: {
|
||||||
|
generic: "<T, V>",
|
||||||
|
type: "class",
|
||||||
|
methods: [{
|
||||||
|
name: "get",
|
||||||
|
args: "key: T",
|
||||||
|
return: "V | undefined",
|
||||||
|
}, {
|
||||||
|
name: "set",
|
||||||
|
args: "key: T, value: V",
|
||||||
|
return: "void",
|
||||||
|
}, {
|
||||||
|
name: "delete",
|
||||||
|
args: "key: T",
|
||||||
|
return: "V | undefined",
|
||||||
|
}, {
|
||||||
|
name: "size",
|
||||||
|
return: "number",
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
|
||||||
RingBuffer: {
|
RingBuffer: {
|
||||||
generic: "<T>",
|
generic: "<T>",
|
||||||
type: "class",
|
type: "class",
|
||||||
|
|
@ -26,51 +87,64 @@ module.exports = {
|
||||||
scope: "private",
|
scope: "private",
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
|
|
||||||
ArrayList: {
|
ArrayList: {
|
||||||
type: "class",
|
type: "class",
|
||||||
generic: "<T>",
|
generic: "<T>",
|
||||||
methods: [{
|
...list_interface,
|
||||||
name: "add",
|
|
||||||
args: "item: T",
|
|
||||||
return: "void",
|
|
||||||
}, {
|
|
||||||
name: "peek",
|
|
||||||
return: "T | undefined",
|
|
||||||
}, {
|
|
||||||
name: "remove",
|
|
||||||
args: "item: T",
|
|
||||||
return: "T | undefined",
|
|
||||||
}, {
|
|
||||||
name: "removeAt",
|
|
||||||
args: "idx: number",
|
|
||||||
return: "T | undefined",
|
|
||||||
}],
|
|
||||||
getters: [{
|
|
||||||
name: "length",
|
|
||||||
return: "number",
|
|
||||||
prop_name: "_length",
|
|
||||||
}],
|
|
||||||
properties: [{
|
|
||||||
name: "_length",
|
|
||||||
type: "number",
|
|
||||||
scope: "private",
|
|
||||||
}]
|
|
||||||
},
|
},
|
||||||
SinglyLinkedList: {
|
SinglyLinkedList: {
|
||||||
generic: "<T>",
|
generic: "<T>",
|
||||||
type: "class",
|
type: "class",
|
||||||
|
...list_interface,
|
||||||
},
|
},
|
||||||
DoublyLinkedList: {
|
DoublyLinkedList: {
|
||||||
generic: "<T>",
|
generic: "<T>",
|
||||||
type: "class",
|
type: "class",
|
||||||
|
...list_interface,
|
||||||
},
|
},
|
||||||
Queue: {
|
Queue: {
|
||||||
generic: "<T>",
|
generic: "<T>",
|
||||||
type: "class",
|
type: "class",
|
||||||
|
...length_property,
|
||||||
|
methods: [{
|
||||||
|
name: "enqueue",
|
||||||
|
args: "item: T",
|
||||||
|
return: "void",
|
||||||
|
}, {
|
||||||
|
name: "deque",
|
||||||
|
args: "",
|
||||||
|
return: "T | undefined",
|
||||||
|
}, {
|
||||||
|
name: "peek",
|
||||||
|
args: "",
|
||||||
|
return: "T | undefined",
|
||||||
|
}]
|
||||||
},
|
},
|
||||||
Stack: {
|
Stack: {
|
||||||
generic: "<T>",
|
generic: "<T>",
|
||||||
type: "class",
|
type: "class",
|
||||||
|
...length_property,
|
||||||
|
methods: [{
|
||||||
|
name: "push",
|
||||||
|
args: "item: T",
|
||||||
|
return: "void",
|
||||||
|
}, {
|
||||||
|
name: "pop",
|
||||||
|
args: "",
|
||||||
|
return: "T | undefined",
|
||||||
|
}, {
|
||||||
|
name: "peek",
|
||||||
|
args: "",
|
||||||
|
return: "T | undefined",
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
|
||||||
|
BubbleSort: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "bubble_sort",
|
||||||
|
args: "arr: number[]",
|
||||||
|
"return": "void",
|
||||||
},
|
},
|
||||||
|
|
||||||
InsertionSort: {
|
InsertionSort: {
|
||||||
|
|
@ -107,5 +181,100 @@ module.exports = {
|
||||||
args: "list: WeightedAdjacencyList",
|
args: "list: WeightedAdjacencyList",
|
||||||
"return": ": WeightedAdjacencyList | null",
|
"return": ": WeightedAdjacencyList | null",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
BinarySearchList: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "bs_list",
|
||||||
|
args: "haystack: number[], needle: number",
|
||||||
|
"return": ": boolean",
|
||||||
|
},
|
||||||
|
|
||||||
|
LinearSearchList: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "linear_search",
|
||||||
|
args: "haystack: number[], needle: number",
|
||||||
|
"return": ": boolean",
|
||||||
|
},
|
||||||
|
|
||||||
|
TwoCrystalBalls: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "two_crystal_balls",
|
||||||
|
args: "breaks: boolean[]",
|
||||||
|
"return": ": number",
|
||||||
|
},
|
||||||
|
|
||||||
|
MazeSolver: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "solve",
|
||||||
|
args: "maze: string[][], wall: string, path: string, start: Point, end: Point",
|
||||||
|
"return": ": Point[]",
|
||||||
|
},
|
||||||
|
|
||||||
|
BTPreOrder: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "pre_order_search",
|
||||||
|
generic: "<T>",
|
||||||
|
args: "head: BinaryNode<T>",
|
||||||
|
"return": ": BinaryNode<T>[]",
|
||||||
|
},
|
||||||
|
|
||||||
|
BTInOrder: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "in_order_search",
|
||||||
|
generic: "<T>",
|
||||||
|
args: "head: BinaryNode<T>",
|
||||||
|
"return": ": BinaryNode<T>[]",
|
||||||
|
},
|
||||||
|
|
||||||
|
BTPostOrder: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "post_order_search",
|
||||||
|
generic: "<T>",
|
||||||
|
args: "head: BinaryNode<T>",
|
||||||
|
"return": ": BinaryNode<T>[]",
|
||||||
|
},
|
||||||
|
|
||||||
|
BTBFS: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "bfs",
|
||||||
|
generic: "<T>",
|
||||||
|
args: "head: BinaryNode<T>",
|
||||||
|
"return": ": BinaryNode<T>[]",
|
||||||
|
},
|
||||||
|
|
||||||
|
CompareBinaryTrees: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "compare",
|
||||||
|
args: "head: BinaryNode<number>",
|
||||||
|
"return": ": boolean",
|
||||||
|
},
|
||||||
|
|
||||||
|
DFSOnBST: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "search",
|
||||||
|
args: "head: BinaryNode<number>, needle: number",
|
||||||
|
"return": ": boolean",
|
||||||
|
},
|
||||||
|
|
||||||
|
DFSGraphList: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "dfs",
|
||||||
|
args: "graph: WeightedAdjacencyList, source: number, needle: number",
|
||||||
|
"return": "number[]",
|
||||||
|
},
|
||||||
|
|
||||||
|
BFSGraphList: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "bfs",
|
||||||
|
args: "graph: WeightedAdjacencyList, source: number, needle: number",
|
||||||
|
"return": "number[]",
|
||||||
|
},
|
||||||
|
|
||||||
|
BFSGraphMatrix: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "bfs",
|
||||||
|
args: "graph: WeightedAdjacencyMatrix, source: number, needle: number",
|
||||||
|
"return": "number[]",
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,8 @@ function create_class(name, item) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function create_function(name, item) {
|
function create_function(name, item) {
|
||||||
fs.writeFileSync(path.join(day_path, `${name}.ts`), `export default function ${item.fn}(${item.args}): ${item.return} {
|
const g = item.generic ? item.generic : "";
|
||||||
|
fs.writeFileSync(path.join(day_path, `${name}.ts`), `export default function ${item.fn}${g}(${item.args}): ${item.return} {
|
||||||
|
|
||||||
}`);
|
}`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
src/__tests__/BinarySearchArray.ts
Normal file
13
src/__tests__/BinarySearchArray.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import binary_fn from "@code/binary_search_array"
|
||||||
|
|
||||||
|
test("binary search array", function() {
|
||||||
|
|
||||||
|
const foo = [1, 3, 4, 69, 71, 81, 90, 99, 420, 1337, 69420];
|
||||||
|
expect(binary_fn(foo, 69)).toEqual(true);
|
||||||
|
expect(binary_fn(foo, 1336)).toEqual(false);
|
||||||
|
expect(binary_fn(foo, 69420)).toEqual(true);
|
||||||
|
expect(binary_fn(foo, 69421)).toEqual(false);
|
||||||
|
expect(binary_fn(foo, 1)).toEqual(true);
|
||||||
|
expect(binary_fn(foo, 0)).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
5
src/global.d.ts
vendored
5
src/global.d.ts
vendored
|
|
@ -1,3 +1,8 @@
|
||||||
|
declare type Point = {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
}
|
||||||
|
|
||||||
declare interface List<T> {
|
declare interface List<T> {
|
||||||
get length(): number;
|
get length(): number;
|
||||||
removeAt(index: number): T | undefined;
|
removeAt(index: number): T | undefined;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue