feat: speed violence and momentumn are happening

This commit is contained in:
mpaulson 2022-07-19 20:11:49 -06:00
parent b6bc2a63b1
commit 44bdb7177f
15 changed files with 248 additions and 18 deletions

View file

@ -16,8 +16,7 @@ module.exports = {
"BTBFS", "BTBFS",
"CompareBinaryTrees", "CompareBinaryTrees",
"DFSOnBST", "DFSOnBST",
// TODO: "Trie",
// Red Black Tree
"DFSGraphList", "DFSGraphList",
"BFSGraphList", "BFSGraphList",
"BFSGraphMatrix", "BFSGraphMatrix",

View file

@ -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: { BubbleSort: {
type: "fn", type: "fn",
fn: "bubble_sort", fn: "bubble_sort",
@ -206,40 +223,36 @@ module.exports = {
MazeSolver: { MazeSolver: {
type: "fn", type: "fn",
fn: "solve", 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[]", "return": ": Point[]",
}, },
BTPreOrder: { BTPreOrder: {
type: "fn", type: "fn",
fn: "pre_order_search", fn: "pre_order_search",
generic: "<T>", args: "head: BinaryNode<number>",
args: "head: BinaryNode<T>", "return": ": number[]",
"return": ": BinaryNode<T>[]",
}, },
BTInOrder: { BTInOrder: {
type: "fn", type: "fn",
fn: "in_order_search", fn: "in_order_search",
generic: "<T>", args: "head: BinaryNode<number>",
args: "head: BinaryNode<T>", "return": ": number[]",
"return": ": BinaryNode<T>[]",
}, },
BTPostOrder: { BTPostOrder: {
type: "fn", type: "fn",
fn: "post_order_search", fn: "post_order_search",
generic: "<T>", args: "head: BinaryNode<number>",
args: "head: BinaryNode<T>", "return": ": number[]",
"return": ": BinaryNode<T>[]",
}, },
BTBFS: { BTBFS: {
type: "fn", type: "fn",
fn: "bfs", fn: "bfs",
generic: "<T>", args: "head: BinaryNode<number>",
args: "head: BinaryNode<T>", "return": ": number[]",
"return": ": BinaryNode<T>[]",
}, },
CompareBinaryTrees: { CompareBinaryTrees: {

12
src/__tests__/BTBFS.ts Normal file
View file

@ -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);
});

13
src/__tests__/BTDFS.ts Normal file
View file

@ -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);
});

View file

@ -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,
]);
});

View file

@ -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,
]);
});

View file

@ -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,
]);
});

View file

@ -1,4 +1,4 @@
import binary_fn from "@code/binary_search_array" import binary_fn from "@code/BinarySearchList"
test("binary search array", function() { test("binary search array", function() {

View file

@ -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]);
});

View file

@ -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);
});

View file

@ -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},
]);
});

23
src/__tests__/Trie.ts Normal file
View file

@ -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",
]);
});

View file

@ -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);
});

42
src/__tests__/tree.ts Normal file
View file

@ -0,0 +1,42 @@
export const tree: BinaryNode<number> = {
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,
}
}
};

4
src/global.d.ts vendored
View file

@ -21,8 +21,8 @@ declare type AdjacencyMatrix = number[][]; // A 1 means connected
declare type BinaryNode<T> = { declare type BinaryNode<T> = {
value: T; value: T;
left: BinaryNode<T>; left: BinaryNode<T> | null;
right: BinaryNode<T>; right: BinaryNode<T> | null;
}; };
declare type GeneralNode<T> = { declare type GeneralNode<T> = {