Skip to content
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
15 changes: 9 additions & 6 deletions Backtracking/KnightTour.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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
Expand All @@ -40,22 +42,23 @@ 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
}
}
return false
}

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) {
Expand Down
49 changes: 49 additions & 0 deletions Backtracking/MColoringProblem.js
Original file line number Diff line number Diff line change
@@ -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.<number>} 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 };
23 changes: 23 additions & 0 deletions Backtracking/tests/MColoringProblem.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
2 changes: 1 addition & 1 deletion Bit-Manipulation/BinaryCountSetBits.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function BinaryCountSetBits(a) {

let count = 0
while (a) {
a &= (a - 1)
a &= a - 1
count++
}

Expand Down
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Maths/AutomorphicNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ export const isAutomorphic = (n) => {
n = Math.floor(n / 10)
n_sq = Math.floor(n_sq / 10)
}

return true
}
16 changes: 8 additions & 8 deletions Maths/test/AutomorphicNumber.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
6 changes: 3 additions & 3 deletions Project-Euler/Problem006.js
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions Project-Euler/test/Problem004.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
48 changes: 24 additions & 24 deletions Recursive/test/BinaryEquivalent.test.js
Original file line number Diff line number Diff line change
@@ -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)
}
)
})
2 changes: 1 addition & 1 deletion Search/InterpolationSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) {
}

return -1
}
}