Skip to content

Add and run prettier #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ jobs:
node-version: "18.x"
- run: npm ci
- run: npm test
- run: npm run check-style
2 changes: 0 additions & 2 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
tasks:
- init: |
npm install && npm test


2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.github
*.md
15 changes: 15 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"endOfLine": "lf",
"insertPragma": false,
"printWidth": 80,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
}
11 changes: 5 additions & 6 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript'
]
};

presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript'
]
}
20 changes: 10 additions & 10 deletions backtracking/all_combinations_of_size_k.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* and repeat the same process for the next number.
*/
export function generateCombinations(n: number, k: number): number[][] {
const combinationsAcc: number[][] = [];
const currentCombination: number[] = [];
const combinationsAcc: number[][] = []
const currentCombination: number[] = []

function generateAllCombos(
n: number,
Expand All @@ -20,19 +20,19 @@ export function generateCombinations(n: number, k: number): number[][] {
): number[][] {
if (k === 0) {
if (currentCombination.length > 0) {
combinationsAcc.push(currentCombination.slice());
combinationsAcc.push(currentCombination.slice())
}
return combinationsAcc;
return combinationsAcc
}

const endCursor = n - k + 2;
const endCursor = n - k + 2
for (let i = startCursor; i < endCursor; i++) {
currentCombination.push(i);
generateAllCombos(n, k - 1, i + 1);
currentCombination.pop();
currentCombination.push(i)
generateAllCombos(n, k - 1, i + 1)
currentCombination.pop()
}
return combinationsAcc;
return combinationsAcc
}

return generateAllCombos(n, k, 1);
return generateAllCombos(n, k, 1)
}
26 changes: 15 additions & 11 deletions backtracking/generateparentheses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
*/

const generateParentheses = (n: number): string[] => {
const result: string[] = [];
const result: string[] = []

const solve = (chars: string, openParentheses: number, closedParentheses: number) => {
const solve = (
chars: string,
openParentheses: number,
closedParentheses: number
) => {
if (openParentheses === n && closedParentheses === n) {
result.push(chars);
return;
result.push(chars)
return
}

if (openParentheses <= n) {
solve(chars + "(", openParentheses + 1, closedParentheses);
solve(chars + '(', openParentheses + 1, closedParentheses)
}

if (closedParentheses < openParentheses) {
solve(chars + ")", openParentheses, closedParentheses + 1);
solve(chars + ')', openParentheses, closedParentheses + 1)
}
};
}

solve("", 0, 0);
solve('', 0, 0)

return result;
};
return result
}

export { generateParentheses };
export { generateParentheses }
33 changes: 22 additions & 11 deletions backtracking/test/all_combinations_of_size_k.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateCombinations } from "../all_combinations_of_size_k";
import { generateCombinations } from '../all_combinations_of_size_k'

const cases = [
[
Expand All @@ -7,20 +7,31 @@ const cases = [
[
[1, 2],
[1, 3],
[2, 3]
]
],
[
4,
2,
[
[1, 2],
[1, 3],
[1, 4],
[2, 3],
],
[2, 4],
[3, 4]
]
],
[4, 2, [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]],
[0, 0, []],
[2, 3, []],
] as const;
[2, 3, []]
] as const

describe("AllCombinationsOfSizeK", () => {
describe('AllCombinationsOfSizeK', () => {
it.each(cases)(
"create all combinations given n=%p and k=%p",
'create all combinations given n=%p and k=%p',
(n, k, expectedCombos) => {
const combinations = generateCombinations(n, k);
expect(combinations).toEqual(expectedCombos);
const combinations = generateCombinations(n, k)
expect(combinations).toEqual(expectedCombos)
}
);
});
)
})
140 changes: 70 additions & 70 deletions backtracking/test/generateparentheses.test.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
import { generateParentheses } from "../generateparentheses";
import { generateParentheses } from '../generateparentheses'

const cases: [number, string[]][] = [
[0, [""]],
[1, ["()"]],
[2, ["(())", "()()"]],
[3, ["((()))", "(()())", "(())()", "()(())", "()()()"]],
[0, ['']],
[1, ['()']],
[2, ['(())', '()()']],
[3, ['((()))', '(()())', '(())()', '()(())', '()()()']],
[
4,
[
"(((())))",
"((()()))",
"((())())",
"((()))()",
"(()(()))",
"(()()())",
"(()())()",
"(())(())",
"(())()()",
"()((()))",
"()(()())",
"()(())()",
"()()(())",
"()()()()",
],
'(((())))',
'((()()))',
'((())())',
'((()))()',
'(()(()))',
'(()()())',
'(()())()',
'(())(())',
'(())()()',
'()((()))',
'()(()())',
'()(())()',
'()()(())',
'()()()()'
]
],
[
5,
[
"((((()))))",
"(((()())))",
"(((())()))",
"(((()))())",
"(((())))()",
"((()(())))",
"((()()()))",
"((()())())",
"((()()))()",
"((())(()))",
"((())()())",
"((())())()",
"((()))(())",
"((()))()()",
"(()((())))",
"(()(()()))",
"(()(())())",
"(()(()))()",
"(()()(()))",
"(()()()())",
"(()()())()",
"(()())(())",
"(()())()()",
"(())((()))",
"(())(()())",
"(())(())()",
"(())()(())",
"(())()()()",
"()(((())))",
"()((()()))",
"()((())())",
"()((()))()",
"()(()(()))",
"()(()()())",
"()(()())()",
"()(())(())",
"()(())()()",
"()()((()))",
"()()(()())",
"()()(())()",
"()()()(())",
"()()()()()",
],
],
];
'((((()))))',
'(((()())))',
'(((())()))',
'(((()))())',
'(((())))()',
'((()(())))',
'((()()()))',
'((()())())',
'((()()))()',
'((())(()))',
'((())()())',
'((())())()',
'((()))(())',
'((()))()()',
'(()((())))',
'(()(()()))',
'(()(())())',
'(()(()))()',
'(()()(()))',
'(()()()())',
'(()()())()',
'(()())(())',
'(()())()()',
'(())((()))',
'(())(()())',
'(())(())()',
'(())()(())',
'(())()()()',
'()(((())))',
'()((()()))',
'()((())())',
'()((()))()',
'()(()(()))',
'()(()()())',
'()(()())()',
'()(())(())',
'()(())()()',
'()()((()))',
'()()(()())',
'()()(())()',
'()()()(())',
'()()()()()'
]
]
]

describe("Generate Parentheses", () => {
describe('Generate Parentheses', () => {
test.each(cases)(
"generate all valid parentheses of input %n",
'generate all valid parentheses of input %n',
(n: number, expected: string[]) => {
expect(generateParentheses(n)).toStrictEqual(expected);
expect(generateParentheses(n)).toStrictEqual(expected)
}
);
});
)
})
37 changes: 21 additions & 16 deletions bit_manipulation/add_binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@
* @param secondBinaryNo - The second binary string.
* @returns The binary sum of the input strings.
*/
export function addBinary(firstBinaryNo: string, secondBinaryNo: string): string {
let lengthOfFirstNumber: number = firstBinaryNo.length - 1;
let lengthOfSecondNumber: number = secondBinaryNo.length - 1;
const solution: string[] = [];
let carry: number = 0;
export function addBinary(
firstBinaryNo: string,
secondBinaryNo: string
): string {
let lengthOfFirstNumber: number = firstBinaryNo.length - 1
let lengthOfSecondNumber: number = secondBinaryNo.length - 1
const solution: string[] = []
let carry: number = 0

while ( lengthOfFirstNumber >= 0 || lengthOfSecondNumber >= 0) {
let sum: number = carry;
if (lengthOfFirstNumber >= 0) sum += parseInt(firstBinaryNo.charAt(lengthOfFirstNumber));
if (lengthOfSecondNumber >= 0) sum += parseInt(secondBinaryNo.charAt(lengthOfSecondNumber));
solution.push((sum % 2).toString());
carry = Math.floor(sum / 2);
lengthOfFirstNumber--;
lengthOfSecondNumber--;
}
while (lengthOfFirstNumber >= 0 || lengthOfSecondNumber >= 0) {
let sum: number = carry
if (lengthOfFirstNumber >= 0)
sum += parseInt(firstBinaryNo.charAt(lengthOfFirstNumber))
if (lengthOfSecondNumber >= 0)
sum += parseInt(secondBinaryNo.charAt(lengthOfSecondNumber))
solution.push((sum % 2).toString())
carry = Math.floor(sum / 2)
lengthOfFirstNumber--
lengthOfSecondNumber--
}

if (carry !== 0) solution.push(carry.toString());
if (carry !== 0) solution.push(carry.toString())

return solution.reverse().join('');
return solution.reverse().join('')
}
2 changes: 1 addition & 1 deletion bit_manipulation/is_power_of_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
* @returns {boolean}
*/

export const isPowerOfTwo = (n: number): boolean => n > 0 && (n & (n - 1)) === 0
export const isPowerOfTwo = (n: number): boolean => n > 0 && (n & (n - 1)) === 0
3 changes: 2 additions & 1 deletion bit_manipulation/is_power_of_4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
* const result = isPowerOfFour(16); // Returns true (16 is 4^2)
* const result2 = isPowerOfFour(5); // Returns false (5 is not a power of four)
*/
export const isPowerOfFour = (n: number): boolean => ((n > 0) && ((n & n - 1) === 0) && (n % 3 === 1))
export const isPowerOfFour = (n: number): boolean =>
n > 0 && (n & (n - 1)) === 0 && n % 3 === 1
Loading