diff --git a/.jest.config.json b/.jest.config.json index b0d729d..9ad17ab 100644 --- a/.jest.config.json +++ b/.jest.config.json @@ -2,7 +2,7 @@ "clearMocks": true, "moduleNameMapper": { "@code/(.*)": [ - "/src/day10/$1" + "/src/day3/$1" ] }, "preset": "ts-jest" diff --git a/ligma.config.js b/ligma.config.js index f9624f0..85010ad 100644 --- a/ligma.config.js +++ b/ligma.config.js @@ -1,6 +1,11 @@ module.exports = { dsa: [ - "ArrayList", + "InsertionSort", + "MergeSort", + "Queue", + "Stack", + "QuickSort", + "DijkstraList", ], } diff --git a/package.json b/package.json index 55ad4dc..c3fae46 100644 --- a/package.json +++ b/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 } -} \ No newline at end of file +} diff --git a/scripts/dsa.js b/scripts/dsa.js index eef52cb..2f3eff7 100644 --- a/scripts/dsa.js +++ b/scripts/dsa.js @@ -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[]", + }, }; diff --git a/scripts/generate b/scripts/generate index 31758da..9a2a39b 100755 --- a/scripts/generate +++ b/scripts/generate @@ -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} { constructor() { } diff --git a/src/__tests__/ArrayList.ts b/src/__tests__/ArrayList.ts index 480c8a0..40de2da 100644 --- a/src/__tests__/ArrayList.ts +++ b/src/__tests__/ArrayList.ts @@ -1,34 +1,9 @@ import ArrayList from "@code/ArrayList"; +import { test_list } from "./ListTest"; test("array-list", function() { const list = new ArrayList(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); }); diff --git a/src/__tests__/DijkstraList.ts b/src/__tests__/DijkstraList.ts new file mode 100644 index 0000000..696b10b --- /dev/null +++ b/src/__tests__/DijkstraList.ts @@ -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, + ]); +}); + diff --git a/src/__tests__/DoublyLinkedList.ts b/src/__tests__/DoublyLinkedList.ts index 5e66a79..b696784 100644 --- a/src/__tests__/DoublyLinkedList.ts +++ b/src/__tests__/DoublyLinkedList.ts @@ -1,20 +1,9 @@ import LinkedList from "@code/DoublyLinkedList"; +import { test_list } from "./ListTest"; -test("linked-list", function() { - const list = new LinkedList(); - - 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(3); + test_list(list); }); diff --git a/src/__tests__/InsertionSort.ts b/src/__tests__/InsertionSort.ts index c4cb104..b52c2e5 100644 --- a/src/__tests__/InsertionSort.ts +++ b/src/__tests__/InsertionSort.ts @@ -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]); }); diff --git a/src/__tests__/ListTest.ts b/src/__tests__/ListTest.ts new file mode 100644 index 0000000..de20a84 --- /dev/null +++ b/src/__tests__/ListTest.ts @@ -0,0 +1,17 @@ +export function test_list(list: List): 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); + +} diff --git a/src/__tests__/Queue.ts b/src/__tests__/Queue.ts index 6d61ca9..e28bf7b 100644 --- a/src/__tests__/Queue.ts +++ b/src/__tests__/Queue.ts @@ -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); }); diff --git a/src/__tests__/QuickSort.ts b/src/__tests__/QuickSort.ts new file mode 100644 index 0000000..e6c62e1 --- /dev/null +++ b/src/__tests__/QuickSort.ts @@ -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]); +}); + + + diff --git a/src/__tests__/SinglyLinkedList.ts b/src/__tests__/SinglyLinkedList.ts index 664b45b..99e565c 100644 --- a/src/__tests__/SinglyLinkedList.ts +++ b/src/__tests__/SinglyLinkedList.ts @@ -1,27 +1,9 @@ import LinkedList from "@code/SingleLinkedList"; +import { test_list } from "./ListTest"; test("linked-list", function() { - const list = new LinkedList(); - - 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(3); + test_list(list); }); diff --git a/src/__tests__/Stack.ts b/src/__tests__/Stack.ts index 9d30d41..b478b68 100644 --- a/src/__tests__/Stack.ts +++ b/src/__tests__/Stack.ts @@ -1,6 +1,6 @@ import Stack from "@code/Stack"; -test("queue", function() { +test("stack", function() { const list = new Stack(); 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 }); diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 0000000..4d5f63a --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,25 @@ +declare interface List { + 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 = { + value: T; + left: BinaryNode; + right: BinaryNode; +} + +declare type GeneralNode = { + value: T; + children: GeneralNode[] +} diff --git a/tsconfig.json b/tsconfig.json index a635010..56dd8c6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" ] -} +} \ No newline at end of file