From 44bdb7177f7f0ae1d0b8e7b9e98503f6f0b53ed7 Mon Sep 17 00:00:00 2001 From: mpaulson Date: Tue, 19 Jul 2022 20:11:49 -0600 Subject: [PATCH] feat: speed violence and momentumn are happening --- ligma.config.js | 3 +- scripts/dsa.js | 39 +++++++++++------ src/__tests__/BTBFS.ts | 12 ++++++ src/__tests__/BTDFS.ts | 13 ++++++ src/__tests__/BTInOrder.ts | 20 +++++++++ src/__tests__/BTPostOrder.ts | 20 +++++++++ src/__tests__/BTPreOrder.ts | 19 +++++++++ ...narySearchArray.ts => BinarySearchList.ts} | 2 +- src/__tests__/BubbleSort.ts | 10 +++++ src/__tests__/LinearSearchList.ts | 14 +++++++ src/__tests__/MazeSolver.ts | 32 ++++++++++++++ src/__tests__/Trie.ts | 23 ++++++++++ src/__tests__/TwoCrystalBalls.ts | 13 ++++++ src/__tests__/tree.ts | 42 +++++++++++++++++++ src/global.d.ts | 4 +- 15 files changed, 248 insertions(+), 18 deletions(-) create mode 100644 src/__tests__/BTBFS.ts create mode 100644 src/__tests__/BTDFS.ts create mode 100644 src/__tests__/BTInOrder.ts create mode 100644 src/__tests__/BTPostOrder.ts create mode 100644 src/__tests__/BTPreOrder.ts rename src/__tests__/{BinarySearchArray.ts => BinarySearchList.ts} (88%) create mode 100644 src/__tests__/BubbleSort.ts create mode 100644 src/__tests__/LinearSearchList.ts create mode 100644 src/__tests__/MazeSolver.ts create mode 100644 src/__tests__/Trie.ts create mode 100644 src/__tests__/TwoCrystalBalls.ts create mode 100644 src/__tests__/tree.ts diff --git a/ligma.config.js b/ligma.config.js index 74d7bc5..f947164 100644 --- a/ligma.config.js +++ b/ligma.config.js @@ -16,8 +16,7 @@ module.exports = { "BTBFS", "CompareBinaryTrees", "DFSOnBST", - // TODO: - // Red Black Tree + "Trie", "DFSGraphList", "BFSGraphList", "BFSGraphMatrix", diff --git a/scripts/dsa.js b/scripts/dsa.js index 883a9bd..3610957 100644 --- a/scripts/dsa.js +++ b/scripts/dsa.js @@ -140,6 +140,23 @@ module.exports = { }] }, + Trie: { + type: "class", + methods: [{ + name: "insert", + args: "item: string", + return: "void", + }, { + name: "delete", + args: "item: string", + return: "void", + }, { + name: "find", + args: "partial: string", + return: "string[]", + }] + }, + BubbleSort: { type: "fn", fn: "bubble_sort", @@ -206,40 +223,36 @@ module.exports = { MazeSolver: { type: "fn", fn: "solve", - args: "maze: string[][], wall: string, path: string, start: Point, end: Point", + args: "maze: string[], wall: string, path: string, start: Point, end: Point", "return": ": Point[]", }, BTPreOrder: { type: "fn", fn: "pre_order_search", - generic: "", - args: "head: BinaryNode", - "return": ": BinaryNode[]", + args: "head: BinaryNode", + "return": ": number[]", }, BTInOrder: { type: "fn", fn: "in_order_search", - generic: "", - args: "head: BinaryNode", - "return": ": BinaryNode[]", + args: "head: BinaryNode", + "return": ": number[]", }, BTPostOrder: { type: "fn", fn: "post_order_search", - generic: "", - args: "head: BinaryNode", - "return": ": BinaryNode[]", + args: "head: BinaryNode", + "return": ": number[]", }, BTBFS: { type: "fn", fn: "bfs", - generic: "", - args: "head: BinaryNode", - "return": ": BinaryNode[]", + args: "head: BinaryNode", + "return": ": number[]", }, CompareBinaryTrees: { diff --git a/src/__tests__/BTBFS.ts b/src/__tests__/BTBFS.ts new file mode 100644 index 0000000..c92a70a --- /dev/null +++ b/src/__tests__/BTBFS.ts @@ -0,0 +1,12 @@ +import bfs from "@code/BTBFS"; +import { tree } from "./tree"; + +test("bt bfs", function () { + expect(bfs(tree, 45)).toEqual(true); + expect(bfs(tree, 7)).toEqual(true); + expect(bfs(tree, 69)).toEqual(false); +}); + + + + diff --git a/src/__tests__/BTDFS.ts b/src/__tests__/BTDFS.ts new file mode 100644 index 0000000..99350c9 --- /dev/null +++ b/src/__tests__/BTDFS.ts @@ -0,0 +1,13 @@ +import dfs from "@code/BTDFS"; +import { tree } from "./tree"; + +test("bt dfs", function () { + expect(dfs(tree, 45)).toEqual(true); + expect(dfs(tree, 7)).toEqual(true); + expect(dfs(tree, 69)).toEqual(false); +}); + + + + + diff --git a/src/__tests__/BTInOrder.ts b/src/__tests__/BTInOrder.ts new file mode 100644 index 0000000..6ec397d --- /dev/null +++ b/src/__tests__/BTInOrder.ts @@ -0,0 +1,20 @@ +import bt_in_order from "@code/BTInOrder"; +import { tree } from "./tree"; + +test("In order", function () { + expect(bt_in_order(tree)).toEqual([ + 5, + 7, + 10, + 15, + 20, + 29, + 30, + 45, + 50, + 100, + ]); +}); + + + diff --git a/src/__tests__/BTPostOrder.ts b/src/__tests__/BTPostOrder.ts new file mode 100644 index 0000000..2c0a859 --- /dev/null +++ b/src/__tests__/BTPostOrder.ts @@ -0,0 +1,20 @@ +import bt_post_order from "@code/BTPostOrder"; +import { tree } from "./tree"; + +test("post order", function () { + expect(bt_post_order(tree)).toEqual([ + 7, + 5, + 15, + 10, + 15, + 29, + 45, + 30, + 100, + 20, + ]); +}); + + + diff --git a/src/__tests__/BTPreOrder.ts b/src/__tests__/BTPreOrder.ts new file mode 100644 index 0000000..91009a0 --- /dev/null +++ b/src/__tests__/BTPreOrder.ts @@ -0,0 +1,19 @@ +import bt_pre_order from "@code/BTPreOrder"; +import { tree } from "./tree"; + +test("Pre order", function () { + expect(bt_pre_order(tree)).toEqual([ + 20, + 10, + 5, + 7, + 15, + 50, + 30, + 29, + 45, + 100, + ]); +}); + + diff --git a/src/__tests__/BinarySearchArray.ts b/src/__tests__/BinarySearchList.ts similarity index 88% rename from src/__tests__/BinarySearchArray.ts rename to src/__tests__/BinarySearchList.ts index 90f2d31..804a001 100644 --- a/src/__tests__/BinarySearchArray.ts +++ b/src/__tests__/BinarySearchList.ts @@ -1,4 +1,4 @@ -import binary_fn from "@code/binary_search_array" +import binary_fn from "@code/BinarySearchList" test("binary search array", function() { diff --git a/src/__tests__/BubbleSort.ts b/src/__tests__/BubbleSort.ts new file mode 100644 index 0000000..dc60bc0 --- /dev/null +++ b/src/__tests__/BubbleSort.ts @@ -0,0 +1,10 @@ +import bubble_sort from "@code/BubbleSort"; + +test("bubble-sort", function () { + const arr = [9, 3, 7, 4, 69, 420, 42]; + + debugger; + bubble_sort(arr); + expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]); +}); + diff --git a/src/__tests__/LinearSearchList.ts b/src/__tests__/LinearSearchList.ts new file mode 100644 index 0000000..f732b00 --- /dev/null +++ b/src/__tests__/LinearSearchList.ts @@ -0,0 +1,14 @@ +import linear_fn from "@code/LinearSearchList" + +test("linear search array", function() { + + const foo = [1, 3, 4, 69, 71, 81, 90, 99, 420, 1337, 69420]; + expect(linear_fn(foo, 69)).toEqual(true); + expect(linear_fn(foo, 1336)).toEqual(false); + expect(linear_fn(foo, 69420)).toEqual(true); + expect(linear_fn(foo, 69421)).toEqual(false); + expect(linear_fn(foo, 1)).toEqual(true); + expect(linear_fn(foo, 0)).toEqual(false); +}); + + diff --git a/src/__tests__/MazeSolver.ts b/src/__tests__/MazeSolver.ts new file mode 100644 index 0000000..74a4d2c --- /dev/null +++ b/src/__tests__/MazeSolver.ts @@ -0,0 +1,32 @@ +import maze_solver from "@code/MazeSolver"; + +test("maze solver", function () { + const maze = [ + "xxxxxxxxxx x", + "x x x", + "x x x", + "x xxxxxxxx x", + "x x", + "x xxxxxxxxxx", + ]; + + // there is only one path through + expect(maze_solver(maze, "x", " ", {x: 10, y: 0}, {x: 1, y: 5})).toEqual([ + {x: 10, y: 0}, + {x: 10, y: 1}, + {x: 10, y: 2}, + {x: 10, y: 3}, + {x: 10, y: 4}, + {x: 9, y: 4}, + {x: 8, y: 4}, + {x: 7, y: 4}, + {x: 6, y: 4}, + {x: 5, y: 4}, + {x: 4, y: 4}, + {x: 3, y: 4}, + {x: 2, y: 4}, + {x: 1, y: 4}, + {x: 1, y: 5}, + ]); +}); + diff --git a/src/__tests__/Trie.ts b/src/__tests__/Trie.ts new file mode 100644 index 0000000..6862e7c --- /dev/null +++ b/src/__tests__/Trie.ts @@ -0,0 +1,23 @@ +import Trie from "@code/Trie"; + +test("Trie", function() { + const trie = new Trie(); + trie.insert("foo"); + trie.insert("fool"); + trie.insert("foolish"); + trie.insert("bar"); + + expect(trie.find("fo").sort()).toEqual([ + "foo", + "fool", + "foolish", + ]); + + trie.remove("fool"); + + expect(trie.find("fo").sort()).toEqual([ + "foo", + "foolish", + ]); +}); + diff --git a/src/__tests__/TwoCrystalBalls.ts b/src/__tests__/TwoCrystalBalls.ts new file mode 100644 index 0000000..bbb0c35 --- /dev/null +++ b/src/__tests__/TwoCrystalBalls.ts @@ -0,0 +1,13 @@ +import two_crystal_balls from "@code/TwoCrystalBalls"; + +test("two crystal balls", function () { + let idx = Math.floor(Math.random() * 10000); + const data = new Array(10000).fill(false); + + for (let i = idx; i < 10000; ++i) { + data[i] = true; + } + + expect(two_crystal_balls(data)).toEqual(idx); +}); + diff --git a/src/__tests__/tree.ts b/src/__tests__/tree.ts new file mode 100644 index 0000000..fbd9d9e --- /dev/null +++ b/src/__tests__/tree.ts @@ -0,0 +1,42 @@ +export const tree: BinaryNode = { + value: 20, + right: { + value: 50, + right: { + value: 100, + right: null, + left: null, + }, + left: { + value: 30, + right: { + value: 45, + right: null, + left: null, + }, + left: { + value: 29, + 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, + } + } +}; + diff --git a/src/global.d.ts b/src/global.d.ts index 14e3185..77811a1 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -21,8 +21,8 @@ declare type AdjacencyMatrix = number[][]; // A 1 means connected declare type BinaryNode = { value: T; - left: BinaryNode; - right: BinaryNode; + left: BinaryNode | null; + right: BinaryNode | null; }; declare type GeneralNode = {