From 7d40bb4a29d64834d17c9d554a52b5742a176dc2 Mon Sep 17 00:00:00 2001 From: shidian00 <81557908+shidian00@users.noreply.github.com> Date: Wed, 27 Jul 2022 20:01:11 +0800 Subject: [PATCH 1/2] merge: Add Affine Cipher (#1067) * Add Affine Cipher * Replace IndexOf function to improve performance * remove leading spacing and fixed typo * Remove leading spacing in first row. --- Ciphers/AffineCipher.js | 95 +++++++++++++++++++++++++++++++ Ciphers/test/AffineCipher.test.js | 27 +++++++++ 2 files changed, 122 insertions(+) create mode 100644 Ciphers/AffineCipher.js create mode 100644 Ciphers/test/AffineCipher.test.js diff --git a/Ciphers/AffineCipher.js b/Ciphers/AffineCipher.js new file mode 100644 index 0000000000..367cb18597 --- /dev/null +++ b/Ciphers/AffineCipher.js @@ -0,0 +1,95 @@ +/** + * @description - The affine cipher is a type of monoalphabetic substitution cipher, where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter + * @see - [wiki](https://en.wikipedia.org/wiki/Affine_cipher) + */ + +// Default key for Affine Cipher +const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + +/** + * Fix result for negative value in modulas equation + * @param {Number} n - Constant number + * @param {Number} m - Modulos value + * @return {Number} Return n mod m + */ +function mod (n, m) { + return ((n % m) + m) % m +} + +/** + * Modular multiplicative inverse + * @param {Number} a - A coefficient + * @param {Number} m - Modulos value + * @return {Number} Return modular multiplicative inverse of coefficient a and modulos m + */ +function inverseMod (a, m) { + for (let x = 1; x < m; x++) { + if (mod(a * x, m) === 1) return x + } +} + +/** + * Argument validation + * @param {String} str - String to be checked + * @param {Number} a - A coefficient to be checked + * @param {Number} b - B coefficient to be checked + * @return {Boolean} Result of the checking + */ +function isCorrectFormat (str, a, b) { + if (typeof a !== 'number' || typeof b !== 'number') { + throw new TypeError('Coefficient a, b should be number') + } + + if (typeof str !== 'string') { + throw new TypeError('Argument str should be String') + } + + return true +} +/** + * Find character index based on ASCII order + * @param {String} char - Character index to be found + * @return {Boolean} Character index + */ +function findCharIndex (char) { + return char.toUpperCase().charCodeAt(0) - ('A').charCodeAt(0) +} + +/** + * Encrypt a Affine Cipher + * @param {String} str - String to be encrypted + * @param {Number} a - A coefficient + * @param {Number} b - B coefficient + * @return {String} result - Encrypted string + */ +function encrypt (str, a, b) { + let result = '' + if (isCorrectFormat(str, a, b)) { + for (let x = 0; x < str.length; x++) { + const charIndex = findCharIndex(str[x]) + if (charIndex < 0) result += '-1' + ' ' + else result += mod(a * charIndex + b, 26) + ' ' + } + } + return result.trim() +} +/** + * Decrypt a Affine Cipher + * @param {String} str - String to be decrypted + * @param {Number} a - A coefficient + * @param {Number} b - B coefficient + * @return {String} result - Decrypted string + */ +function decrypt (str, a, b) { + let result = '' + if (isCorrectFormat(str, a, b)) { + str = str.split(' ') + for (let x = 0; x < str.length; x++) { + if (str[x] === '-1') result += ' ' + else result += key[mod(inverseMod(a, 26) * (parseInt(str[x]) - b), 26)] + } + } + return result +} + +export { encrypt, decrypt } diff --git a/Ciphers/test/AffineCipher.test.js b/Ciphers/test/AffineCipher.test.js new file mode 100644 index 0000000000..632704bce2 --- /dev/null +++ b/Ciphers/test/AffineCipher.test.js @@ -0,0 +1,27 @@ +import { encrypt, decrypt } from '../AffineCipher' + +describe('Test Affine Cipher', () => { + it('Test - 1, Pass invalid input to encrypt function', () => { + expect(() => encrypt(null, null, null)).toThrow() + expect(() => encrypt('null', null, null)).toThrow() + expect(() => encrypt('null', 1, null)).toThrow() + expect(() => encrypt('null', null, 1)).toThrow() + }) + + it('Test - 2, Pass invalid input to decrypt function', () => { + expect(() => decrypt(null, null, null)).toThrow() + expect(() => decrypt('null', null, null)).toThrow() + expect(() => decrypt('null', 1, null)).toThrow() + expect(() => decrypt('null', null, 1)).toThrow() + }) + + it('Test - 3 Pass string value to encrypt and ecrypt function', () => { + const a = 5 + const b = 8 + expect(decrypt(encrypt('HELLO WORLD', a, b), a, b)).toBe('HELLO WORLD') + expect(decrypt(encrypt('ABC DEF', a, b), a, b)).toBe('ABC DEF') + expect(decrypt(encrypt('Brown fox jump over the fence', a, b), a, b)).toBe( + 'BROWN FOX JUMP OVER THE FENCE' + ) + }) +}) From 1a089cc49122f558fb23fe0447637db1c9c2a721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:13:07 +0200 Subject: [PATCH 2/2] merge: Fix GetEuclidGCD (#1068) * Fix GetEuclidGCD Implement the actual Euclidean Algorithm * Replace == with === * Lua > JS * Standard sucks * Oops * Update GetEuclidGCD.js * Updated Documentation in README.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + Maths/GetEuclidGCD.js | 42 +++++++++++++++--------------------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3793104e27..5a491bf570 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -18,6 +18,7 @@ * **Cellular-Automata** * [ConwaysGameOfLife](Cellular-Automata/ConwaysGameOfLife.js) * **Ciphers** + * [AffineCipher](Ciphers/AffineCipher.js) * [Atbash](Ciphers/Atbash.js) * [CaesarsCipher](Ciphers/CaesarsCipher.js) * [KeyFinder](Ciphers/KeyFinder.js) diff --git a/Maths/GetEuclidGCD.js b/Maths/GetEuclidGCD.js index 3aff8dbbeb..aa77acbcc1 100644 --- a/Maths/GetEuclidGCD.js +++ b/Maths/GetEuclidGCD.js @@ -1,32 +1,20 @@ -/* - Problem statement and Explanation : https://en.wikipedia.org/wiki/Euclidean_algorithm - - In this method, we have followed the iterative approach to first - find a minimum of both numbers and go to the next step. -*/ - /** - * GetEuclidGCD return the gcd of two numbers using Euclidean algorithm. - * @param {Number} arg1 first argument for gcd - * @param {Number} arg2 second argument for gcd - * @returns return a `gcd` value of both number. + * GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers + * @param {Number} a integer (may be negative) + * @param {Number} b integer (may be negative) + * @returns {Number} Greatest Common Divisor gcd(a, b) */ -const GetEuclidGCD = (arg1, arg2) => { - // firstly, check that input is a number or not. - if (typeof arg1 !== 'number' || typeof arg2 !== 'number') { - return new TypeError('Argument is not a number.') +export function GetEuclidGCD (a, b) { + if (typeof a !== 'number' || typeof b !== 'number') { + throw new TypeError('Arguments must be numbers') } - // check that the input number is not a negative value. - if (arg1 < 1 || arg2 < 1) { - return new TypeError('Argument is a negative number.') + if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0 + a = Math.abs(a) + b = Math.abs(b) + while (b !== 0) { + const rem = a % b + a = b + b = rem } - // Find a minimum of both numbers. - let less = arg1 > arg2 ? arg2 : arg1 - // Iterate the number and find the gcd of the number using the above explanation. - for (less; less >= 2; less--) { - if ((arg1 % less === 0) && (arg2 % less === 0)) return (less) - } - return (less) + return a } - -export { GetEuclidGCD }