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,
|
||||
"moduleNameMapper": {
|
||||
"@code/(.*)": [
|
||||
"<rootDir>/src/day10/$1"
|
||||
"<rootDir>/src/day3/$1"
|
||||
]
|
||||
},
|
||||
"preset": "ts-jest"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
module.exports = {
|
||||
dsa: [
|
||||
"ArrayList",
|
||||
"InsertionSort",
|
||||
"MergeSort",
|
||||
"Queue",
|
||||
"Stack",
|
||||
"QuickSort",
|
||||
"DijkstraList",
|
||||
],
|
||||
}
|
||||
|
||||
|
|
|
|||
15
package.json
15
package.json
|
|
@ -13,13 +13,18 @@
|
|||
"typescript": "^4.7.4"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest ArrayList",
|
||||
"test": "jest InsertionSort MergeSort Queue Stack QuickSort DijkstraList",
|
||||
"clear": "./scripts/clear",
|
||||
"generate": "./scripts/generate",
|
||||
"start": "echo vim /home/mpaulson/personal/kata/src/day9",
|
||||
"day": "echo /home/mpaulson/personal/kata/src/day10"
|
||||
"day": "echo /home/mpaulson/personal/kata/src/day3"
|
||||
},
|
||||
"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[]",
|
||||
"return": "void",
|
||||
},
|
||||
|
||||
MergeSort: {
|
||||
type: "fn",
|
||||
fn: "merge_sort",
|
||||
args: "arr: number[]",
|
||||
"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) { }
|
||||
|
||||
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() {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,34 +1,9 @@
|
|||
import ArrayList from "@code/ArrayList";
|
||||
import { test_list } from "./ListTest";
|
||||
|
||||
test("array-list", function() {
|
||||
const list = new ArrayList<number>(3);
|
||||
|
||||
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);
|
||||
test_list(list);
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
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 { test_list } from "./ListTest";
|
||||
|
||||
test("linked-list", function() {
|
||||
const list = new LinkedList<number>();
|
||||
|
||||
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);
|
||||
test("DoublyLinkedList", function() {
|
||||
const list = new LinkedList<number>(3);
|
||||
test_list(list);
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ import insertion_sort from "@code/InsertionSort";
|
|||
|
||||
test("insertion-sort", function() {
|
||||
const arr = [9, 3, 7, 4, 69, 420, 42];
|
||||
debugger
|
||||
// where is my debugger
|
||||
insertion_sort(arr);
|
||||
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.length).toEqual(2);
|
||||
|
||||
// this must be wrong..?
|
||||
debugger
|
||||
|
||||
// i hate using debuggers
|
||||
list.enqueue(11);
|
||||
debugger;
|
||||
expect(list.dequeue()).toEqual(7);
|
||||
expect(list.dequeue()).toEqual(9);
|
||||
expect(list.peek()).toEqual(11);
|
||||
expect(list.dequeue()).toEqual(11);
|
||||
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 { test_list } from "./ListTest";
|
||||
|
||||
test("linked-list", function() {
|
||||
const list = new LinkedList<number>();
|
||||
|
||||
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);
|
||||
const list = new LinkedList<number>(3);
|
||||
test_list(list);
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import Stack from "@code/Stack";
|
||||
|
||||
test("queue", function() {
|
||||
test("stack", function() {
|
||||
const list = new Stack<number>();
|
||||
|
||||
list.push(5);
|
||||
|
|
@ -16,6 +16,14 @@ test("queue", function() {
|
|||
expect(list.peek()).toEqual(5);
|
||||
expect(list.pop()).toEqual(5);
|
||||
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,
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"typeRoots": [
|
||||
"./node_modules/@nf-types",
|
||||
"./node_modules/@types"
|
||||
],
|
||||
"baseUrl": "src",
|
||||
"paths": {
|
||||
"@code/*": [
|
||||
"day10/*"
|
||||
"day3/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
|
@ -26,4 +22,4 @@
|
|||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue