Skip to content

Commit 2fe6f34

Browse files
authored
Merge pull request #57 from monkey0722/feature/settings
Update project configuration and improve code quality across data structures
2 parents 42cc2ee + 261fe02 commit 2fe6f34

File tree

21 files changed

+71
-117
lines changed

21 files changed

+71
-117
lines changed

.prettierrc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
2+
"printWidth": 100,
3+
"tabWidth": 2,
4+
"useTabs": false,
5+
"semi": true,
26
"singleQuote": true,
7+
"quoteProps": "as-needed",
8+
"trailingComma": "all",
39
"bracketSpacing": false,
4-
"trailingComma": "es5"
10+
"bracketSameLine": false,
11+
"arrowParens": "always",
12+
"endOfLine": "lf",
13+
"embeddedLanguageFormatting": "auto",
14+
"singleAttributePerLine": false
515
}

algorithms/dp/knapsack.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('Knapsack', () => {
5252
];
5353
const capacity = 5;
5454
expect(() => Knapsack.solve(items, capacity)).toThrowError(
55-
'Item weights and values must be non-negative'
55+
'Item weights and values must be non-negative',
5656
);
5757
});
5858
});
@@ -95,7 +95,7 @@ describe('Knapsack', () => {
9595
];
9696
const capacity = 15;
9797
expect(() => Knapsack.solveFractional(items, capacity)).toThrowError(
98-
'Item weights and values must be non-negative'
98+
'Item weights and values must be non-negative',
9999
);
100100
});
101101
});

algorithms/dp/knapsack.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class Knapsack {
1313
*/
1414
static solve(
1515
items: Item[],
16-
capacity: number
16+
capacity: number,
1717
): {
1818
maxValue: number;
1919
selectedItems: boolean[];
@@ -28,19 +28,14 @@ export class Knapsack {
2828
}
2929

3030
const n = items.length;
31-
const dp: number[][] = Array.from({length: n + 1}, () =>
32-
Array(capacity + 1).fill(0)
33-
);
31+
const dp: number[][] = Array.from({length: n + 1}, () => Array(capacity + 1).fill(0));
3432

3533
// Build the DP table
3634
for (let i = 1; i <= n; i++) {
3735
const item = items[i - 1];
3836
for (let w = 0; w <= capacity; w++) {
3937
if (item.weight <= w) {
40-
dp[i][w] = Math.max(
41-
dp[i - 1][w],
42-
dp[i - 1][w - item.weight] + item.value
43-
);
38+
dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - item.weight] + item.value);
4439
} else {
4540
dp[i][w] = dp[i - 1][w];
4641
}
@@ -72,7 +67,7 @@ export class Knapsack {
7267
*/
7368
static solveFractional(
7469
items: Item[],
75-
capacity: number
70+
capacity: number,
7671
): {
7772
maxValue: number;
7873
fractions: number[];

algorithms/search/bellman-ford-search/bellmanFordSearch.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ describe('BellmanFord', () => {
3535
{source: 1, target: 2, weight: 3},
3636
];
3737
expect(() => BellmanFord.findShortestPaths(0, edges, 0)).toThrowError(
38-
'Number of vertices must be positive'
38+
'Number of vertices must be positive',
3939
);
4040
});
4141
test('should throw error for invalid edge vertices', () => {
4242
const edges = [{source: 0, target: 5, weight: 4}];
4343
expect(() => BellmanFord.findShortestPaths(4, edges, 0)).toThrowError(
44-
'Edge contains an invalid vertex'
44+
'Edge contains an invalid vertex',
4545
);
4646
});
4747
test('should handle large graph with no negative weight cycles', () => {

algorithms/search/bellman-ford-search/bellmanFordSearch.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ export class BellmanFord {
1515
* @returns {number[] | null} - An array of shortest distances to each vertex from the source, or `null` if a negative weight cycle exists.
1616
* @throws {Error} If the input parameters are invalid.
1717
*/
18-
static findShortestPaths(
19-
vertices: number,
20-
edges: Edge[],
21-
source: number
22-
): number[] | null {
18+
static findShortestPaths(vertices: number, edges: Edge[], source: number): number[] | null {
2319
// Validate input
2420
this.validateInput(vertices, edges, source);
2521

@@ -62,7 +58,7 @@ export class BellmanFord {
6258
vertices: number,
6359
edges: Edge[],
6460
source: number,
65-
target: number
61+
target: number,
6662
): number[] | null {
6763
// Validate input
6864
this.validateInput(vertices, edges, source);
@@ -131,11 +127,7 @@ export class BellmanFord {
131127
* @param {number} source - The starting vertex.
132128
* @throws {Error} If the input parameters are invalid.
133129
*/
134-
private static validateInput(
135-
vertices: number,
136-
edges: Edge[],
137-
source: number
138-
): void {
130+
private static validateInput(vertices: number, edges: Edge[], source: number): void {
139131
if (vertices <= 0) {
140132
throw new Error('Number of vertices must be positive');
141133
}

algorithms/search/interpolation-search/interpolationSearch.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
export function interpolationSearch(
2-
arr: number[],
3-
key: number
4-
): number | undefined {
1+
export function interpolationSearch(arr: number[], key: number): number | undefined {
52
let low = 0;
63
let high = arr.length - 1;
74
while (low <= high && key >= arr[low] && key <= arr[high]) {
8-
const pos =
9-
low + ((high - low) / (arr[high] - arr[low])) * (key - arr[low]);
5+
const pos = low + ((high - low) / (arr[high] - arr[low])) * (key - arr[low]);
106
if (arr[Math.floor(pos)] === key) {
117
return Math.floor(pos);
128
}

algorithms/search/jump-search/jumpSearch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export function jumpSearch(arr: number[], x: number): number | undefined {
1111
}
1212
while (arr[prev] < x) {
1313
prev++;
14-
if (prev == Math.min(step, n)) {
14+
if (prev === Math.min(step, n)) {
1515
return undefined;
1616
}
1717
}
18-
if (arr[prev] == x) {
18+
if (arr[prev] === x) {
1919
return prev;
2020
}
2121
return undefined;

algorithms/search/linear-search/linearSearch.test.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
import {linearSearch} from './linearSearch';
22

33
describe('linear-search', () => {
4-
const colors = [
5-
'red',
6-
'orange',
7-
'yellow',
8-
'green',
9-
'blue',
10-
'indigo',
11-
'violet',
12-
];
4+
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
135

146
it('normal', () => {
157
expect(linearSearch(colors, 'red')).toEqual(0);

algorithms/sorting/bubble-sort-counters/bubbleSortCounters.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
bubbleSortCountersBasic,
3-
bubbleSortCounters,
4-
} from './bubbleSortCounters';
1+
import {bubbleSortCountersBasic, bubbleSortCounters} from './bubbleSortCounters';
52

63
describe('bubble-sort-counters', () => {
74
const randomArray = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];

algorithms/sorting/merge-sort-counters/mergeSortCounters.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let countOuter = 0;
77
let countInner = 0;
88
let countSwap = 0;
99

10-
function resetCounters() {
10+
function resetCounters(): void {
1111
countOuter = 0;
1212
countInner = 0;
1313
countSwap = 0;
@@ -42,7 +42,7 @@ export function mergeSortCountersTopDown(items: number[]): number[] {
4242

4343
return mergeCountersTopDown(
4444
mergeSortCountersTopDown(leftItems),
45-
mergeSortCountersTopDown(rightItems)
45+
mergeSortCountersTopDown(rightItems),
4646
);
4747
}
4848

@@ -76,11 +76,7 @@ function mergeSortCountersBottomUp(items: number[]): number[] {
7676
return items;
7777
}
7878

79-
function mergeCountersBottomUp(
80-
items: number[],
81-
left: number,
82-
step: number
83-
): void {
79+
function mergeCountersBottomUp(items: number[], left: number, step: number): void {
8480
const tmp: number[] = [];
8581
const right: number = left + step;
8682
const last: number = Math.min(left + step * 2 - 1, items.length - 1);
@@ -89,10 +85,7 @@ function mergeCountersBottomUp(
8985
let moveRight: number = right;
9086

9187
for (let i = left; i <= last; i++) {
92-
if (
93-
(items[moveLeft] <= items[moveRight] || moveRight > last) &&
94-
moveLeft < right
95-
) {
88+
if ((items[moveLeft] <= items[moveRight] || moveRight > last) && moveLeft < right) {
9689
tmp[i] = items[moveLeft];
9790
moveLeft++;
9891
} else {

algorithms/sorting/merge-sort/mergeSort.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ export function mergeSortTopDown(items: number[]): number[] {
2323
const leftItems = items.slice(0, middle);
2424
const rightItems = items.slice(middle);
2525

26-
return mergeTopDown(
27-
mergeSortTopDown(leftItems),
28-
mergeSortTopDown(rightItems)
29-
);
26+
return mergeTopDown(mergeSortTopDown(leftItems), mergeSortTopDown(rightItems));
3027
}
3128

3229
/**
@@ -41,10 +38,7 @@ function mergeBottomUp(items: number[], left: number, step: number): void {
4138
let moveRight = right;
4239

4340
for (let i = left; i <= last; i++) {
44-
if (
45-
(items[moveLeft] <= items[moveRight] || moveRight > last) &&
46-
moveLeft < right
47-
) {
41+
if ((items[moveLeft] <= items[moveRight] || moveRight > last) && moveLeft < right) {
4842
tmp[i] = items[moveLeft];
4943
moveLeft++;
5044
} else {

data-structures/binary-tree/binaryTree.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class BinaryTree<T> {
1010
* Inserts a new value into the binary search tree.
1111
* @param {T} value The value to insert.
1212
*/
13-
insert(value: T) {
13+
insert(value: T): void {
1414
const newNode = new BinaryTreeNode(value);
1515
if (this.root === null) {
1616
this.root = newNode;
@@ -19,7 +19,7 @@ export class BinaryTree<T> {
1919
}
2020
}
2121

22-
private insertNode(node: BinaryTreeNode<T>, newNode: BinaryTreeNode<T>) {
22+
private insertNode(node: BinaryTreeNode<T>, newNode: BinaryTreeNode<T>): void {
2323
if (newNode.value < node.value) {
2424
if (node.left === null) {
2525
node.left = newNode;
@@ -44,10 +44,7 @@ export class BinaryTree<T> {
4444
return this.search(this.root, value) !== null;
4545
}
4646

47-
private search(
48-
node: BinaryTreeNode<T> | null,
49-
value: T
50-
): BinaryTreeNode<T> | null {
47+
private search(node: BinaryTreeNode<T> | null, value: T): BinaryTreeNode<T> | null {
5148
if (node === null) {
5249
return null;
5350
}
@@ -65,8 +62,8 @@ export class BinaryTree<T> {
6562
* Traverses the binary search tree in order (left, root, right).
6663
* @param {function} visit Function to call on each value.
6764
*/
68-
inOrderTraverse(visit: (value: T) => void) {
69-
function traverse(node: BinaryTreeNode<T> | null) {
65+
inOrderTraverse(visit: (value: T) => void): void {
66+
function traverse(node: BinaryTreeNode<T> | null): void {
7067
if (node === null) return;
7168
traverse(node.left);
7269
visit(node.value);
@@ -79,14 +76,11 @@ export class BinaryTree<T> {
7976
* Removes a value from the binary search tree.
8077
* @param {T} value The value to remove.
8178
*/
82-
remove(value: T) {
79+
remove(value: T): void {
8380
this.root = this.removeNode(this.root, value);
8481
}
8582

86-
private removeNode(
87-
node: BinaryTreeNode<T> | null,
88-
value: T
89-
): BinaryTreeNode<T> | null {
83+
private removeNode(node: BinaryTreeNode<T> | null, value: T): BinaryTreeNode<T> | null {
9084
if (node === null) {
9185
return null;
9286
}

data-structures/hash-table/hashTable.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ describe('HashTable with large data set', () => {
8080
hashTable.set(i, i);
8181
}
8282
const endTime = performance.now();
83-
console.log(
84-
`Inserting ${largeSize} items took ${endTime - startTime} milliseconds`
85-
);
83+
console.log(`Inserting ${largeSize} items took ${endTime - startTime} milliseconds`);
8684
});
8785
});

data-structures/heap/minHeap.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class MinHeap<T> {
2525
* @param {number} parentIndex The index of the parent node.
2626
* @return {number} The index of the right child.
2727
*/
28-
priva;
2928
private getRightChildIndex(parentIndex: number): number {
3029
return 2 * parentIndex + 2;
3130
}
@@ -45,10 +44,7 @@ export class MinHeap<T> {
4544
* @param {number} index2 The index of the second element.
4645
*/
4746
private swap(index1: number, index2: number): void {
48-
[this.heap[index1], this.heap[index2]] = [
49-
this.heap[index2],
50-
this.heap[index1],
51-
];
47+
[this.heap[index1], this.heap[index2]] = [this.heap[index2], this.heap[index1]];
5248
}
5349

5450
/**
@@ -58,8 +54,7 @@ export class MinHeap<T> {
5854
let index = this.heap.length - 1;
5955
while (
6056
index > 0 &&
61-
this.compareFn(this.heap[this.getParentIndex(index)], this.heap[index]) >
62-
0
57+
this.compareFn(this.heap[this.getParentIndex(index)], this.heap[index]) > 0
6358
) {
6459
const parentIndex = this.getParentIndex(index);
6560
this.swap(index, parentIndex);
@@ -81,10 +76,7 @@ export class MinHeap<T> {
8176
const rightChildIndex = this.getRightChildIndex(index);
8277
if (
8378
rightChildIndex < this.heap.length &&
84-
this.compareFn(
85-
this.heap[rightChildIndex],
86-
this.heap[smallerChildIndex]
87-
) < 0
79+
this.compareFn(this.heap[rightChildIndex], this.heap[smallerChildIndex]) < 0
8880
) {
8981
smallerChildIndex = rightChildIndex;
9082
}

data-structures/red-black-tree/redBlackTree.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ export class RedBlackTree<T> {
4343
* Initializes an empty Red-Black Tree.
4444
* @param {(a: T, b: T) => number} [compare] - A custom comparator function. Default: numerical comparison.
4545
*/
46-
constructor(
47-
private compare: (a: T, b: T) => number = (a, b) =>
48-
a < b ? -1 : a > b ? 1 : 0
49-
) {
46+
constructor(private compare: (a: T, b: T) => number = (a, b) => (a < b ? -1 : a > b ? 1 : 0)) {
5047
this.nil = new Node<T>(null as T, null!);
5148
this.nil.color = Color.BLACK;
5249
this.nil.left = this.nil.right = this.nil;

data-structures/trie/trie.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ describe('Trie', () => {
2828
trie.insert('app');
2929
trie.insert('application');
3030
trie.insert('banana');
31-
expect(trie.findWords('app').sort()).toEqual(
32-
['app', 'apple', 'application'].sort()
33-
);
31+
expect(trie.findWords('app').sort()).toEqual(['app', 'apple', 'application'].sort());
3432
expect(trie.findWords('ban')).toEqual(['banana']);
3533
expect(trie.findWords('cat')).toEqual([]);
3634
});

data-structures/union-find/unionFind.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ export class UnionFind<T> {
6161
*/
6262
union(element1: T, element2: T): void {
6363
if (!this.parent.has(element1) || !this.parent.has(element2)) {
64-
throw new Error(
65-
`One or both elements (${element1}, ${element2}) not found in any set`
66-
);
64+
throw new Error(`One or both elements (${element1}, ${element2}) not found in any set`);
6765
}
6866

6967
const root1 = this.find(element1);

0 commit comments

Comments
 (0)