From 60205795d0e10ff255ee179d84b883542df11e91 Mon Sep 17 00:00:00 2001 From: mpaulson Date: Tue, 19 Jul 2022 20:44:40 -0600 Subject: [PATCH] feat: all the things --- scripts/dsa.js | 10 +-- src/__tests__/BFSGraphList.ts | 16 +++++ src/__tests__/BFSGraphMatrix.ts | 17 +++++ src/__tests__/CompareBinaryTrees.ts | 12 ++++ src/__tests__/DFSGraphList.ts | 15 +++++ src/__tests__/{BTDFS.ts => DFSOnBST.ts} | 0 src/__tests__/DijkstraList.ts | 43 +------------ src/__tests__/Map.ts | 16 +++++ src/__tests__/PrimsList.ts | 43 +------------ src/__tests__/graph.ts | 86 +++++++++++++++++++++++++ src/__tests__/tree.ts | 45 +++++++++++++ 11 files changed, 216 insertions(+), 87 deletions(-) create mode 100644 src/__tests__/BFSGraphList.ts create mode 100644 src/__tests__/BFSGraphMatrix.ts create mode 100644 src/__tests__/CompareBinaryTrees.ts create mode 100644 src/__tests__/DFSGraphList.ts rename src/__tests__/{BTDFS.ts => DFSOnBST.ts} (100%) create mode 100644 src/__tests__/Map.ts create mode 100644 src/__tests__/graph.ts diff --git a/scripts/dsa.js b/scripts/dsa.js index 3610957..3e523ed 100644 --- a/scripts/dsa.js +++ b/scripts/dsa.js @@ -41,7 +41,7 @@ const list_interface = { module.exports = { Map: { - generic: "", + generic: "", type: "class", methods: [{ name: "get", @@ -258,7 +258,7 @@ module.exports = { CompareBinaryTrees: { type: "fn", fn: "compare", - args: "head: BinaryNode", + args: "a: BinaryNode, b: BinaryNode", "return": ": boolean", }, @@ -273,21 +273,21 @@ module.exports = { type: "fn", fn: "dfs", args: "graph: WeightedAdjacencyList, source: number, needle: number", - "return": "number[]", + "return": "number[] | null", }, BFSGraphList: { type: "fn", fn: "bfs", args: "graph: WeightedAdjacencyList, source: number, needle: number", - "return": "number[]", + "return": "number[] | null", }, BFSGraphMatrix: { type: "fn", fn: "bfs", args: "graph: WeightedAdjacencyMatrix, source: number, needle: number", - "return": "number[]", + "return": "number[] | null", }, }; diff --git a/src/__tests__/BFSGraphList.ts b/src/__tests__/BFSGraphList.ts new file mode 100644 index 0000000..dddf38a --- /dev/null +++ b/src/__tests__/BFSGraphList.ts @@ -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); +}); + + diff --git a/src/__tests__/BFSGraphMatrix.ts b/src/__tests__/BFSGraphMatrix.ts new file mode 100644 index 0000000..19f85b6 --- /dev/null +++ b/src/__tests__/BFSGraphMatrix.ts @@ -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); +}); + + + diff --git a/src/__tests__/CompareBinaryTrees.ts b/src/__tests__/CompareBinaryTrees.ts new file mode 100644 index 0000000..b7c5c5c --- /dev/null +++ b/src/__tests__/CompareBinaryTrees.ts @@ -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); +}); + + + + + diff --git a/src/__tests__/DFSGraphList.ts b/src/__tests__/DFSGraphList.ts new file mode 100644 index 0000000..7ba1710 --- /dev/null +++ b/src/__tests__/DFSGraphList.ts @@ -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); +}); + diff --git a/src/__tests__/BTDFS.ts b/src/__tests__/DFSOnBST.ts similarity index 100% rename from src/__tests__/BTDFS.ts rename to src/__tests__/DFSOnBST.ts diff --git a/src/__tests__/DijkstraList.ts b/src/__tests__/DijkstraList.ts index fa5587f..c0f2836 100644 --- a/src/__tests__/DijkstraList.ts +++ b/src/__tests__/DijkstraList.ts @@ -1,48 +1,9 @@ import dijkstra_list from "@code/DijkstraList"; +import { list1 } from "./graph"; 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? // 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]); }); diff --git a/src/__tests__/Map.ts b/src/__tests__/Map.ts new file mode 100644 index 0000000..b69b303 --- /dev/null +++ b/src/__tests__/Map.ts @@ -0,0 +1,16 @@ +import MyMap from "@code/Map"; + +test("Map", function() { + const map = new MyMap(); + 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); +}); + diff --git a/src/__tests__/PrimsList.ts b/src/__tests__/PrimsList.ts index f1a874f..26b92c3 100644 --- a/src/__tests__/PrimsList.ts +++ b/src/__tests__/PrimsList.ts @@ -1,48 +1,9 @@ import prims from "@code/PrimsAlgorithm"; +import { list1 } from "./graph"; 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 - expect(prims(list)).toEqual([ + expect(prims(list1)).toEqual([ [ { to: 2, weight: 1 }, { to: 1, weight: 3 }, diff --git a/src/__tests__/graph.ts b/src/__tests__/graph.ts new file mode 100644 index 0000000..0767fec --- /dev/null +++ b/src/__tests__/graph.ts @@ -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], +]; diff --git a/src/__tests__/tree.ts b/src/__tests__/tree.ts index fbd9d9e..b9c62a2 100644 --- a/src/__tests__/tree.ts +++ b/src/__tests__/tree.ts @@ -40,3 +40,48 @@ export const tree: BinaryNode = { } }; +export const tree2: BinaryNode = { + 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, + } + } +};