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 {

0 commit comments

Comments
 (0)