Skip to content

Commit 188a16e

Browse files
authored
Merge branch 'TheAlgorithms:master' into patch-1
2 parents 03008e4 + 4b07e8a commit 188a16e

File tree

6 files changed

+98
-17
lines changed

6 files changed

+98
-17
lines changed

Ciphers/AffineCipher.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
2-
* @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
2+
* @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
33
* @see - [wiki](https://en.wikipedia.org/wiki/Affine_cipher)
44
*/
55

6+
import { CoPrimeCheck } from '../Maths/CoPrimeCheck'
67
// Default key for Affine Cipher
78
const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
89

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

48+
if (!CoPrimeCheck(a, 26)) {
49+
throw new Error(a + ' is not coprime of 26')
50+
}
51+
4752
return true
4853
}
54+
4955
/**
5056
* Find character index based on ASCII order
5157
* @param {String} char - Character index to be found
@@ -62,17 +68,19 @@ function findCharIndex (char) {
6268
* @param {Number} b - B coefficient
6369
* @return {String} result - Encrypted string
6470
*/
71+
6572
function encrypt (str, a, b) {
6673
let result = ''
6774
if (isCorrectFormat(str, a, b)) {
6875
for (let x = 0; x < str.length; x++) {
6976
const charIndex = findCharIndex(str[x])
7077
if (charIndex < 0) result += '-1' + ' '
71-
else result += mod(a * charIndex + b, 26) + ' '
78+
else result += key.charAt(mod(a * charIndex + b, 26)) + ' '
7279
}
7380
}
7481
return result.trim()
7582
}
83+
7684
/**
7785
* Decrypt a Affine Cipher
7886
* @param {String} str - String to be decrypted
@@ -86,10 +94,12 @@ function decrypt (str, a, b) {
8694
str = str.split(' ')
8795
for (let x = 0; x < str.length; x++) {
8896
if (str[x] === '-1') result += ' '
89-
else result += key[mod(inverseMod(a, 26) * (parseInt(str[x]) - b), 26)]
97+
else {
98+
const charIndex = findCharIndex(str[x])
99+
result += key[mod(inverseMod(a, 26) * (charIndex - b), 26)]
100+
}
90101
}
102+
return result
91103
}
92-
return result
93104
}
94-
95105
export { encrypt, decrypt }

Ciphers/test/AffineCipher.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ describe('Test Affine Cipher', () => {
66
expect(() => encrypt('null', null, null)).toThrow()
77
expect(() => encrypt('null', 1, null)).toThrow()
88
expect(() => encrypt('null', null, 1)).toThrow()
9+
expect(() => encrypt('null', 2, 1)).toThrow()
10+
expect(() => encrypt('null', 4, 1)).toThrow()
911
})
1012

1113
it('Test - 2, Pass invalid input to decrypt function', () => {
1214
expect(() => decrypt(null, null, null)).toThrow()
1315
expect(() => decrypt('null', null, null)).toThrow()
1416
expect(() => decrypt('null', 1, null)).toThrow()
1517
expect(() => decrypt('null', null, 1)).toThrow()
18+
expect(() => encrypt('null', 2, 1)).toThrow()
19+
expect(() => encrypt('null', 4, 1)).toThrow()
1620
})
1721

1822
it('Test - 3 Pass string value to encrypt and ecrypt function', () => {
19-
const a = 5
20-
const b = 8
21-
expect(decrypt(encrypt('HELLO WORLD', a, b), a, b)).toBe('HELLO WORLD')
22-
expect(decrypt(encrypt('ABC DEF', a, b), a, b)).toBe('ABC DEF')
23-
expect(decrypt(encrypt('Brown fox jump over the fence', a, b), a, b)).toBe(
23+
expect(decrypt(encrypt('HELLO WORLD', 5, 8), 5, 8)).toBe('HELLO WORLD')
24+
expect(decrypt(encrypt('ABC DEF', 3, 5), 3, 5)).toBe('ABC DEF')
25+
expect(decrypt(encrypt('Brown fox jump over the fence', 7, 3), 7, 3)).toBe(
2426
'BROWN FOX JUMP OVER THE FENCE'
2527
)
2628
})

Graphs/NumberOfIslands.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,3 @@ const islands = (matrixGrid) => {
7979
}
8080

8181
export { islands }
82-
83-
// islands(
84-
// ['1', '1', '0', '0', '0'],
85-
// ['1', '1', '0', '0', '0'],
86-
// ['0', '0', '1', '0', '0'],
87-
// ['0', '0', '0', '1', '1']
88-
// )

Graphs/test/NumberOfIslands.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { islands } from '../NumberOfIslands'
2+
3+
describe('Number of Islands', () => {
4+
test('Graph with three islands', () => {
5+
const graph = [
6+
['1', '1', '0', '0', '0'],
7+
['1', '1', '0', '0', '0'],
8+
['0', '0', '1', '0', '0'],
9+
['0', '0', '0', '1', '1']
10+
]
11+
expect(islands(graph)).toBe(3)
12+
})
13+
14+
test('Graph with only one island', () => {
15+
const graph = [
16+
['1', '1'],
17+
['1', '1'],
18+
['0', '0'],
19+
['0', '0']
20+
]
21+
expect(islands(graph)).toBe(1)
22+
})
23+
24+
test('No islands', () => {
25+
const graph = [
26+
['0', '0'],
27+
['0', '0']
28+
]
29+
expect(islands(graph)).toBe(0)
30+
})
31+
})

Maths/JugglerSequence.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Juggler Sequence: https://en.wikipedia.org/wiki/Juggler_sequence
3+
* function jugglerSequence
4+
* Juggler Sequence is a series of integer number in which the first term starts with a positive integer number n
5+
* and the remaining terms are generated from the immediate previous term using the recurrence relation
6+
* Produce Juggler Sequence using number n as the first term of the sequence and store in an array
7+
* Reference: https://www.geeksforgeeks.org/juggler-sequence/
8+
* jugglerSequence(3) // returns [3, 5, 11, 36, 6, 2, 1 ]
9+
* jugglerSequence(9) // returns [9, 27, 140, 11, 36, 6, 2, 1]
10+
* jugglerSequence(15) // returns [15, 58, 7, 18, 4, 2, 1]
11+
*/
12+
13+
function jugglerSequence (n) {
14+
const sequence = []
15+
sequence.push(n)
16+
// Calculate terms until last term is not 1
17+
while (n !== 1) {
18+
n = Math.floor(n ** ((n % 2) + 0.5))
19+
sequence.push(n)
20+
}
21+
return sequence
22+
}
23+
24+
export { jugglerSequence }

Maths/test/JugglerSequence.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { jugglerSequence } from '../JugglerSequence'
2+
3+
describe('Testing jugglerSequence function', () => {
4+
it('should return [3, 5, 11, 36, 6, 2, 1 ] if the number is 3', () => {
5+
expect(jugglerSequence(3)).toEqual(
6+
expect.arrayContaining([3, 5, 11, 36, 6, 2, 1])
7+
)
8+
})
9+
10+
it('should return [9, 27, 140, 11, 36, 6, 2, 1] if the number is 9', () => {
11+
expect(jugglerSequence(9)).toEqual(
12+
expect.arrayContaining([9, 27, 140, 11, 36, 6, 2, 1])
13+
)
14+
})
15+
16+
it('should return [15, 58, 7, 18, 4, 2, 1] if the number is 15', () => {
17+
expect(jugglerSequence(15)).toEqual(
18+
expect.arrayContaining([15, 58, 7, 18, 4, 2, 1])
19+
)
20+
})
21+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy