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
2 changes: 1 addition & 1 deletion Backtracking/GeneratePermutations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Problem Statement: Generate all distinct permutations of a an array (all permutations should be in sorted order);
* Problem Statement: Generate all distinct permutations of an array (all permutations should be in sorted order);
*
* What is permutations?
* - Permutation means possible arrangements in a set (here it is an array);
Expand Down
42 changes: 42 additions & 0 deletions Bit-Manipulation/GrayCodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Generates a Gray code sequence for the given number of bits.
* @param {number} n - The number of bits in the Gray code sequence.
* @returns {number[]} - An array of Gray codes in binary format.
* @description
* Gray codes are binary sequences in which two successive values differ in only one bit.
* This function generates a Gray code sequence of length 2^n for the given number of bits.
*
* The algorithm follows these steps:
*
* 1. Initialize an array `grayCodes` to store the Gray codes. Start with [0, 1] for n = 1.
* 2. Iterate from 1 to n:
* a. Calculate `highestBit` as 2^i, where `i` is the current iteration index.
* b. Iterate in reverse order through the existing Gray codes:
* - For each Gray code `code`, add `highestBit | code` to `grayCodes`.
* - This operation flips a single bit in each existing code, creating new codes.
* 3. Return the `grayCodes` array containing the Gray codes in decimal representation.
*
*resources: [GFG](https://www.geeksforgeeks.org/generate-n-bit-gray-codes/)
* @example
* const n = 3;
* const grayCodes = generateGrayCodes(n);
* // grayCodes will be [0, 1, 3, 2, 6, 7, 5, 4] for n=3.
*/
function generateGrayCodes(n) {
if (n <= 0) {
return [0]
}

const grayCodes = [0, 1]

for (let i = 1; i < n; i++) {
const highestBit = 1 << i
for (let j = grayCodes.length - 1; j >= 0; j--) {
grayCodes.push(highestBit | grayCodes[j])
}
}

return grayCodes
}

export { generateGrayCodes }
19 changes: 19 additions & 0 deletions Bit-Manipulation/test/GrayCodes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { generateGrayCodes } from '../GrayCodes.js'

describe('Gray codes', () => {
test.each([
[0, [0b0]],
[1, [0b0, 0b1]],
[2, [0b00, 0b01, 0b11, 0b10]],
[3, [0b000, 0b001, 0b011, 0b010, 0b110, 0b111, 0b101, 0b100]],
[
4,
[
0b0000, 0b0001, 0b0011, 0b0010, 0b0110, 0b0111, 0b0101, 0b0100, 0b1100,
0b1101, 0b1111, 0b1110, 0b1010, 0b1011, 0b1001, 0b1000
]
]
])('n = %i -> %j', (n, expected) => {
expect(generateGrayCodes(n)).toEqual(expected)
})
})
149 changes: 77 additions & 72 deletions Ciphers/KeyFinder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/******************************************************
Find and retrieve the encryption key automatically
Note: This is a draft version, please help to modify, Thanks!
******************************************************/
/**
* Find and retrieve the encryption key automatically.
* @param {string} str - The input encrypted string.
* @returns {number} - The encryption key found, or 0 if not found.
*/
function keyFinder(str) {
// str is used to get the input of encrypted string
const wordBank = [
Expand Down Expand Up @@ -30,8 +31,6 @@ function keyFinder(str) {
' be ',
'Be '
]
// let wordbankelementCounter = 0;
// let key = 0; // return zero means the key can not be found
const inStr = str.toString() // convert the input to String
let outStr = '' // store the output value
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison
Expand Down Expand Up @@ -59,89 +58,95 @@ function keyFinder(str) {
return 0 // return 0 if found nothing
}

/* this sub-function is used to assist the keyFinder to find the key */
/**
* This sub-function is used to assist the keyFinder in finding the key.
* @param {string} inStr - The input string.
* @param {number} numShifted - The number of characters to shift in the Caesar cipher.
* @returns {string} - The decrypted string.
*/
function caesarCipherEncodeAndDecodeEngine(inStr, numShifted) {
const shiftNum = numShifted
let charCode = 0
let outStr = ''
let shiftedCharCode = 0
let result = 0

for (let i = 0; i < inStr.length; i++) {
charCode = inStr[i].charCodeAt()
shiftedCharCode = charCode + shiftNum
result = charCode
return inStr
.split('')
.map((char) => {
charCode = char.charCodeAt()
shiftedCharCode = charCode + shiftNum
result = charCode

if (charCode >= 48 && charCode <= 57) {
if (shiftedCharCode < 48) {
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10
if (charCode >= 48 && charCode <= 57) {
if (shiftedCharCode < 48) {
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10

while (diff >= 10) {
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff
while (diff >= 10) {
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff

shiftedCharCode = 57 - diff
shiftedCharCode = 57 - diff

result = shiftedCharCode
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
result = shiftedCharCode
} else if (shiftedCharCode > 57) {
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10
result = shiftedCharCode
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
result = shiftedCharCode
} else if (shiftedCharCode > 57) {
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10

while (diff >= 10) {
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff
while (diff >= 10) {
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff

shiftedCharCode = 48 + diff
shiftedCharCode = 48 + diff

result = shiftedCharCode
}
} else if (charCode >= 65 && charCode <= 90) {
if (shiftedCharCode <= 64) {
let diff = Math.abs(65 - 1 - shiftedCharCode) % 26

while (diff % 26 >= 26) {
diff = diff % 26
result = shiftedCharCode
}
shiftedCharCode = 90 - diff
result = shiftedCharCode
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
result = shiftedCharCode
} else if (shiftedCharCode > 90) {
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26

while (diff % 26 >= 26) {
diff = diff % 26
} else if (charCode >= 65 && charCode <= 90) {
if (shiftedCharCode <= 64) {
let diff = Math.abs(65 - 1 - shiftedCharCode) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 90 - diff
result = shiftedCharCode
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
result = shiftedCharCode
} else if (shiftedCharCode > 90) {
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 65 + diff
result = shiftedCharCode
}
shiftedCharCode = 65 + diff
result = shiftedCharCode
}
} else if (charCode >= 97 && charCode <= 122) {
if (shiftedCharCode <= 96) {
let diff = Math.abs(97 - 1 - shiftedCharCode) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 122 - diff
result = shiftedCharCode
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
result = shiftedCharCode
} else if (shiftedCharCode > 122) {
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26

while (diff % 26 >= 26) {
diff = diff % 26
} else if (charCode >= 97 && charCode <= 122) {
if (shiftedCharCode <= 96) {
let diff = Math.abs(97 - 1 - shiftedCharCode) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 122 - diff
result = shiftedCharCode
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
result = shiftedCharCode
} else if (shiftedCharCode > 122) {
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 97 + diff
result = shiftedCharCode
}
shiftedCharCode = 97 + diff
result = shiftedCharCode
}
}
outStr = outStr + String.fromCharCode(parseInt(result))
}
return outStr
return String.fromCharCode(parseInt(result))
})
.join('')
}

export { keyFinder }
Expand Down
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
* [DecimalExpansion](Maths/DecimalExpansion.js)
* [DecimalIsolate](Maths/DecimalIsolate.js)
* [DegreeToRadian](Maths/DegreeToRadian.js)
* [EuclideanDistance](Maths/EuclideanDistance.js)
* [EulerMethod](Maths/EulerMethod.js)
* [EulersTotient](Maths/EulersTotient.js)
* [EulersTotientFunction](Maths/EulersTotientFunction.js)
Expand Down Expand Up @@ -249,6 +250,7 @@
* [SumOfDigits](Maths/SumOfDigits.js)
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
* [TwinPrime](Maths/TwinPrime.js)
* [TwoSum](Maths/TwoSum.js)
* [Volume](Maths/Volume.js)
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
* [ZellersCongruenceAlgorithm](Maths/ZellersCongruenceAlgorithm.js)
Expand Down
Loading