feat: more algos
This commit is contained in:
parent
5a946a1f53
commit
5335cc10b0
16 changed files with 151 additions and 78 deletions
|
|
@ -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>[]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue