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
20 changes: 15 additions & 5 deletions Ciphers/AffineCipher.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* @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
* @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)
*/

import { CoPrimeCheck } from '../Maths/CoPrimeCheck'
// Default key for Affine Cipher
const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

Expand Down Expand Up @@ -44,8 +45,13 @@ function isCorrectFormat (str, a, b) {
throw new TypeError('Argument str should be String')
}

if (!CoPrimeCheck(a, 26)) {
throw new Error(a + ' is not coprime of 26')
}

return true
}

/**
* Find character index based on ASCII order
* @param {String} char - Character index to be found
Expand All @@ -62,17 +68,19 @@ function findCharIndex (char) {
* @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) + ' '
else result += key.charAt(mod(a * charIndex + b, 26)) + ' '
}
}
return result.trim()
}

/**
* Decrypt a Affine Cipher
* @param {String} str - String to be decrypted
Expand All @@ -86,10 +94,12 @@ function decrypt (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)]
else {
const charIndex = findCharIndex(str[x])
result += key[mod(inverseMod(a, 26) * (charIndex - b), 26)]
}
}
return result
}
return result
}

export { encrypt, decrypt }
12 changes: 7 additions & 5 deletions Ciphers/test/AffineCipher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ describe('Test Affine Cipher', () => {
expect(() => encrypt('null', null, null)).toThrow()
expect(() => encrypt('null', 1, null)).toThrow()
expect(() => encrypt('null', null, 1)).toThrow()
expect(() => encrypt('null', 2, 1)).toThrow()
expect(() => encrypt('null', 4, 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()
expect(() => encrypt('null', 2, 1)).toThrow()
expect(() => encrypt('null', 4, 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(
expect(decrypt(encrypt('HELLO WORLD', 5, 8), 5, 8)).toBe('HELLO WORLD')
expect(decrypt(encrypt('ABC DEF', 3, 5), 3, 5)).toBe('ABC DEF')
expect(decrypt(encrypt('Brown fox jump over the fence', 7, 3), 7, 3)).toBe(
'BROWN FOX JUMP OVER THE FENCE'
)
})
Expand Down
24 changes: 24 additions & 0 deletions Maths/JugglerSequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Juggler Sequence: https://en.wikipedia.org/wiki/Juggler_sequence
* function jugglerSequence
* Juggler Sequence is a series of integer number in which the first term starts with a positive integer number n
* and the remaining terms are generated from the immediate previous term using the recurrence relation
* Produce Juggler Sequence using number n as the first term of the sequence and store in an array
* Reference: https://www.geeksforgeeks.org/juggler-sequence/
* jugglerSequence(3) // returns [3, 5, 11, 36, 6, 2, 1 ]
* jugglerSequence(9) // returns [9, 27, 140, 11, 36, 6, 2, 1]
* jugglerSequence(15) // returns [15, 58, 7, 18, 4, 2, 1]
*/

function jugglerSequence (n) {
const sequence = []
sequence.push(n)
// Calculate terms until last term is not 1
while (n !== 1) {
n = Math.floor(n ** ((n % 2) + 0.5))
sequence.push(n)
}
return sequence
}

export { jugglerSequence }
21 changes: 21 additions & 0 deletions Maths/test/JugglerSequence.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { jugglerSequence } from '../JugglerSequence'

describe('Testing jugglerSequence function', () => {
it('should return [3, 5, 11, 36, 6, 2, 1 ] if the number is 3', () => {
expect(jugglerSequence(3)).toEqual(
expect.arrayContaining([3, 5, 11, 36, 6, 2, 1])
)
})

it('should return [9, 27, 140, 11, 36, 6, 2, 1] if the number is 9', () => {
expect(jugglerSequence(9)).toEqual(
expect.arrayContaining([9, 27, 140, 11, 36, 6, 2, 1])
)
})

it('should return [15, 58, 7, 18, 4, 2, 1] if the number is 15', () => {
expect(jugglerSequence(15)).toEqual(
expect.arrayContaining([15, 58, 7, 18, 4, 2, 1])
)
})
})