From 7d7f109e6fe36e901aff06750d89788ee7be5f3a Mon Sep 17 00:00:00 2001 From: Hardik Pawar <97388607+Hardvan@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:18:13 +0530 Subject: [PATCH 1/3] Enhance readability of KnightTour (#1572) --- Backtracking/KnightTour.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Backtracking/KnightTour.js b/Backtracking/KnightTour.js index 36f400160d..5f81ca345a 100644 --- a/Backtracking/KnightTour.js +++ b/Backtracking/KnightTour.js @@ -2,12 +2,13 @@ class OpenKnightTour { constructor(size) { + // Constructor to initialize the chessboard and size this.board = new Array(size).fill(0).map(() => new Array(size).fill(0)) this.size = size } getMoves([i, j]) { - // helper function to get the valid moves of the knight from the current position + // Helper function to get the valid moves of the knight from the current position const moves = [ [i + 2, j - 1], [i + 2, j + 1], @@ -19,18 +20,19 @@ class OpenKnightTour { [i - 1, j + 2] ] + // Filter out moves that are within the board boundaries return moves.filter( ([y, x]) => y >= 0 && y < this.size && x >= 0 && x < this.size ) } isComplete() { - // helper function to check if the board is complete + // Helper function to check if the board is complete return !this.board.map((row) => row.includes(0)).includes(true) } solve() { - // function to find the solution for the given board + // Function to find the solution for the given board for (let i = 0; i < this.size; i++) { for (let j = 0; j < this.size; j++) { if (this.solveHelper([i, j], 0)) return true @@ -40,14 +42,15 @@ class OpenKnightTour { } solveHelper([i, j], curr) { - // helper function for the main computation + // Helper function for the main computation if (this.isComplete()) return true + // Iterate through possible moves and attempt to fill the board for (const [y, x] of this.getMoves([i, j])) { if (this.board[y][x] === 0) { this.board[y][x] = curr + 1 if (this.solveHelper([y, x], curr + 1)) return true - // backtracking + // Backtracking: If the solution is not found, reset the cell to 0 this.board[y][x] = 0 } } @@ -55,7 +58,7 @@ class OpenKnightTour { } printBoard(output = (value) => console.log(value)) { - // utility function to display the board + // Utility function to display the board for (const row of this.board) { let string = '' for (const elem of row) { From fb134b10b0521d01ade8da215803d34d9fafd8e8 Mon Sep 17 00:00:00 2001 From: Vedas Dixit <111585043+vedas-dixit@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:18:59 +0530 Subject: [PATCH 2/3] Implemented M Coloring Problem (#1562) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Implemented M Coloring Problem * Implemented M Coloring Problem * Switch to a functional approach instead of class-based. Use proper JSDoc comments. Refine the comments and remove redundancies. * Updated Documentation in README.md * Proper JSDoc comment --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars MΓΌller <34514239+appgurueu@users.noreply.github.com> --- Backtracking/MColoringProblem.js | 49 +++++++++++++++++++++ Backtracking/tests/MColoringProblem.test.js | 23 ++++++++++ DIRECTORY.md | 2 + 3 files changed, 74 insertions(+) create mode 100644 Backtracking/MColoringProblem.js create mode 100644 Backtracking/tests/MColoringProblem.test.js diff --git a/Backtracking/MColoringProblem.js b/Backtracking/MColoringProblem.js new file mode 100644 index 0000000000..c89f30e043 --- /dev/null +++ b/Backtracking/MColoringProblem.js @@ -0,0 +1,49 @@ +/** + * Colors a graph using up to m colors such that no two adjacent vertices share the same color. + * @param {number[][]} graph - Adjacency matrix of the graph, using 0 for no edge. + * @param {number} m - The number of colors to use. + * @returns {?Array.} A valid M-coloring of the graph using colors 1 to m, or null if none exists. + * @see https://en.wikipedia.org/wiki/Graph_coloring + */ +function mColoring(graph, m) { + const colors = new Array(graph.length).fill(0); + + // Check if it's safe to color a vertex with a given color. + function isSafe(vertex, color) { + for (let i = 0; i < graph.length; i++) { + if (graph[vertex][i] && colors[i] === color) { + return false; + } + } + return true; + } + + // Use backtracking to try and color the graph. + function solveColoring(vertex = 0) { + if (vertex === graph.length) { + return true; + } + + for (let color = 1; color <= m; color++) { + if (isSafe(vertex, color)) { + colors[vertex] = color; + + if (solveColoring(vertex + 1)) { + return true; + } + + // If no solution, backtrack. + colors[vertex] = 0; + } + } + return false; + } + + // If coloring is possible, return the colors. + if (solveColoring()) { + return colors; + } + return null; +} + +export { mColoring }; diff --git a/Backtracking/tests/MColoringProblem.test.js b/Backtracking/tests/MColoringProblem.test.js new file mode 100644 index 0000000000..d98134b62a --- /dev/null +++ b/Backtracking/tests/MColoringProblem.test.js @@ -0,0 +1,23 @@ +import { mColoring } from '../MColoringProblem'; + +describe('MColoringProblem', () => { + it('should color a triangle with 3 colors', () => { + const graph = [ + [0, 1, 1], + [1, 0, 1], + [1, 1, 0] + ]; + const solution = mColoring(graph, 3); + expect(solution).not.toBeNull(); + }); + + it('should not color a triangle with 2 colors', () => { + const graph = [ + [0, 1, 1], + [1, 0, 1], + [1, 1, 0] + ]; + const solution = mColoring(graph, 2); + expect(solution).toBeNull(); + }); +}); diff --git a/DIRECTORY.md b/DIRECTORY.md index aea7b1548f..61fa501119 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -3,6 +3,7 @@ * [generateParentheses](Backtracking/generateParentheses.js) * [GeneratePermutations](Backtracking/GeneratePermutations.js) * [KnightTour](Backtracking/KnightTour.js) + * [MColoringProblem](Backtracking/MColoringProblem.js) * [NQueens](Backtracking/NQueens.js) * [RatInAMaze](Backtracking/RatInAMaze.js) * [Sudoku](Backtracking/Sudoku.js) @@ -166,6 +167,7 @@ * [Area](Maths/Area.js) * [ArithmeticGeometricMean](Maths/ArithmeticGeometricMean.js) * [ArmstrongNumber](Maths/ArmstrongNumber.js) + * [AutomorphicNumber](Maths/AutomorphicNumber.js) * [AverageMean](Maths/AverageMean.js) * [AverageMedian](Maths/AverageMedian.js) * [BinaryConvert](Maths/BinaryConvert.js) From 9a875264cc526a874defd09544a8bcfd7d3f6212 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Tue, 24 Oct 2023 22:49:37 +0530 Subject: [PATCH 3/3] prettier fixes & added test cases for Project Euler problem 4 (#1566) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * πŸ“¦ NEW: Added solution for ProjectEuler-007 * πŸ› FIX: Spelling mistake fixes * πŸ‘Œ IMPROVE: changed variable name from `inc` to `candidateValue` and thrown error in case of invalid input * πŸ‘Œ IMPROVE: Modified the code * πŸ‘Œ IMPROVE: Added test case for ProjectEuler Problem001 * πŸ‘Œ IMPROVE: Added test cases for Project Euler Problem 4 * πŸ‘Œ IMPROVE: auto prettier fixes --------- Co-authored-by: Omkarnath Parida --- Bit-Manipulation/BinaryCountSetBits.js | 2 +- Maths/AutomorphicNumber.js | 2 +- Maths/test/AutomorphicNumber.test.js | 16 ++++----- Project-Euler/Problem006.js | 6 ++-- Project-Euler/test/Problem004.test.js | 11 ++++++ Recursive/test/BinaryEquivalent.test.js | 48 ++++++++++++------------- Search/InterpolationSearch.js | 2 +- 7 files changed, 49 insertions(+), 38 deletions(-) create mode 100644 Project-Euler/test/Problem004.test.js diff --git a/Bit-Manipulation/BinaryCountSetBits.js b/Bit-Manipulation/BinaryCountSetBits.js index b879f3bd67..b959caf062 100644 --- a/Bit-Manipulation/BinaryCountSetBits.js +++ b/Bit-Manipulation/BinaryCountSetBits.js @@ -16,7 +16,7 @@ function BinaryCountSetBits(a) { let count = 0 while (a) { - a &= (a - 1) + a &= a - 1 count++ } diff --git a/Maths/AutomorphicNumber.js b/Maths/AutomorphicNumber.js index d1b6608316..ba008271fc 100644 --- a/Maths/AutomorphicNumber.js +++ b/Maths/AutomorphicNumber.js @@ -35,6 +35,6 @@ export const isAutomorphic = (n) => { n = Math.floor(n / 10) n_sq = Math.floor(n_sq / 10) } - + return true } diff --git a/Maths/test/AutomorphicNumber.test.js b/Maths/test/AutomorphicNumber.test.js index 19b963388c..57f40d27ee 100644 --- a/Maths/test/AutomorphicNumber.test.js +++ b/Maths/test/AutomorphicNumber.test.js @@ -9,19 +9,19 @@ describe('AutomorphicNumber', () => { }) test.each([ - { n: -3 , expected: false }, - { n: -25 , expected: false }, + { n: -3, expected: false }, + { n: -25, expected: false } ])('should return false when n is negetive', ({ n, expected }) => { expect(isAutomorphic(n)).toBe(false) }) test.each([ - { n: 7 , expected: false }, - { n: 83 , expected: false }, - { n: 0 , expected: true }, - { n: 1 , expected: true }, - { n: 376 , expected: true }, - { n: 90625 , expected: true }, + { n: 7, expected: false }, + { n: 83, expected: false }, + { n: 0, expected: true }, + { n: 1, expected: true }, + { n: 376, expected: true }, + { n: 90625, expected: true } ])('should return $expected when n is $n', ({ n, expected }) => { expect(isAutomorphic(n)).toBe(expected) }) diff --git a/Project-Euler/Problem006.js b/Project-Euler/Problem006.js index 474de2ae96..804b165558 100644 --- a/Project-Euler/Problem006.js +++ b/Project-Euler/Problem006.js @@ -1,8 +1,8 @@ // https://projecteuler.net/problem=6 export const squareDifference = (num = 100) => { - let sumOfSquares = (num)*(num+1)*(2*num+1)/6 - let sums = (num*(num+1))/2 - + let sumOfSquares = (num * (num + 1) * (2 * num + 1)) / 6 + let sums = (num * (num + 1)) / 2 + return sums ** 2 - sumOfSquares // difference of square of the total sum and sum of squares } diff --git a/Project-Euler/test/Problem004.test.js b/Project-Euler/test/Problem004.test.js new file mode 100644 index 0000000000..cd6a55ac98 --- /dev/null +++ b/Project-Euler/test/Problem004.test.js @@ -0,0 +1,11 @@ +import { largestPalindromic } from '../Problem004.js' + +describe('Largest Palindromic Number', () => { + test('if digit is 2', () => { + expect(largestPalindromic(2)).toBe(9009) + }) + // Project Euler Condition Check + test('if digit is 3', () => { + expect(largestPalindromic(3)).toBe(906609) + }) +}) diff --git a/Recursive/test/BinaryEquivalent.test.js b/Recursive/test/BinaryEquivalent.test.js index b79a455eed..ddabb7d477 100644 --- a/Recursive/test/BinaryEquivalent.test.js +++ b/Recursive/test/BinaryEquivalent.test.js @@ -1,29 +1,29 @@ -import { binaryEquivalent } from "../BinaryEquivalent"; +import { binaryEquivalent } from '../BinaryEquivalent' const tests = [ - { - test: 2, - expectedValue: "10" - }, - { - test: 0, - expectedValue: "0" - }, - { - test: 543, - expectedValue: "1000011111" - }, - { - test: 4697621023, - expectedValue: "100011000000000000000001000011111" - } + { + test: 2, + expectedValue: '10' + }, + { + test: 0, + expectedValue: '0' + }, + { + test: 543, + expectedValue: '1000011111' + }, + { + test: 4697621023, + expectedValue: '100011000000000000000001000011111' + } ] -describe("Binary Equivalent", () => { - test.each(tests)( - "of $test should be $expectedValue", - ({test, expectedValue}) => { - expect(binaryEquivalent(test)).toBe(expectedValue); - } - ) +describe('Binary Equivalent', () => { + test.each(tests)( + 'of $test should be $expectedValue', + ({ test, expectedValue }) => { + expect(binaryEquivalent(test)).toBe(expectedValue) + } + ) }) diff --git a/Search/InterpolationSearch.js b/Search/InterpolationSearch.js index e6deae496f..93f3b78b0e 100644 --- a/Search/InterpolationSearch.js +++ b/Search/InterpolationSearch.js @@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) { } return -1 -} \ No newline at end of file +}