feat: more algos
This commit is contained in:
parent
5a946a1f53
commit
5335cc10b0
16 changed files with 151 additions and 78 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
"clearMocks": true,
|
"clearMocks": true,
|
||||||
"moduleNameMapper": {
|
"moduleNameMapper": {
|
||||||
"@code/(.*)": [
|
"@code/(.*)": [
|
||||||
"<rootDir>/src/day10/$1"
|
"<rootDir>/src/day3/$1"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"preset": "ts-jest"
|
"preset": "ts-jest"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,11 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
dsa: [
|
dsa: [
|
||||||
"ArrayList",
|
"InsertionSort",
|
||||||
|
"MergeSort",
|
||||||
|
"Queue",
|
||||||
|
"Stack",
|
||||||
|
"QuickSort",
|
||||||
|
"DijkstraList",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
13
package.json
13
package.json
|
|
@ -13,13 +13,18 @@
|
||||||
"typescript": "^4.7.4"
|
"typescript": "^4.7.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest ArrayList",
|
"test": "jest InsertionSort MergeSort Queue Stack QuickSort DijkstraList",
|
||||||
"clear": "./scripts/clear",
|
"clear": "./scripts/clear",
|
||||||
"generate": "./scripts/generate",
|
"generate": "./scripts/generate",
|
||||||
"start": "echo vim /home/mpaulson/personal/kata/src/day9",
|
"day": "echo /home/mpaulson/personal/kata/src/day3"
|
||||||
"day": "echo /home/mpaulson/personal/kata/src/day10"
|
|
||||||
},
|
},
|
||||||
"kata_stats": {
|
"kata_stats": {
|
||||||
"ArrayList": 8
|
"ArrayList": 0,
|
||||||
|
"DijkstraList": 2,
|
||||||
|
"InsertionSort": 1,
|
||||||
|
"MergeSort": 1,
|
||||||
|
"Queue": 1,
|
||||||
|
"Stack": 1,
|
||||||
|
"QuickSort": 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -22,11 +22,26 @@ module.exports = {
|
||||||
args: "arr: number[]",
|
args: "arr: number[]",
|
||||||
"return": "void",
|
"return": "void",
|
||||||
},
|
},
|
||||||
|
|
||||||
MergeSort: {
|
MergeSort: {
|
||||||
type: "fn",
|
type: "fn",
|
||||||
fn: "merge_sort",
|
fn: "merge_sort",
|
||||||
args: "arr: number[]",
|
args: "arr: number[]",
|
||||||
"return": "void",
|
"return": "void",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
QuickSort: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "quick_sort",
|
||||||
|
args: "arr: number[]",
|
||||||
|
"return": "void",
|
||||||
|
},
|
||||||
|
|
||||||
|
DijkstraList: {
|
||||||
|
type: "fn",
|
||||||
|
fn: "dijkstra_list",
|
||||||
|
args: "source: number, sink: number, arr: WeightedAdjacencyList",
|
||||||
|
"return": "number[]",
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ try { fs.unlinkSync(day_path); } catch (e) { }
|
||||||
try { fs.mkdirSync(day_path); } catch (e) { }
|
try { fs.mkdirSync(day_path); } catch (e) { }
|
||||||
|
|
||||||
function create_class(name, item) {
|
function create_class(name, item) {
|
||||||
fs.writeFileSync(path.join(day_path, `${name}.ts`), `export default class ${name} {
|
fs.writeFileSync(path.join(day_path, `${name}.ts`), `export default class ${name}<T> {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,9 @@
|
||||||
import ArrayList from "@code/ArrayList";
|
import ArrayList from "@code/ArrayList";
|
||||||
|
import { test_list } from "./ListTest";
|
||||||
|
|
||||||
test("array-list", function() {
|
test("array-list", function() {
|
||||||
const list = new ArrayList<number>(3);
|
const list = new ArrayList<number>(3);
|
||||||
|
test_list(list);
|
||||||
list.add(5);
|
|
||||||
list.add(7);
|
|
||||||
list.add(9);
|
|
||||||
|
|
||||||
expect(list.get(2)).toEqual(9);
|
|
||||||
expect(list.removeAt(1)).toEqual(7);
|
|
||||||
expect(list.length).toEqual(2);
|
|
||||||
|
|
||||||
list.add(11);
|
|
||||||
expect(list.removeAt(1)).toEqual(9);
|
|
||||||
expect(list.remove(9)).toEqual(undefined);
|
|
||||||
expect(list.removeAt(0)).toEqual(5);
|
|
||||||
expect(list.removeAt(0)).toEqual(11);
|
|
||||||
expect(list.length).toEqual(0);
|
|
||||||
|
|
||||||
list.add(5);
|
|
||||||
list.add(7);
|
|
||||||
list.add(9);
|
|
||||||
expect(list.length).toEqual(3);
|
|
||||||
expect(list.removeAt(1)).toEqual(7);
|
|
||||||
expect(list.capacity).toEqual(3);
|
|
||||||
|
|
||||||
list.add(11);
|
|
||||||
list.add(13);
|
|
||||||
expect(list.length).toEqual(4);
|
|
||||||
expect(list.capacity).toEqual(6);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
30
src/__tests__/DijkstraList.ts
Normal file
30
src/__tests__/DijkstraList.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import dijkstra_list from "@code/DijkstraList"
|
||||||
|
|
||||||
|
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,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
@ -1,20 +1,9 @@
|
||||||
import LinkedList from "@code/DoublyLinkedList";
|
import LinkedList from "@code/DoublyLinkedList";
|
||||||
|
import { test_list } from "./ListTest";
|
||||||
|
|
||||||
test("linked-list", function() {
|
test("DoublyLinkedList", function() {
|
||||||
const list = new LinkedList<number>();
|
const list = new LinkedList<number>(3);
|
||||||
|
test_list(list);
|
||||||
list.add(5);
|
|
||||||
list.add(7);
|
|
||||||
list.add(9);
|
|
||||||
|
|
||||||
expect(list.remove()).toEqual(5);
|
|
||||||
expect(list.length).toEqual(2);
|
|
||||||
|
|
||||||
list.add(11);
|
|
||||||
expect(list.remove()).toEqual(7);
|
|
||||||
expect(list.remove()).toEqual(9);
|
|
||||||
expect(list.remove()).toEqual(11);
|
|
||||||
expect(list.remove()).toEqual(undefined);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ import insertion_sort from "@code/InsertionSort";
|
||||||
|
|
||||||
test("insertion-sort", function() {
|
test("insertion-sort", function() {
|
||||||
const arr = [9, 3, 7, 4, 69, 420, 42];
|
const arr = [9, 3, 7, 4, 69, 420, 42];
|
||||||
|
debugger
|
||||||
|
// where is my debugger
|
||||||
insertion_sort(arr);
|
insertion_sort(arr);
|
||||||
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
17
src/__tests__/ListTest.ts
Normal file
17
src/__tests__/ListTest.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
export function test_list(list: List<number>): void {
|
||||||
|
list.add(5);
|
||||||
|
list.add(7);
|
||||||
|
list.add(9);
|
||||||
|
|
||||||
|
expect(list.get(2)).toEqual(9);
|
||||||
|
expect(list.removeAt(1)).toEqual(7);
|
||||||
|
expect(list.length).toEqual(2);
|
||||||
|
|
||||||
|
list.add(11);
|
||||||
|
expect(list.removeAt(1)).toEqual(9);
|
||||||
|
expect(list.remove(9)).toEqual(undefined);
|
||||||
|
expect(list.removeAt(0)).toEqual(5);
|
||||||
|
expect(list.removeAt(0)).toEqual(11);
|
||||||
|
expect(list.length).toEqual(0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -10,12 +10,24 @@ test("queue", function() {
|
||||||
expect(list.dequeue()).toEqual(5);
|
expect(list.dequeue()).toEqual(5);
|
||||||
expect(list.length).toEqual(2);
|
expect(list.length).toEqual(2);
|
||||||
|
|
||||||
|
// this must be wrong..?
|
||||||
|
debugger
|
||||||
|
|
||||||
|
// i hate using debuggers
|
||||||
list.enqueue(11);
|
list.enqueue(11);
|
||||||
|
debugger;
|
||||||
expect(list.dequeue()).toEqual(7);
|
expect(list.dequeue()).toEqual(7);
|
||||||
expect(list.dequeue()).toEqual(9);
|
expect(list.dequeue()).toEqual(9);
|
||||||
expect(list.peek()).toEqual(11);
|
expect(list.peek()).toEqual(11);
|
||||||
expect(list.dequeue()).toEqual(11);
|
expect(list.dequeue()).toEqual(11);
|
||||||
expect(list.dequeue()).toEqual(undefined);
|
expect(list.dequeue()).toEqual(undefined);
|
||||||
|
expect(list.length).toEqual(0);
|
||||||
|
|
||||||
|
// just wanted to make sure that I could not blow up myself when i remove
|
||||||
|
// everything
|
||||||
|
list.enqueue(69);
|
||||||
|
expect(list.peek()).toEqual(69);
|
||||||
|
expect(list.length).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
12
src/__tests__/QuickSort.ts
Normal file
12
src/__tests__/QuickSort.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
import quick_sort from "@code/QuickSort";
|
||||||
|
|
||||||
|
test("quick-sort", function() {
|
||||||
|
const arr = [9, 3, 7, 4, 69, 420, 42];
|
||||||
|
|
||||||
|
debugger;
|
||||||
|
quick_sort(arr);
|
||||||
|
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,27 +1,9 @@
|
||||||
import LinkedList from "@code/SingleLinkedList";
|
import LinkedList from "@code/SingleLinkedList";
|
||||||
|
import { test_list } from "./ListTest";
|
||||||
|
|
||||||
test("linked-list", function() {
|
test("linked-list", function() {
|
||||||
const list = new LinkedList<number>();
|
const list = new LinkedList<number>(3);
|
||||||
|
test_list(list);
|
||||||
list.add(5);
|
|
||||||
list.add(7);
|
|
||||||
list.add(9);
|
|
||||||
|
|
||||||
expect(list.remove()).toEqual(5);
|
|
||||||
expect(list.length).toEqual(2);
|
|
||||||
|
|
||||||
list.add(11);
|
|
||||||
expect(list.remove()).toEqual(7);
|
|
||||||
expect(list.remove()).toEqual(9);
|
|
||||||
expect(list.remove()).toEqual(11);
|
|
||||||
expect(list.remove()).toEqual(undefined);
|
|
||||||
expect(list.length).toEqual(0);
|
|
||||||
|
|
||||||
list.add(5);
|
|
||||||
list.add(7);
|
|
||||||
list.add(9);
|
|
||||||
expect(list.length).toEqual(3);
|
|
||||||
expect(list.removeAt(1)).toEqual(7);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import Stack from "@code/Stack";
|
import Stack from "@code/Stack";
|
||||||
|
|
||||||
test("queue", function() {
|
test("stack", function() {
|
||||||
const list = new Stack<number>();
|
const list = new Stack<number>();
|
||||||
|
|
||||||
list.push(5);
|
list.push(5);
|
||||||
|
|
@ -16,6 +16,14 @@ test("queue", function() {
|
||||||
expect(list.peek()).toEqual(5);
|
expect(list.peek()).toEqual(5);
|
||||||
expect(list.pop()).toEqual(5);
|
expect(list.pop()).toEqual(5);
|
||||||
expect(list.pop()).toEqual(undefined);
|
expect(list.pop()).toEqual(undefined);
|
||||||
|
|
||||||
|
// just wanted to make sure that I could not blow up myself when i remove
|
||||||
|
// everything
|
||||||
|
list.push(69);
|
||||||
|
expect(list.peek()).toEqual(69);
|
||||||
|
expect(list.length).toEqual(1);
|
||||||
|
|
||||||
|
//yayaya
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
25
src/global.d.ts
vendored
Normal file
25
src/global.d.ts
vendored
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
declare interface List<T> {
|
||||||
|
get length(): number;
|
||||||
|
removeAt(index: number): T | undefined;
|
||||||
|
remove(item: T): T | undefined;
|
||||||
|
get(index: number): T | undefined;
|
||||||
|
add(item: T): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare type GraphEdge = {to: number, weight: number};
|
||||||
|
declare type WeightedAdjacencyList = GraphEdge[][];
|
||||||
|
declare type WeightedAdjacencyMatrix = number[][]; // A number means weight
|
||||||
|
|
||||||
|
declare type AdjacencyList = number[][];
|
||||||
|
declare type AdjacencyMatrix = number[][]; // A 1 means connected
|
||||||
|
|
||||||
|
declare type BinaryNode<T> = {
|
||||||
|
value: T;
|
||||||
|
left: BinaryNode<T>;
|
||||||
|
right: BinaryNode<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare type GeneralNode<T> = {
|
||||||
|
value: T;
|
||||||
|
children: GeneralNode<T>[]
|
||||||
|
}
|
||||||
|
|
@ -9,14 +9,10 @@
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"target": "es6",
|
"target": "es6",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"typeRoots": [
|
|
||||||
"./node_modules/@nf-types",
|
|
||||||
"./node_modules/@types"
|
|
||||||
],
|
|
||||||
"baseUrl": "src",
|
"baseUrl": "src",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@code/*": [
|
"@code/*": [
|
||||||
"day10/*"
|
"day3/*"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue