From d45d58ec9f4e5da0a2f90a8c7534aca811c86fe0 Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Fri, 6 Oct 2023 16:28:33 +1100 Subject: [PATCH 1/6] Improve algorithm --- Ciphers/KeyFinder.js | 154 +++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 73 deletions(-) diff --git a/Ciphers/KeyFinder.js b/Ciphers/KeyFinder.js index add581f65b..1f0830b933 100644 --- a/Ciphers/KeyFinder.js +++ b/Ciphers/KeyFinder.js @@ -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 = [ 'I ', @@ -27,9 +28,9 @@ function keyFinder (str) { // str is used to get the input of encrypted string ' may ', 'May ', ' be ', - 'Be '] - // let wordbankelementCounter = 0; - // let key = 0; // return zero means the key can not be found + 'Be ' + ] + 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 @@ -56,92 +57,99 @@ function keyFinder (str) { // str is used to get the input of encrypted string 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 - - result = shiftedCharCode - } - } else if ((charCode >= 65 && charCode <= 90)) { - if (shiftedCharCode <= 64) { - let diff = Math.abs(65 - 1 - shiftedCharCode) % 26 + shiftedCharCode = 48 + diff - 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 } + return String.fromCharCode(parseInt(result)) } - outStr = outStr + String.fromCharCode(parseInt(result)) - } - return outStr + ).join('') } -export { keyFinder } +export { + keyFinder +} // > keyFinder('test') // 0 From 8406493435063f6cf550b21e501a001498a586c3 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 6 Oct 2023 05:29:27 +0000 Subject: [PATCH 2/6] Updated Documentation in README.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2d70c5d6a2..938cbbcf3a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -47,6 +47,7 @@ * [HexToBinary](Conversions/HexToBinary.js) * [HexToDecimal](Conversions/HexToDecimal.js) * [HexToRGB](Conversions/HexToRGB.js) + * [LengthConversion](Conversions/LengthConversion.js) * [LitersToImperialGallons](Conversions/LitersToImperialGallons.js) * [LitersToUSGallons](Conversions/LitersToUSGallons.js) * [LowerCaseConversion](Conversions/LowerCaseConversion.js) @@ -233,6 +234,7 @@ * [PowLogarithmic](Maths/PowLogarithmic.js) * [PrimeCheck](Maths/PrimeCheck.js) * [PrimeFactors](Maths/PrimeFactors.js) + * [QuadraticRoots](Maths/QuadraticRoots.js) * [RadianToDegree](Maths/RadianToDegree.js) * [ReverseNumber](Maths/ReverseNumber.js) * [ReversePolishNotation](Maths/ReversePolishNotation.js) From 90e50e6b0ce4b68af45443c91a686a4cca34e89d Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 6 Oct 2023 05:36:56 +0000 Subject: [PATCH 3/6] Updated Documentation in README.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 31969e4cd1..a7e5fe2c6c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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) From d4f2fe00420cd007cd517c0462e861c1febbb627 Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Fri, 6 Oct 2023 16:37:45 +1100 Subject: [PATCH 4/6] Remove unwanted changes --- Maths/test/EuclideanDistance.test.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Maths/test/EuclideanDistance.test.js b/Maths/test/EuclideanDistance.test.js index 717ea2e6a0..d73bb03875 100644 --- a/Maths/test/EuclideanDistance.test.js +++ b/Maths/test/EuclideanDistance.test.js @@ -2,17 +2,11 @@ import { EuclideanDistance } from '../EuclideanDistance.js' describe('EuclideanDistance', () => { it('should calculate the distance correctly for 2D vectors', () => { - expect(EuclideanDistance([0, 0], [2, 2])).toBeCloseTo( - 2.8284271247461903, - 10 - ) + expect(EuclideanDistance([0, 0], [2, 2])).toBeCloseTo(2.8284271247461903, 10) }) it('should calculate the distance correctly for 3D vectors', () => { - expect(EuclideanDistance([0, 0, 0], [2, 2, 2])).toBeCloseTo( - 3.4641016151377544, - 10 - ) + expect(EuclideanDistance([0, 0, 0], [2, 2, 2])).toBeCloseTo(3.4641016151377544, 10) }) it('should calculate the distance correctly for 4D vectors', () => { From e230c6c91d5bff127ba1e2a52ea651fab44ef8df Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Fri, 6 Oct 2023 16:40:20 +1100 Subject: [PATCH 5/6] Make the changes fit --- DIRECTORY.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index a7e5fe2c6c..31969e4cd1 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -179,7 +179,6 @@ * [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) @@ -250,7 +249,6 @@ * [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) From 021c2dee9290d29ed497b2db1112c06137b06766 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 6 Oct 2023 05:42:41 +0000 Subject: [PATCH 6/6] Updated Documentation in README.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 31969e4cd1..a7e5fe2c6c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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)