diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..dc3d677 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,7 @@ +.github +.nyc_output +node_modules +coverage +pnpm-lock.yaml +CODE_OF_CONDUCT.md +LICENSE diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..e69de29 diff --git a/eslint.config.js b/eslint.config.js index 9db1bea..a567be9 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -2,10 +2,18 @@ import js from "@eslint/js"; import globals from "globals"; import tseslint from "typescript-eslint"; import { defineConfig } from "eslint/config"; - +import eslintConfigPrettier from "eslint-config-prettier/flat"; export default defineConfig([ - { files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], plugins: { js }, extends: ["js/recommended"] }, - { files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], languageOptions: { globals: globals.browser } }, + { + files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], + plugins: { js }, + extends: ["js/recommended"], + }, + { + files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], + languageOptions: { globals: globals.browser }, + }, tseslint.configs.recommended, + eslintConfigPrettier, ]); diff --git a/package.json b/package.json index d6a8488..ed45dcf 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "clean": "rm -rf .nyc_output coverage dist", "test": "nyc mocha 'src/**/*.test.ts' --require=tsx", "lint": "eslint 'src/**/*.ts'", + "prettier": "prettier --write .", "coverage": "nyc report --reporter html && open coverage/index.html", "coverage:check": "nyc check-coverage --lines 90 --branches 80 --statements 90" }, @@ -18,9 +19,11 @@ "@types/sinon": "^17.0.4", "chai": "^5.2.1", "eslint": "^9.31.0", + "eslint-config-prettier": "^10.1.5", "globals": "^16.3.0", "mocha": "^11.7.1", "nyc": "^17.1.0", + "prettier": "3.6.2", "sinon": "^21.0.0", "tsx": "^4.20.3", "typescript": "^5.8.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f37e2ad..db05b87 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,9 @@ importers: eslint: specifier: ^9.31.0 version: 9.31.0 + eslint-config-prettier: + specifier: ^10.1.5 + version: 10.1.5(eslint@9.31.0) globals: specifier: ^16.3.0 version: 16.3.0 @@ -35,6 +38,9 @@ importers: nyc: specifier: ^17.1.0 version: 17.1.0 + prettier: + specifier: 3.6.2 + version: 3.6.2 sinon: specifier: ^21.0.0 version: 21.0.0 @@ -665,6 +671,12 @@ packages: resolution: {integrity: sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==} engines: {node: '>=10'} + eslint-config-prettier@10.1.5: + resolution: {integrity: sha512-zc1UmCpNltmVY34vuLRV61r1K27sWuX39E+uyUnY8xS2Bex88VV9cugG+UZbRSRGtGyFboj+D8JODyme1plMpw==} + hasBin: true + peerDependencies: + eslint: '>=7.0.0' + eslint-scope@8.4.0: resolution: {integrity: sha512-sNXOfKCn74rt8RICKMvJS7XKV/Xk9kA7DyJr8mJik3S7Cwgy3qlkkmyS2uQB3jiJg6VNdZd/pDBJu0nvG2NlTg==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} @@ -1129,6 +1141,11 @@ packages: resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==} engines: {node: '>= 0.8.0'} + prettier@3.6.2: + resolution: {integrity: sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==} + engines: {node: '>=14'} + hasBin: true + process-on-spawn@1.1.0: resolution: {integrity: sha512-JOnOPQ/8TZgjs1JIH/m9ni7FfimjNa/PRx7y/Wb5qdItsnhO0jE4AT7fC0HjC28DUQWDr50dwSYZLdRMlqDq3Q==} engines: {node: '>=8'} @@ -2002,6 +2019,10 @@ snapshots: escape-string-regexp@4.0.0: {} + eslint-config-prettier@10.1.5(eslint@9.31.0): + dependencies: + eslint: 9.31.0 + eslint-scope@8.4.0: dependencies: esrecurse: 4.3.0 @@ -2504,6 +2525,8 @@ snapshots: prelude-ls@1.2.1: {} + prettier@3.6.2: {} + process-on-spawn@1.1.0: dependencies: fromentries: 1.3.2 diff --git a/src/algorithms/dynamic-programming/fibonacci.test.ts b/src/algorithms/dynamic-programming/fibonacci.test.ts index f0df5f6..4a3e646 100644 --- a/src/algorithms/dynamic-programming/fibonacci.test.ts +++ b/src/algorithms/dynamic-programming/fibonacci.test.ts @@ -1,5 +1,5 @@ -import { expect } from 'chai'; -import fibonacci from './fibonacci.ts'; +import { expect } from "chai"; +import fibonacci from "./fibonacci.ts"; describe("fibonacci", function () { const tests = [ diff --git a/src/algorithms/dynamic-programming/grid-traveller.test.ts b/src/algorithms/dynamic-programming/grid-traveller.test.ts index 0176be1..4e82d2f 100644 --- a/src/algorithms/dynamic-programming/grid-traveller.test.ts +++ b/src/algorithms/dynamic-programming/grid-traveller.test.ts @@ -1,7 +1,7 @@ -import { expect } from 'chai'; -import gridTraveller from './grid-traveller.ts'; +import { expect } from "chai"; +import gridTraveller from "./grid-traveller.ts"; -describe('gridTraveller', () => { +describe("gridTraveller", () => { const tests = [ { rows: 0, columns: 0, expected: 0 }, { rows: 0, columns: 1, expected: 0 }, diff --git a/src/algorithms/dynamic-programming/grid-traveller.ts b/src/algorithms/dynamic-programming/grid-traveller.ts index bdeb748..3582c06 100644 --- a/src/algorithms/dynamic-programming/grid-traveller.ts +++ b/src/algorithms/dynamic-programming/grid-traveller.ts @@ -24,7 +24,11 @@ * @param buffer the object used for memoisation * @returns the total number of ways to travel on a N by M grid */ -export default function gridTraveller(rows: number, columns: number, buffer: object = {}): number { +export default function gridTraveller( + rows: number, + columns: number, + buffer: object = {}, +): number { const key = getKey(rows, columns); if (key in buffer) return buffer[key]; if (rows <= 0 || columns <= 0) return 0; diff --git a/src/algorithms/dynamic-programming/sum/can-sum.test.ts b/src/algorithms/dynamic-programming/sum/can-sum.test.ts index dcee154..cce02b0 100644 --- a/src/algorithms/dynamic-programming/sum/can-sum.test.ts +++ b/src/algorithms/dynamic-programming/sum/can-sum.test.ts @@ -1,5 +1,5 @@ -import { expect } from 'chai'; -import canSum from './can-sum.ts'; +import { expect } from "chai"; +import canSum from "./can-sum.ts"; describe("canSum", function () { const tests = [ diff --git a/src/algorithms/dynamic-programming/sum/can-sum.ts b/src/algorithms/dynamic-programming/sum/can-sum.ts index 8e06fa2..95d7022 100644 --- a/src/algorithms/dynamic-programming/sum/can-sum.ts +++ b/src/algorithms/dynamic-programming/sum/can-sum.ts @@ -23,7 +23,11 @@ * @param buffer the object used for memoisation * @returns true if the target sun can be constructed */ -export default function canSum(targetSum: number, numbers: number[], buffer: object = {}): boolean { +export default function canSum( + targetSum: number, + numbers: number[], + buffer: object = {}, +): boolean { if (targetSum in buffer) return buffer[targetSum]; if (targetSum === 0) return true; if (targetSum < 0) return false; diff --git a/src/algorithms/dynamic-programming/sum/how-sum.test.ts b/src/algorithms/dynamic-programming/sum/how-sum.test.ts index 8f32b85..d6f0955 100644 --- a/src/algorithms/dynamic-programming/sum/how-sum.test.ts +++ b/src/algorithms/dynamic-programming/sum/how-sum.test.ts @@ -1,5 +1,5 @@ -import { expect } from 'chai'; -import howSum from './how-sum.ts'; +import { expect } from "chai"; +import howSum from "./how-sum.ts"; describe("howSum", function () { const tests = [ diff --git a/src/algorithms/dynamic-programming/sum/shortest-sum.test.ts b/src/algorithms/dynamic-programming/sum/shortest-sum.test.ts index a899e25..0daba55 100644 --- a/src/algorithms/dynamic-programming/sum/shortest-sum.test.ts +++ b/src/algorithms/dynamic-programming/sum/shortest-sum.test.ts @@ -1,5 +1,5 @@ -import { expect } from 'chai'; -import shortestSum from './shortest-sum.ts'; +import { expect } from "chai"; +import shortestSum from "./shortest-sum.ts"; describe("shortestSum", function () { const tests = [ diff --git a/src/algorithms/matrices.test.ts b/src/algorithms/matrices.test.ts index b46f3bb..c6233e5 100644 --- a/src/algorithms/matrices.test.ts +++ b/src/algorithms/matrices.test.ts @@ -1,5 +1,5 @@ -import { expect } from 'chai'; -import { sum, sumDiagonal, sumAntiDiagonal, sumFrame } from './matrices.js'; +import { expect } from "chai"; +import { sum, sumDiagonal, sumAntiDiagonal, sumFrame } from "./matrices.js"; describe("Matrix Operations", function () { const matrix = [ @@ -9,19 +9,19 @@ describe("Matrix Operations", function () { [6, 8, 2, 3], ]; - it('sum', () => { + it("sum", () => { expect(sum(matrix)).equal(73); }); - it('sumDiagonal', () => { + it("sumDiagonal", () => { expect(sumDiagonal(matrix)).equal(21); }); - it('sumAntiDiagonal', () => { + it("sumAntiDiagonal", () => { expect(sumAntiDiagonal(matrix)).equal(19); }); - it('sumFrame', () => { + it("sumFrame", () => { expect(sumFrame(matrix)).equal(49); }); }); diff --git a/src/algorithms/search.test.ts b/src/algorithms/search.test.ts index da73784..ed47b8e 100644 --- a/src/algorithms/search.test.ts +++ b/src/algorithms/search.test.ts @@ -1,26 +1,30 @@ -import { expect } from 'chai'; -import { linearSearch, binarySearch } from '@src/algorithms/search.ts'; +import { expect } from "chai"; +import { linearSearch, binarySearch } from "@src/algorithms/search.ts"; -describe('Search', () => { - const unsortedSequence = [11, 3, 10, 9, 1, 23, 12, 7, 113, 39, 2, 948, 5, 82, 49, 33]; - const sortedSequence = [1, 2, 3, 5, 7, 9, 10, 11, 12, 23, 33, 39, 49, 82, 113, 948]; +describe("Search", () => { + const unsortedSequence = [ + 11, 3, 10, 9, 1, 23, 12, 7, 113, 39, 2, 948, 5, 82, 49, 33, + ]; + const sortedSequence = [ + 1, 2, 3, 5, 7, 9, 10, 11, 12, 23, 33, 39, 49, 82, 113, 948, + ]; - describe('Linear Search', () => { - it('should find 11', () => { + describe("Linear Search", () => { + it("should find 11", () => { expect(linearSearch(11, unsortedSequence)).equal(true); }); - it('should not find 0', () => { + it("should not find 0", () => { expect(linearSearch(0, unsortedSequence)).equal(false); }); }); - describe('Binary Search', () => { - it('should find 11', () => { + describe("Binary Search", () => { + it("should find 11", () => { expect(binarySearch(11, sortedSequence)).equal(true); }); - it('should not find 0', () => { + it("should not find 0", () => { expect(binarySearch(0, sortedSequence)).equal(false); }); }); diff --git a/src/algorithms/sorting/merge-sort.test.ts b/src/algorithms/sorting/merge-sort.test.ts index 1dd5cbc..aca27ae 100644 --- a/src/algorithms/sorting/merge-sort.test.ts +++ b/src/algorithms/sorting/merge-sort.test.ts @@ -1,6 +1,6 @@ -import { expect } from 'chai'; -import mergeSort from '@src/algorithms/sorting/merge-sort.ts'; -import SortingOrder from '@src/util/SortingOrder.ts'; +import { expect } from "chai"; +import mergeSort from "@src/algorithms/sorting/merge-sort.ts"; +import SortingOrder from "@src/util/SortingOrder.ts"; describe("mergeSort", function () { const tests = [ diff --git a/src/algorithms/sorting/merge-sort.ts b/src/algorithms/sorting/merge-sort.ts index cbd998c..c81ccad 100644 --- a/src/algorithms/sorting/merge-sort.ts +++ b/src/algorithms/sorting/merge-sort.ts @@ -1,4 +1,4 @@ -import SortingOrder from '../../util/SortingOrder'; +import SortingOrder from "../../util/SortingOrder"; interface Split { left: Array; @@ -31,7 +31,11 @@ function whoGoesFirst(x: number, y: number, order: SortingOrder): Operand { return Operand.Left; } -function merge(left: Array, right: Array, order: SortingOrder): Array { +function merge( + left: Array, + right: Array, + order: SortingOrder, +): Array { let leftIndex = 0; let rightIndex = 0; @@ -40,7 +44,9 @@ function merge(left: Array, right: Array, order: SortingOrder): // while the left index or the right index are not out of bound while (leftIndex < left.length || rightIndex < right.length) { if (leftIndex < left.length && rightIndex < right.length) { - if (whoGoesFirst(left[leftIndex], right[rightIndex], order) === Operand.Left) { + if ( + whoGoesFirst(left[leftIndex], right[rightIndex], order) === Operand.Left + ) { sortedArray.push(left[leftIndex]); leftIndex += 1; } else { @@ -67,7 +73,10 @@ function merge(left: Array, right: Array, order: SortingOrder): * @param input the array of numbers to sort. * @param order specify the order of sorting: ascending or descending. */ -export default function mergeSort(input: Array | null, order: SortingOrder): Array | null { +export default function mergeSort( + input: Array | null, + order: SortingOrder, +): Array | null { if (!Array.isArray(input) || input.length <= 1) return input; const splitInput: Split = split(input); diff --git a/src/algorithms/tree-traversal.test.ts b/src/algorithms/tree-traversal.test.ts index 16d91ca..390c426 100644 --- a/src/algorithms/tree-traversal.test.ts +++ b/src/algorithms/tree-traversal.test.ts @@ -1,4 +1,4 @@ -import { expect } from 'chai'; +import { expect } from "chai"; import { recursivePreOrderTraversal, iterativePreOrderTraversal, @@ -9,8 +9,8 @@ import { recursivePostOrderTraversal, iterativePostOrderTraversal, breadthFirstTraversal, -} from '@src/algorithms/tree-traversal.ts'; -import BinaryTree from '@src/data-structures/BinaryTree.ts'; +} from "@src/algorithms/tree-traversal.ts"; +import BinaryTree from "@src/data-structures/BinaryTree.ts"; /* Given the following rooted tree @@ -29,7 +29,7 @@ import BinaryTree from '@src/data-structures/BinaryTree.ts'; use DFS and BFS to traverse it */ -describe('Tree Traversal Algorithms', () => { +describe("Tree Traversal Algorithms", () => { const preOrder = [1, 2, 4, 3, 5, 7, 8, 6]; const inOrder = [4, 2, 1, 7, 5, 8, 3, 6]; const postOrder = [4, 2, 7, 8, 5, 6, 3, 1]; @@ -48,60 +48,64 @@ describe('Tree Traversal Algorithms', () => { tree.rightChild.leftChild.leftChild = new BinaryTree(7); tree.rightChild.leftChild.rightChild = new BinaryTree(8); - describe('Depth-first Approach', () => { - describe('Pre-order Traversal', () => { - describe('Recursive Algorithm', () => { - it('recursivePreOrderTraversal', () => { + describe("Depth-first Approach", () => { + describe("Pre-order Traversal", () => { + describe("Recursive Algorithm", () => { + it("recursivePreOrderTraversal", () => { expect(recursivePreOrderTraversal(tree)).to.deep.equal(preOrder); }); }); - describe('Iterative Algorithm', () => { - it('iterativePreOrderTraversal', () => { + describe("Iterative Algorithm", () => { + it("iterativePreOrderTraversal", () => { expect(iterativePreOrderTraversal(tree)).to.deep.equal(preOrder); }); - it('iterativePreOrderTraversalRightChildOnly', () => { - expect(iterativePreOrderTraversalRightChildOnly(tree)).to.deep.equal(preOrder); + it("iterativePreOrderTraversalRightChildOnly", () => { + expect(iterativePreOrderTraversalRightChildOnly(tree)).to.deep.equal( + preOrder, + ); }); }); }); - describe('In-order Traversal', () => { - describe('Recursive Algorithm', () => { - it('recursiveInOrderTraversal', () => { + describe("In-order Traversal", () => { + describe("Recursive Algorithm", () => { + it("recursiveInOrderTraversal", () => { expect(recursiveInOrderTraversal(tree)).to.deep.equal(inOrder); }); }); - describe('Iterative Algorithm', () => { - it('iterativeInOrderTraversal', () => { + describe("Iterative Algorithm", () => { + it("iterativeInOrderTraversal", () => { expect(iterativeInOrderTraversal(tree)).to.deep.equal(inOrder); }); - it('iterativeInOrderTraversalWithDoubleWhile', () => { - expect(iterativeInOrderTraversalWithDoubleWhile(tree)).to.deep.equal(inOrder); + it("iterativeInOrderTraversalWithDoubleWhile", () => { + expect(iterativeInOrderTraversalWithDoubleWhile(tree)).to.deep.equal( + inOrder, + ); }); }); }); - describe('Post-order Traversal', () => { - describe('Recursive Algorithm', () => { - it('recursivePostOrderTraversal', () => { + describe("Post-order Traversal", () => { + describe("Recursive Algorithm", () => { + it("recursivePostOrderTraversal", () => { expect(recursivePostOrderTraversal(tree)).to.deep.equal(postOrder); }); }); - describe('Iterative Algorithm', () => { - it('iterativePostOrderTraversal', () => { + describe("Iterative Algorithm", () => { + it("iterativePostOrderTraversal", () => { expect(iterativePostOrderTraversal(tree)).to.deep.equal(postOrder); }); }); }); }); - describe('Breadth-first traversal', () => { - it('breadthFirstTraversal', () => { + describe("Breadth-first traversal", () => { + it("breadthFirstTraversal", () => { expect(breadthFirstTraversal(tree)).to.deep.equal(bfs); }); }); diff --git a/src/algorithms/tree-traversal.ts b/src/algorithms/tree-traversal.ts index 70e03ac..07df863 100644 --- a/src/algorithms/tree-traversal.ts +++ b/src/algorithms/tree-traversal.ts @@ -1,4 +1,4 @@ -import BinaryTree from '../data-structures/BinaryTree'; +import BinaryTree from "../data-structures/BinaryTree"; /** * Depth-First traversal (DFS) @@ -47,7 +47,9 @@ export function iterativePreOrderTraversal(node: BinaryTree): T[] { * Time Complexity: O(N) * Auxiliary Space: O(H), where H is the height of the tree. */ -export function iterativePreOrderTraversalRightChildOnly(node: BinaryTree): T[] { +export function iterativePreOrderTraversalRightChildOnly( + node: BinaryTree, +): T[] { const result = []; if (node === null) return result; @@ -110,7 +112,9 @@ export function iterativeInOrderTraversal(node: BinaryTree): T[] { return result; } -export function iterativeInOrderTraversalWithDoubleWhile(node: BinaryTree): T[] { +export function iterativeInOrderTraversalWithDoubleWhile( + node: BinaryTree, +): T[] { const result = []; if (node === null) return result; @@ -162,7 +166,10 @@ export function iterativePostOrderTraversal(node: BinaryTree): T[] { while (current !== null && !visited.has(current)) { if (current.leftChild !== null && !visited.has(current.leftChild)) { current = current.leftChild; - } else if (current.rightChild !== null && !visited.has(current.rightChild)) { + } else if ( + current.rightChild !== null && + !visited.has(current.rightChild) + ) { current = current.rightChild; } else { result.push(current.value); diff --git a/src/data-structures/collections/NumericMaxHeap.ts b/src/data-structures/collections/NumericMaxHeap.ts index 7da5475..33e62ed 100644 --- a/src/data-structures/collections/NumericMaxHeap.ts +++ b/src/data-structures/collections/NumericMaxHeap.ts @@ -21,7 +21,10 @@ export default class NumericMaxHeap { // If the current node is a leaf, exit if (this.isLeaf(current)) return; // Get the max child - const maxChild = this.getMax(this.getLeftChild(current), this.getRightChild(current)); + const maxChild = this.getMax( + this.getLeftChild(current), + this.getRightChild(current), + ); // If the current node is smaller than the max child if (this.heap[current] < this.heap[maxChild]) { // swap positio between the current node and its child diff --git a/src/data-structures/collections/NumericMinHeap.ts b/src/data-structures/collections/NumericMinHeap.ts index aaf0213..d5008e3 100644 --- a/src/data-structures/collections/NumericMinHeap.ts +++ b/src/data-structures/collections/NumericMinHeap.ts @@ -21,7 +21,10 @@ export default class NumericMinHeap { // If the current node is a leaf, exit if (this.isLeaf(current)) return; // Get the min child - const minChild = this.getMin(this.getLeftChild(current), this.getRightChild(current)); + const minChild = this.getMin( + this.getLeftChild(current), + this.getRightChild(current), + ); // If the current node is smaller than the max child if (this.heap[current] > this.heap[minChild]) { // swap positio between the current node and its child diff --git a/src/data-structures/collections/PriorityQueueArray.ts b/src/data-structures/collections/PriorityQueueArray.ts index f4ef84e..4be03c3 100644 --- a/src/data-structures/collections/PriorityQueueArray.ts +++ b/src/data-structures/collections/PriorityQueueArray.ts @@ -1,4 +1,4 @@ -import QueueArray from './QueueArray.js'; +import QueueArray from "./QueueArray.js"; export default class PriorityQueueArray extends QueueArray { constructor() { diff --git a/src/data-structures/collections/Queue.ts b/src/data-structures/collections/Queue.ts index 2633c61..f519a35 100644 --- a/src/data-structures/collections/Queue.ts +++ b/src/data-structures/collections/Queue.ts @@ -1,4 +1,4 @@ -import Collection from './Collection'; +import Collection from "./Collection"; /** * The {@code Queue} interface represents a first-in-first-out diff --git a/src/data-structures/collections/QueueArray.ts b/src/data-structures/collections/QueueArray.ts index 432775b..9a13ba3 100644 --- a/src/data-structures/collections/QueueArray.ts +++ b/src/data-structures/collections/QueueArray.ts @@ -1,4 +1,4 @@ -import Queue from './Queue'; +import Queue from "./Queue"; export default class QueueArray implements Queue { protected queue: Array; diff --git a/src/data-structures/collections/Stack.ts b/src/data-structures/collections/Stack.ts index da3e1d6..22b6470 100644 --- a/src/data-structures/collections/Stack.ts +++ b/src/data-structures/collections/Stack.ts @@ -1,4 +1,4 @@ -import Collection from './Collection'; +import Collection from "./Collection"; /** * The {@code Stack} interface represents a last-in-first-out diff --git a/src/data-structures/collections/StackArray.ts b/src/data-structures/collections/StackArray.ts index c2d91cb..4b524d3 100644 --- a/src/data-structures/collections/StackArray.ts +++ b/src/data-structures/collections/StackArray.ts @@ -1,4 +1,4 @@ -import Stack from './Stack'; +import Stack from "./Stack"; export default class StackArray implements Stack { private stack: Array; diff --git a/src/exercises/majority-elements/index.test.ts b/src/exercises/majority-elements/index.test.ts index 62d8bd3..c446cfc 100644 --- a/src/exercises/majority-elements/index.test.ts +++ b/src/exercises/majority-elements/index.test.ts @@ -1,26 +1,26 @@ -import { expect } from 'chai'; -import { majority } from '@src/exercises/majority-elements/index.ts'; +import { expect } from "chai"; +import { majority } from "@src/exercises/majority-elements/index.ts"; -describe('Majority Elements Exercise', () => { - it('majority()', () => { +describe("Majority Elements Exercise", () => { + it("majority()", () => { expect(majority([1, 2, 2, 2, 3])).equal(2); }); - it('majority() with empty array', () => { + it("majority() with empty array", () => { // eslint-disable-next-line @typescript-eslint/no-unused-expressions expect(majority([])).to.be.undefined; }); - it('majority() with no majority element', () => { + it("majority() with no majority element", () => { // eslint-disable-next-line @typescript-eslint/no-unused-expressions expect(majority([1, 2, 3, 4])).to.be.undefined; }); - it('majority() with single element', () => { + it("majority() with single element", () => { expect(majority([1])).equal(1); }); - it('majority() with all same elements', () => { + it("majority() with all same elements", () => { expect(majority([2, 2, 2, 2])).equal(2); }); }); diff --git a/src/exercises/palindrome/index.test.ts b/src/exercises/palindrome/index.test.ts index 774773d..d40baa8 100644 --- a/src/exercises/palindrome/index.test.ts +++ b/src/exercises/palindrome/index.test.ts @@ -1,30 +1,30 @@ -import { expect } from 'chai'; -import { isPalindrome } from '@src/exercises/palindrome/index.ts'; +import { expect } from "chai"; +import { isPalindrome } from "@src/exercises/palindrome/index.ts"; -describe('Palindrome Exercise', () => { +describe("Palindrome Exercise", () => { const tests = [ { - input: '', + input: "", output: true, }, { - input: 'a', + input: "a", output: true, }, { - input: 'aa', + input: "aa", output: true, }, { - input: 'ab', + input: "ab", output: false, }, { - input: 'aba', + input: "aba", output: true, }, { - input: 'abc', + input: "abc", output: false, }, ]; diff --git a/src/exercises/rotational-cipher/index.test.ts b/src/exercises/rotational-cipher/index.test.ts index c89062d..6068197 100644 --- a/src/exercises/rotational-cipher/index.test.ts +++ b/src/exercises/rotational-cipher/index.test.ts @@ -1,17 +1,17 @@ -import { expect } from 'chai'; -import { rotationalCipher } from '@src/exercises/rotational-cipher/index.ts'; +import { expect } from "chai"; +import { rotationalCipher } from "@src/exercises/rotational-cipher/index.ts"; -describe('Rotational Cipher Exercise', () => { +describe("Rotational Cipher Exercise", () => { const tests = [ { - input: 'All-convoYs-9-be:Alert1.', + input: "All-convoYs-9-be:Alert1.", rotationFactor: 4, - expected: 'Epp-gsrzsCw-3-fi:Epivx5.', + expected: "Epp-gsrzsCw-3-fi:Epivx5.", }, { - input: 'abcdZXYzxy-999.@', + input: "abcdZXYzxy-999.@", rotationFactor: 200, - expected: 'stuvRPQrpq-999.@', + expected: "stuvRPQrpq-999.@", }, ]; diff --git a/src/exercises/rotational-cipher/index.ts b/src/exercises/rotational-cipher/index.ts index a3ddc48..de6c61f 100644 --- a/src/exercises/rotational-cipher/index.ts +++ b/src/exercises/rotational-cipher/index.ts @@ -1,59 +1,59 @@ // Add any extra import statements you may need here const letters = [ - 'a', - 'b', - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i', - 'j', - 'k', - 'l', - 'm', - 'n', - 'o', - 'p', - 'q', - 'r', - 's', - 't', - 'u', - 'v', - 'w', - 'x', - 'y', - 'z', + "a", + "b", + "c", + "d", + "e", + "f", + "g", + "h", + "i", + "j", + "k", + "l", + "m", + "n", + "o", + "p", + "q", + "r", + "s", + "t", + "u", + "v", + "w", + "x", + "y", + "z", ]; const index = { - a: '0', - b: '1', - c: '2', - d: '3', - e: '4', - f: '5', - g: '6', - h: '7', - i: '8', - j: '9', - k: '10', - l: '11', - m: '12', - n: '13', - o: '14', - p: '15', - q: '16', - r: '17', - s: '18', - t: '19', - u: '20', - v: '21', - w: '22', - x: '23', - y: '24', - z: '25', + a: "0", + b: "1", + c: "2", + d: "3", + e: "4", + f: "5", + g: "6", + h: "7", + i: "8", + j: "9", + k: "10", + l: "11", + m: "12", + n: "13", + o: "14", + p: "15", + q: "16", + r: "17", + s: "18", + t: "19", + u: "20", + v: "21", + w: "22", + x: "23", + y: "24", + z: "25", }; // Add any helper functions you may need here @@ -63,7 +63,7 @@ function encrypt(char, rotationFactor) { const entry = index[lowerCaseChar]; // it is a letter because an entry has been found in the index - if (typeof entry === 'string') { + if (typeof entry === "string") { // shift the index module 26 (the size of the alphabet) const shiftedIndex = (Number(entry) + rotationFactor) % 26; const encryptedLetter = letters[shiftedIndex]; @@ -100,5 +100,5 @@ export function rotationalCipher(input, rotationFactor) { responseArray[i] = encrypt(inputArray[i], rotationFactor); } - return responseArray.join(''); + return responseArray.join(""); } diff --git a/src/exercises/sliding-window-maximum/index.test.ts b/src/exercises/sliding-window-maximum/index.test.ts index 16d82db..c5f384c 100644 --- a/src/exercises/sliding-window-maximum/index.test.ts +++ b/src/exercises/sliding-window-maximum/index.test.ts @@ -1,7 +1,7 @@ -import { expect } from 'chai'; -import { maxSlidingWindow } from '.'; +import { expect } from "chai"; +import { maxSlidingWindow } from "."; -describe('Sliding Window Maximum Exercise', () => { +describe("Sliding Window Maximum Exercise", () => { const tests = [ { nums: [1, 3, -1, -3, 5, 3, 6, 7], @@ -20,17 +20,20 @@ describe('Sliding Window Maximum Exercise', () => { }, { nums: [ - 1, 3, -1, -3, 5, 3, 6, 7, -7, 11, 20, -73, 83, 234, -44, -1, 0, -111, 428, 6, 85, 70, 91, 77, - 1, 96, 62, 36, 59, 36, 12, 45, 74, 13, 85, 79, 49, 2, 12, 15, 25, 62, 41, 76, 89, 46, 55, 85, - 22, 25, 56, 51, 67, 6, 42, 33, 65, 58, 92, 5, 90, 5, 51, 81, 12, 81, 32, 75, 17, 21, -33, 54, - -82, 57, 36, 86, 60, 51, 97, 3, 21, 41, 45, 34, 33, 20, 64, 77, 66, 25, 9, + 1, 3, -1, -3, 5, 3, 6, 7, -7, 11, 20, -73, 83, 234, -44, -1, 0, -111, + 428, 6, 85, 70, 91, 77, 1, 96, 62, 36, 59, 36, 12, 45, 74, 13, 85, 79, + 49, 2, 12, 15, 25, 62, 41, 76, 89, 46, 55, 85, 22, 25, 56, 51, 67, 6, + 42, 33, 65, 58, 92, 5, 90, 5, 51, 81, 12, 81, 32, 75, 17, 21, -33, 54, + -82, 57, 36, 86, 60, 51, 97, 3, 21, 41, 45, 34, 33, 20, 64, 77, 66, 25, + 9, ], k: 8, expected: [ - 7, 7, 11, 20, 20, 83, 234, 234, 234, 234, 234, 428, 428, 428, 428, 428, 428, 428, 428, 96, 96, - 96, 96, 96, 96, 96, 74, 85, 85, 85, 85, 85, 85, 85, 85, 79, 76, 89, 89, 89, 89, 89, 89, 89, - 89, 85, 85, 85, 67, 67, 67, 92, 92, 92, 92, 92, 92, 92, 92, 90, 90, 81, 81, 81, 81, 81, 75, - 75, 86, 86, 86, 97, 97, 97, 97, 97, 97, 97, 97, 64, 77, 77, 77, 77, + 7, 7, 11, 20, 20, 83, 234, 234, 234, 234, 234, 428, 428, 428, 428, 428, + 428, 428, 428, 96, 96, 96, 96, 96, 96, 96, 74, 85, 85, 85, 85, 85, 85, + 85, 85, 79, 76, 89, 89, 89, 89, 89, 89, 89, 89, 85, 85, 85, 67, 67, 67, + 92, 92, 92, 92, 92, 92, 92, 92, 90, 90, 81, 81, 81, 81, 81, 75, 75, 86, + 86, 86, 97, 97, 97, 97, 97, 97, 97, 97, 64, 77, 77, 77, 77, ], }, ]; diff --git a/src/exercises/sum-of-two-integers/index.ts b/src/exercises/sum-of-two-integers/index.ts index 6dab82c..c2dfe8c 100644 --- a/src/exercises/sum-of-two-integers/index.ts +++ b/src/exercises/sum-of-two-integers/index.ts @@ -14,12 +14,12 @@ export function getSum(a: number, b: number): number { const xSign = getSign(x); const ySign = getSign(y); - if (xSign === '-') { - x = x.split('').slice(1).join(''); + if (xSign === "-") { + x = x.split("").slice(1).join(""); } - if (ySign === '-') { - y = y.split('').slice(1).join(''); + if (ySign === "-") { + y = y.split("").slice(1).join(""); } if (xSign === ySign) { @@ -29,7 +29,7 @@ export function getSum(a: number, b: number): number { counter++; } - if (xSign === '+') { + if (xSign === "+") { return counter; } @@ -38,7 +38,7 @@ export function getSum(a: number, b: number): number { if (x === y) { return 0; } - let sign = ''; + let sign = ""; if (Number(x) > Number(y)) { let counter = Number(x); @@ -47,8 +47,8 @@ export function getSum(a: number, b: number): number { counter--; } - if (xSign === '-') { - sign = '-'; + if (xSign === "-") { + sign = "-"; } return Number(`${sign}${counter}`); @@ -60,8 +60,8 @@ export function getSum(a: number, b: number): number { counter--; } - if (ySign === '-') { - sign = '-'; + if (ySign === "-") { + sign = "-"; } return Number(`${sign}${counter}`); @@ -70,7 +70,7 @@ export function getSum(a: number, b: number): number { function getSign(n: string): string { const sign = n.charAt(0); - if (sign === '-') return '-'; + if (sign === "-") return "-"; - return '+'; + return "+"; } diff --git a/src/exercises/uniform-integers/index.test.ts b/src/exercises/uniform-integers/index.test.ts index 28a76d6..20498c7 100644 --- a/src/exercises/uniform-integers/index.test.ts +++ b/src/exercises/uniform-integers/index.test.ts @@ -1,7 +1,7 @@ -import { expect } from 'chai'; -import { getUniformIntegerCountInInterval } from '@src/exercises/uniform-integers/index.ts'; +import { expect } from "chai"; +import { getUniformIntegerCountInInterval } from "@src/exercises/uniform-integers/index.ts"; -describe('Uniform Integers Exercise', () => { +describe("Uniform Integers Exercise", () => { const tests = [ { start: 75, @@ -74,7 +74,7 @@ describe('Uniform Integers Exercise', () => { it(`getUniformIntegerCountInInterval(${start}, ${end}) should return ${expected}`, () => { if (isException) { expect(() => getUniformIntegerCountInInterval(start, end)).to.throw( - 'The two numbers in input must be integers', + "The two numbers in input must be integers", ); } else { expect(getUniformIntegerCountInInterval(start, end)).to.equal(expected); diff --git a/src/exercises/uniform-integers/index.ts b/src/exercises/uniform-integers/index.ts index 363e55a..f90a419 100644 --- a/src/exercises/uniform-integers/index.ts +++ b/src/exercises/uniform-integers/index.ts @@ -25,7 +25,7 @@ function initTheCounter(A: number): number { * @returns the first uniform number N >= A */ function findTheFirstUniformInteger(A: number) { - const tokens = `${A}`.split(''); + const tokens = `${A}`.split(""); // gets the left-most digit of the number and the number of digits the number is composed of let leftMostDigit = Number(tokens[0]); @@ -60,7 +60,7 @@ export function getUniformIntegerCountInInterval(A: number, B: number) { */ if (!Number.isInteger(A) || !Number.isInteger(B)) - throw new Error('The two numbers in input must be integers'); + throw new Error("The two numbers in input must be integers"); if (A > B || A <= 0) return 0; @@ -85,7 +85,8 @@ export function getUniformIntegerCountInInterval(A: number, B: number) { startInterval = 10; } - let { uniformInteger, leftMostDigit, numberOfDigits } = findTheFirstUniformInteger(startInterval); + let { uniformInteger, leftMostDigit, numberOfDigits } = + findTheFirstUniformInteger(startInterval); // while the number is less or equal to B while (Number(uniformInteger) <= B) { diff --git a/src/exercises/valid-binary-search-tree/index.test.ts b/src/exercises/valid-binary-search-tree/index.test.ts index ff003f3..a939241 100644 --- a/src/exercises/valid-binary-search-tree/index.test.ts +++ b/src/exercises/valid-binary-search-tree/index.test.ts @@ -1,8 +1,8 @@ -import { expect } from 'chai'; -import { isValidBST } from '@src/exercises/valid-binary-search-tree/index.ts'; -import BinaryTree from '@src/data-structures/BinaryTree.ts'; +import { expect } from "chai"; +import { isValidBST } from "@src/exercises/valid-binary-search-tree/index.ts"; +import BinaryTree from "@src/data-structures/BinaryTree.ts"; -describe('Valid Binary Search Tree Exercise', () => { +describe("Valid Binary Search Tree Exercise", () => { const root1 = new BinaryTree(4); root1.leftChild = new BinaryTree(2); root1.rightChild = new BinaryTree(5); diff --git a/src/exercises/valid-binary-search-tree/index.ts b/src/exercises/valid-binary-search-tree/index.ts index 692614f..51a670c 100644 --- a/src/exercises/valid-binary-search-tree/index.ts +++ b/src/exercises/valid-binary-search-tree/index.ts @@ -1,4 +1,4 @@ -import BinaryTree from '../../data-structures/BinaryTree'; +import BinaryTree from "../../data-structures/BinaryTree"; export function isValidBST( node: BinaryTree, @@ -17,5 +17,8 @@ export function isValidBST( if (right !== null && node.value >= right.value) return false; // check recursively for every node. - return isValidBST(node.leftChild, left, node) && isValidBST(node.rightChild, node, right); + return ( + isValidBST(node.leftChild, left, node) && + isValidBST(node.rightChild, node, right) + ); }