feat: all the things
This commit is contained in:
parent
44bdb7177f
commit
60205795d0
11 changed files with 216 additions and 87 deletions
|
|
@ -41,7 +41,7 @@ const list_interface = {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Map: {
|
Map: {
|
||||||
generic: "<T, V>",
|
generic: "<T extends (string | number), V>",
|
||||||
type: "class",
|
type: "class",
|
||||||
methods: [{
|
methods: [{
|
||||||
name: "get",
|
name: "get",
|
||||||
|
|
@ -258,7 +258,7 @@ module.exports = {
|
||||||
CompareBinaryTrees: {
|
CompareBinaryTrees: {
|
||||||
type: "fn",
|
type: "fn",
|
||||||
fn: "compare",
|
fn: "compare",
|
||||||
args: "head: BinaryNode<number>",
|
args: "a: BinaryNode<number>, b: BinaryNode<number>",
|
||||||
"return": ": boolean",
|
"return": ": boolean",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -273,21 +273,21 @@ module.exports = {
|
||||||
type: "fn",
|
type: "fn",
|
||||||
fn: "dfs",
|
fn: "dfs",
|
||||||
args: "graph: WeightedAdjacencyList, source: number, needle: number",
|
args: "graph: WeightedAdjacencyList, source: number, needle: number",
|
||||||
"return": "number[]",
|
"return": "number[] | null",
|
||||||
},
|
},
|
||||||
|
|
||||||
BFSGraphList: {
|
BFSGraphList: {
|
||||||
type: "fn",
|
type: "fn",
|
||||||
fn: "bfs",
|
fn: "bfs",
|
||||||
args: "graph: WeightedAdjacencyList, source: number, needle: number",
|
args: "graph: WeightedAdjacencyList, source: number, needle: number",
|
||||||
"return": "number[]",
|
"return": "number[] | null",
|
||||||
},
|
},
|
||||||
|
|
||||||
BFSGraphMatrix: {
|
BFSGraphMatrix: {
|
||||||
type: "fn",
|
type: "fn",
|
||||||
fn: "bfs",
|
fn: "bfs",
|
||||||
args: "graph: WeightedAdjacencyMatrix, source: number, needle: number",
|
args: "graph: WeightedAdjacencyMatrix, source: number, needle: number",
|
||||||
"return": "number[]",
|
"return": "number[] | null",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
16
src/__tests__/BFSGraphList.ts
Normal file
16
src/__tests__/BFSGraphList.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import bfs from "@code/BFSGraphList";
|
||||||
|
import { list2 } from "./graph";
|
||||||
|
|
||||||
|
test("bfs - graph", function () {
|
||||||
|
expect(bfs(list2, 0, 6)).toEqual([
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(bfs(list2, 6, 0)).toEqual(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
17
src/__tests__/BFSGraphMatrix.ts
Normal file
17
src/__tests__/BFSGraphMatrix.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import bfs from "@code/BFSGraphMatrix";
|
||||||
|
import { matrix2 } from "./graph";
|
||||||
|
|
||||||
|
test("bfs - graph matrix", function () {
|
||||||
|
expect(bfs(matrix2, 0, 6)).toEqual([
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(bfs(matrix2, 6, 0)).toEqual(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
12
src/__tests__/CompareBinaryTrees.ts
Normal file
12
src/__tests__/CompareBinaryTrees.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
import compare from "@code/CompareBinaryTrees";
|
||||||
|
import { tree, tree2 } from "./tree";
|
||||||
|
|
||||||
|
test("bt bfs", function () {
|
||||||
|
expect(compare(tree, tree)).toEqual(true);
|
||||||
|
expect(compare(tree, tree2)).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
15
src/__tests__/DFSGraphList.ts
Normal file
15
src/__tests__/DFSGraphList.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import dfs from "@code/DFSGraphList";
|
||||||
|
import { list2 } from "./graph";
|
||||||
|
|
||||||
|
test("dfs - graph", function () {
|
||||||
|
expect(dfs(list2, 0, 6)).toEqual([
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(dfs(list2, 6, 0)).toEqual(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
@ -1,48 +1,9 @@
|
||||||
import dijkstra_list from "@code/DijkstraList";
|
import dijkstra_list from "@code/DijkstraList";
|
||||||
|
import { list1 } from "./graph";
|
||||||
|
|
||||||
test("dijkstra via adj list", function () {
|
test("dijkstra via adj list", function () {
|
||||||
const list: WeightedAdjacencyList = [];
|
|
||||||
|
|
||||||
// (1) --- (4) ---- (5)
|
|
||||||
// / | | /|
|
|
||||||
// (0) | ------|------- |
|
|
||||||
// \ |/ | |
|
|
||||||
// (1) --- (4) ---- (5)
|
|
||||||
list[0] = [
|
|
||||||
{ to: 1, weight: 3 },
|
|
||||||
{ to: 2, weight: 1 },
|
|
||||||
];
|
|
||||||
list[1] = [
|
|
||||||
{ to: 0, weight: 3 },
|
|
||||||
{ to: 2, weight: 4 },
|
|
||||||
{ to: 4, weight: 1 },
|
|
||||||
];
|
|
||||||
list[2] = [
|
|
||||||
{ to: 1, weight: 4 },
|
|
||||||
{ to: 3, weight: 7 },
|
|
||||||
{ to: 0, weight: 1 },
|
|
||||||
];
|
|
||||||
list[3] = [
|
|
||||||
{ to: 2, weight: 7 },
|
|
||||||
{ to: 4, weight: 5 },
|
|
||||||
{ to: 6, weight: 1 },
|
|
||||||
];
|
|
||||||
list[4] = [
|
|
||||||
{ to: 1, weight: 1 },
|
|
||||||
{ to: 3, weight: 5 },
|
|
||||||
{ to: 5, weight: 2 },
|
|
||||||
];
|
|
||||||
list[5] = [
|
|
||||||
{ to: 6, weight: 1 },
|
|
||||||
{ to: 4, weight: 2 },
|
|
||||||
];
|
|
||||||
list[6] = [
|
|
||||||
{ to: 3, weight: 1 },
|
|
||||||
{ to: 5, weight: 1 },
|
|
||||||
];
|
|
||||||
|
|
||||||
/// waht?
|
/// waht?
|
||||||
// what..
|
// what..
|
||||||
// what...
|
// what...
|
||||||
expect(dijkstra_list(0, 6, list)).toEqual([0, 1, 4, 5, 6]);
|
expect(dijkstra_list(0, 6, list1)).toEqual([0, 1, 4, 5, 6]);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
16
src/__tests__/Map.ts
Normal file
16
src/__tests__/Map.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import MyMap from "@code/Map";
|
||||||
|
|
||||||
|
test("Map", function() {
|
||||||
|
const map = new MyMap<string, number>();
|
||||||
|
map.set("foo", 55);
|
||||||
|
map.set("fool", 75);
|
||||||
|
map.set("foolish", 105);
|
||||||
|
map.set("bar", 69);
|
||||||
|
|
||||||
|
expect(map.get("bar")).toEqual(69);
|
||||||
|
expect(map.get("blaz")).toEqual(undefined);
|
||||||
|
|
||||||
|
map.delete("bar")
|
||||||
|
expect(map.get("bar")).toEqual(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
@ -1,48 +1,9 @@
|
||||||
import prims from "@code/PrimsAlgorithm";
|
import prims from "@code/PrimsAlgorithm";
|
||||||
|
import { list1 } from "./graph";
|
||||||
|
|
||||||
test("PrimsAlgorithm", function () {
|
test("PrimsAlgorithm", function () {
|
||||||
const list: WeightedAdjacencyList = [];
|
|
||||||
|
|
||||||
// (1) --- (4) ---- (5)
|
|
||||||
// / | | /|
|
|
||||||
// (0) | ------|------- |
|
|
||||||
// \ |/ | |
|
|
||||||
// (1) --- (4) ---- (5)
|
|
||||||
list[0] = [
|
|
||||||
{ to: 1, weight: 3 },
|
|
||||||
{ to: 2, weight: 1 },
|
|
||||||
];
|
|
||||||
list[1] = [
|
|
||||||
{ to: 0, weight: 3 },
|
|
||||||
{ to: 2, weight: 4 },
|
|
||||||
{ to: 4, weight: 1 },
|
|
||||||
];
|
|
||||||
list[2] = [
|
|
||||||
{ to: 1, weight: 4 },
|
|
||||||
{ to: 3, weight: 7 },
|
|
||||||
{ to: 0, weight: 1 },
|
|
||||||
];
|
|
||||||
list[3] = [
|
|
||||||
{ to: 2, weight: 7 },
|
|
||||||
{ to: 4, weight: 5 },
|
|
||||||
{ to: 6, weight: 1 },
|
|
||||||
];
|
|
||||||
list[4] = [
|
|
||||||
{ to: 1, weight: 1 },
|
|
||||||
{ to: 3, weight: 5 },
|
|
||||||
{ to: 5, weight: 2 },
|
|
||||||
];
|
|
||||||
list[5] = [
|
|
||||||
{ to: 6, weight: 1 },
|
|
||||||
{ to: 4, weight: 2 },
|
|
||||||
];
|
|
||||||
list[6] = [
|
|
||||||
{ to: 3, weight: 1 },
|
|
||||||
{ to: 5, weight: 1 },
|
|
||||||
];
|
|
||||||
|
|
||||||
// there is only one right answer for this graph
|
// there is only one right answer for this graph
|
||||||
expect(prims(list)).toEqual([
|
expect(prims(list1)).toEqual([
|
||||||
[
|
[
|
||||||
{ to: 2, weight: 1 },
|
{ to: 2, weight: 1 },
|
||||||
{ to: 1, weight: 3 },
|
{ to: 1, weight: 3 },
|
||||||
|
|
|
||||||
86
src/__tests__/graph.ts
Normal file
86
src/__tests__/graph.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
export const list1: WeightedAdjacencyList = [];
|
||||||
|
|
||||||
|
// (1) --- (4) ---- (5)
|
||||||
|
// / | | /|
|
||||||
|
// (0) | ------|------- |
|
||||||
|
// \ |/ | |
|
||||||
|
// (2) --- (3) ---- (6)
|
||||||
|
list1[0] = [
|
||||||
|
{ to: 1, weight: 3 },
|
||||||
|
{ to: 2, weight: 1 },
|
||||||
|
];
|
||||||
|
list1[1] = [
|
||||||
|
{ to: 0, weight: 3 },
|
||||||
|
{ to: 2, weight: 4 },
|
||||||
|
{ to: 4, weight: 1 },
|
||||||
|
];
|
||||||
|
list1[2] = [
|
||||||
|
{ to: 1, weight: 4 },
|
||||||
|
{ to: 3, weight: 7 },
|
||||||
|
{ to: 0, weight: 1 },
|
||||||
|
];
|
||||||
|
list1[3] = [
|
||||||
|
{ to: 2, weight: 7 },
|
||||||
|
{ to: 4, weight: 5 },
|
||||||
|
{ to: 6, weight: 1 },
|
||||||
|
];
|
||||||
|
list1[4] = [
|
||||||
|
{ to: 1, weight: 1 },
|
||||||
|
{ to: 3, weight: 5 },
|
||||||
|
{ to: 5, weight: 2 },
|
||||||
|
];
|
||||||
|
list1[5] = [
|
||||||
|
{ to: 6, weight: 1 },
|
||||||
|
{ to: 4, weight: 2 },
|
||||||
|
{ to: 2, weight: 18 },
|
||||||
|
];
|
||||||
|
list1[6] = [
|
||||||
|
{ to: 3, weight: 1 },
|
||||||
|
{ to: 5, weight: 1 },
|
||||||
|
];
|
||||||
|
|
||||||
|
export const list2: WeightedAdjacencyList = [];
|
||||||
|
|
||||||
|
// >(1)<--->(4) ---->(5)
|
||||||
|
// / | /|
|
||||||
|
// (0) ------|------- |
|
||||||
|
// \ v v v
|
||||||
|
// >(2) --> (3) <----(6)
|
||||||
|
list2[0] = [
|
||||||
|
{ to: 1, weight: 3 },
|
||||||
|
{ to: 2, weight: 1 },
|
||||||
|
];
|
||||||
|
list2[1] = [
|
||||||
|
{ to: 4, weight: 1 },
|
||||||
|
];
|
||||||
|
list2[2] = [
|
||||||
|
{ to: 3, weight: 7 },
|
||||||
|
];
|
||||||
|
list2[3] = [ ];
|
||||||
|
list2[4] = [
|
||||||
|
{ to: 1, weight: 1 },
|
||||||
|
{ to: 3, weight: 5 },
|
||||||
|
{ to: 5, weight: 2 },
|
||||||
|
];
|
||||||
|
list2[5] = [
|
||||||
|
{ to: 2, weight: 18 },
|
||||||
|
{ to: 6, weight: 1 },
|
||||||
|
];
|
||||||
|
list2[6] = [
|
||||||
|
{ to: 3, weight: 1 },
|
||||||
|
];
|
||||||
|
|
||||||
|
// >(1)<--->(4) ---->(5)
|
||||||
|
// / | /|
|
||||||
|
// (0) ------|------- |
|
||||||
|
// \ v v v
|
||||||
|
// >(2) --> (3) <----(6)
|
||||||
|
export const matrix2: WeightedAdjacencyMatrix = [
|
||||||
|
[0, 3, 1, 0, 0, 0, 0], // 0
|
||||||
|
[0, 0, 0, 1, 0, 0, 0],
|
||||||
|
[0, 0, 7, 0, 0, 0, 0],
|
||||||
|
[0, 0, 0, 0, 0, 0, 0],
|
||||||
|
[0, 1, 0, 5, 0, 2, 0],
|
||||||
|
[0, 0, 18, 0, 0, 0, 1],
|
||||||
|
[0, 0, 0, 1, 0, 0, 1],
|
||||||
|
];
|
||||||
|
|
@ -40,3 +40,48 @@ export const tree: BinaryNode<number> = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const tree2: BinaryNode<number> = {
|
||||||
|
value: 20,
|
||||||
|
right: {
|
||||||
|
value: 50,
|
||||||
|
right: null,
|
||||||
|
left: {
|
||||||
|
value: 30,
|
||||||
|
right: {
|
||||||
|
value: 45,
|
||||||
|
right: {
|
||||||
|
value: 49,
|
||||||
|
left: null,
|
||||||
|
right: null,
|
||||||
|
},
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 29,
|
||||||
|
right: null,
|
||||||
|
left: {
|
||||||
|
value: 21,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 10,
|
||||||
|
right: {
|
||||||
|
value: 15,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 5,
|
||||||
|
right: {
|
||||||
|
value: 7,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue