Skip to content

Commit c2d7aa6

Browse files
authored
Add and run prettier (TheAlgorithms#234)
* chore: add `prettier` * style: run `prettier` * chore: check formatting in CI
1 parent c5b12db commit c2d7aa6

File tree

205 files changed

+11151
-10755
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

205 files changed

+11151
-10755
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ jobs:
1616
node-version: "18.x"
1717
- run: npm ci
1818
- run: npm test
19+
- run: npm run check-style

.gitpod.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
tasks:
22
- init: |
33
npm install && npm test
4-
5-

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.github
2+
*.md

.prettierrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"arrowParens": "always",
3+
"bracketSpacing": true,
4+
"endOfLine": "lf",
5+
"insertPragma": false,
6+
"printWidth": 80,
7+
"proseWrap": "preserve",
8+
"quoteProps": "as-needed",
9+
"requirePragma": false,
10+
"semi": false,
11+
"singleQuote": true,
12+
"tabWidth": 2,
13+
"trailingComma": "none",
14+
"useTabs": false
15+
}

babel.config.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = {
2-
presets: [
3-
['@babel/preset-env', { targets: { node: 'current' } }],
4-
'@babel/preset-typescript'
5-
]
6-
};
7-
2+
presets: [
3+
['@babel/preset-env', { targets: { node: 'current' } }],
4+
'@babel/preset-typescript'
5+
]
6+
}

backtracking/all_combinations_of_size_k.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* and repeat the same process for the next number.
1111
*/
1212
export function generateCombinations(n: number, k: number): number[][] {
13-
const combinationsAcc: number[][] = [];
14-
const currentCombination: number[] = [];
13+
const combinationsAcc: number[][] = []
14+
const currentCombination: number[] = []
1515

1616
function generateAllCombos(
1717
n: number,
@@ -20,19 +20,19 @@ export function generateCombinations(n: number, k: number): number[][] {
2020
): number[][] {
2121
if (k === 0) {
2222
if (currentCombination.length > 0) {
23-
combinationsAcc.push(currentCombination.slice());
23+
combinationsAcc.push(currentCombination.slice())
2424
}
25-
return combinationsAcc;
25+
return combinationsAcc
2626
}
2727

28-
const endCursor = n - k + 2;
28+
const endCursor = n - k + 2
2929
for (let i = startCursor; i < endCursor; i++) {
30-
currentCombination.push(i);
31-
generateAllCombos(n, k - 1, i + 1);
32-
currentCombination.pop();
30+
currentCombination.push(i)
31+
generateAllCombos(n, k - 1, i + 1)
32+
currentCombination.pop()
3333
}
34-
return combinationsAcc;
34+
return combinationsAcc
3535
}
3636

37-
return generateAllCombos(n, k, 1);
37+
return generateAllCombos(n, k, 1)
3838
}

backtracking/generateparentheses.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@
66
*/
77

88
const generateParentheses = (n: number): string[] => {
9-
const result: string[] = [];
9+
const result: string[] = []
1010

11-
const solve = (chars: string, openParentheses: number, closedParentheses: number) => {
11+
const solve = (
12+
chars: string,
13+
openParentheses: number,
14+
closedParentheses: number
15+
) => {
1216
if (openParentheses === n && closedParentheses === n) {
13-
result.push(chars);
14-
return;
17+
result.push(chars)
18+
return
1519
}
1620

1721
if (openParentheses <= n) {
18-
solve(chars + "(", openParentheses + 1, closedParentheses);
22+
solve(chars + '(', openParentheses + 1, closedParentheses)
1923
}
2024

2125
if (closedParentheses < openParentheses) {
22-
solve(chars + ")", openParentheses, closedParentheses + 1);
26+
solve(chars + ')', openParentheses, closedParentheses + 1)
2327
}
24-
};
28+
}
2529

26-
solve("", 0, 0);
30+
solve('', 0, 0)
2731

28-
return result;
29-
};
32+
return result
33+
}
3034

31-
export { generateParentheses };
35+
export { generateParentheses }
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { generateCombinations } from "../all_combinations_of_size_k";
1+
import { generateCombinations } from '../all_combinations_of_size_k'
22

33
const cases = [
44
[
@@ -7,20 +7,31 @@ const cases = [
77
[
88
[1, 2],
99
[1, 3],
10+
[2, 3]
11+
]
12+
],
13+
[
14+
4,
15+
2,
16+
[
17+
[1, 2],
18+
[1, 3],
19+
[1, 4],
1020
[2, 3],
11-
],
21+
[2, 4],
22+
[3, 4]
23+
]
1224
],
13-
[4, 2, [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]],
1425
[0, 0, []],
15-
[2, 3, []],
16-
] as const;
26+
[2, 3, []]
27+
] as const
1728

18-
describe("AllCombinationsOfSizeK", () => {
29+
describe('AllCombinationsOfSizeK', () => {
1930
it.each(cases)(
20-
"create all combinations given n=%p and k=%p",
31+
'create all combinations given n=%p and k=%p',
2132
(n, k, expectedCombos) => {
22-
const combinations = generateCombinations(n, k);
23-
expect(combinations).toEqual(expectedCombos);
33+
const combinations = generateCombinations(n, k)
34+
expect(combinations).toEqual(expectedCombos)
2435
}
25-
);
26-
});
36+
)
37+
})
+70-70
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
1-
import { generateParentheses } from "../generateparentheses";
1+
import { generateParentheses } from '../generateparentheses'
22

33
const cases: [number, string[]][] = [
4-
[0, [""]],
5-
[1, ["()"]],
6-
[2, ["(())", "()()"]],
7-
[3, ["((()))", "(()())", "(())()", "()(())", "()()()"]],
4+
[0, ['']],
5+
[1, ['()']],
6+
[2, ['(())', '()()']],
7+
[3, ['((()))', '(()())', '(())()', '()(())', '()()()']],
88
[
99
4,
1010
[
11-
"(((())))",
12-
"((()()))",
13-
"((())())",
14-
"((()))()",
15-
"(()(()))",
16-
"(()()())",
17-
"(()())()",
18-
"(())(())",
19-
"(())()()",
20-
"()((()))",
21-
"()(()())",
22-
"()(())()",
23-
"()()(())",
24-
"()()()()",
25-
],
11+
'(((())))',
12+
'((()()))',
13+
'((())())',
14+
'((()))()',
15+
'(()(()))',
16+
'(()()())',
17+
'(()())()',
18+
'(())(())',
19+
'(())()()',
20+
'()((()))',
21+
'()(()())',
22+
'()(())()',
23+
'()()(())',
24+
'()()()()'
25+
]
2626
],
2727
[
2828
5,
2929
[
30-
"((((()))))",
31-
"(((()())))",
32-
"(((())()))",
33-
"(((()))())",
34-
"(((())))()",
35-
"((()(())))",
36-
"((()()()))",
37-
"((()())())",
38-
"((()()))()",
39-
"((())(()))",
40-
"((())()())",
41-
"((())())()",
42-
"((()))(())",
43-
"((()))()()",
44-
"(()((())))",
45-
"(()(()()))",
46-
"(()(())())",
47-
"(()(()))()",
48-
"(()()(()))",
49-
"(()()()())",
50-
"(()()())()",
51-
"(()())(())",
52-
"(()())()()",
53-
"(())((()))",
54-
"(())(()())",
55-
"(())(())()",
56-
"(())()(())",
57-
"(())()()()",
58-
"()(((())))",
59-
"()((()()))",
60-
"()((())())",
61-
"()((()))()",
62-
"()(()(()))",
63-
"()(()()())",
64-
"()(()())()",
65-
"()(())(())",
66-
"()(())()()",
67-
"()()((()))",
68-
"()()(()())",
69-
"()()(())()",
70-
"()()()(())",
71-
"()()()()()",
72-
],
73-
],
74-
];
30+
'((((()))))',
31+
'(((()())))',
32+
'(((())()))',
33+
'(((()))())',
34+
'(((())))()',
35+
'((()(())))',
36+
'((()()()))',
37+
'((()())())',
38+
'((()()))()',
39+
'((())(()))',
40+
'((())()())',
41+
'((())())()',
42+
'((()))(())',
43+
'((()))()()',
44+
'(()((())))',
45+
'(()(()()))',
46+
'(()(())())',
47+
'(()(()))()',
48+
'(()()(()))',
49+
'(()()()())',
50+
'(()()())()',
51+
'(()())(())',
52+
'(()())()()',
53+
'(())((()))',
54+
'(())(()())',
55+
'(())(())()',
56+
'(())()(())',
57+
'(())()()()',
58+
'()(((())))',
59+
'()((()()))',
60+
'()((())())',
61+
'()((()))()',
62+
'()(()(()))',
63+
'()(()()())',
64+
'()(()())()',
65+
'()(())(())',
66+
'()(())()()',
67+
'()()((()))',
68+
'()()(()())',
69+
'()()(())()',
70+
'()()()(())',
71+
'()()()()()'
72+
]
73+
]
74+
]
7575

76-
describe("Generate Parentheses", () => {
76+
describe('Generate Parentheses', () => {
7777
test.each(cases)(
78-
"generate all valid parentheses of input %n",
78+
'generate all valid parentheses of input %n',
7979
(n: number, expected: string[]) => {
80-
expect(generateParentheses(n)).toStrictEqual(expected);
80+
expect(generateParentheses(n)).toStrictEqual(expected)
8181
}
82-
);
83-
});
82+
)
83+
})

bit_manipulation/add_binary.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@
55
* @param secondBinaryNo - The second binary string.
66
* @returns The binary sum of the input strings.
77
*/
8-
export function addBinary(firstBinaryNo: string, secondBinaryNo: string): string {
9-
let lengthOfFirstNumber: number = firstBinaryNo.length - 1;
10-
let lengthOfSecondNumber: number = secondBinaryNo.length - 1;
11-
const solution: string[] = [];
12-
let carry: number = 0;
8+
export function addBinary(
9+
firstBinaryNo: string,
10+
secondBinaryNo: string
11+
): string {
12+
let lengthOfFirstNumber: number = firstBinaryNo.length - 1
13+
let lengthOfSecondNumber: number = secondBinaryNo.length - 1
14+
const solution: string[] = []
15+
let carry: number = 0
1316

14-
while ( lengthOfFirstNumber >= 0 || lengthOfSecondNumber >= 0) {
15-
let sum: number = carry;
16-
if (lengthOfFirstNumber >= 0) sum += parseInt(firstBinaryNo.charAt(lengthOfFirstNumber));
17-
if (lengthOfSecondNumber >= 0) sum += parseInt(secondBinaryNo.charAt(lengthOfSecondNumber));
18-
solution.push((sum % 2).toString());
19-
carry = Math.floor(sum / 2);
20-
lengthOfFirstNumber--;
21-
lengthOfSecondNumber--;
22-
}
17+
while (lengthOfFirstNumber >= 0 || lengthOfSecondNumber >= 0) {
18+
let sum: number = carry
19+
if (lengthOfFirstNumber >= 0)
20+
sum += parseInt(firstBinaryNo.charAt(lengthOfFirstNumber))
21+
if (lengthOfSecondNumber >= 0)
22+
sum += parseInt(secondBinaryNo.charAt(lengthOfSecondNumber))
23+
solution.push((sum % 2).toString())
24+
carry = Math.floor(sum / 2)
25+
lengthOfFirstNumber--
26+
lengthOfSecondNumber--
27+
}
2328

24-
if (carry !== 0) solution.push(carry.toString());
29+
if (carry !== 0) solution.push(carry.toString())
2530

26-
return solution.reverse().join('');
31+
return solution.reverse().join('')
2732
}

bit_manipulation/is_power_of_2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
* @returns {boolean}
2323
*/
2424

25-
export const isPowerOfTwo = (n: number): boolean => n > 0 && (n & (n - 1)) === 0
25+
export const isPowerOfTwo = (n: number): boolean => n > 0 && (n & (n - 1)) === 0

bit_manipulation/is_power_of_4.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
* const result = isPowerOfFour(16); // Returns true (16 is 4^2)
1313
* const result2 = isPowerOfFour(5); // Returns false (5 is not a power of four)
1414
*/
15-
export const isPowerOfFour = (n: number): boolean => ((n > 0) && ((n & n - 1) === 0) && (n % 3 === 1))
15+
export const isPowerOfFour = (n: number): boolean =>
16+
n > 0 && (n & (n - 1)) === 0 && n % 3 === 1

0 commit comments

Comments
 (0)