From bd7475ee1921fedc0d0272b7ae331a95451023fc Mon Sep 17 00:00:00 2001 From: gifted-s <52675260+Gifted-s@users.noreply.github.com> Date: Sat, 8 Aug 2020 10:51:22 +0100 Subject: [PATCH] Caeser cipher (#517) * added ceaserCipher algorithm * added ceaserCipher algorithm * fixed a typo --- src/algorithms/string/caeserCipher/README.md | 15 ++++++++++ .../__test__/caeserCipher.test.js | 27 +++++++++++++++++ .../string/caeserCipher/caeserCipher.js | 30 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 src/algorithms/string/caeserCipher/README.md create mode 100644 src/algorithms/string/caeserCipher/__test__/caeserCipher.test.js create mode 100644 src/algorithms/string/caeserCipher/caeserCipher.js diff --git a/src/algorithms/string/caeserCipher/README.md b/src/algorithms/string/caeserCipher/README.md new file mode 100644 index 00000000..9bd84796 --- /dev/null +++ b/src/algorithms/string/caeserCipher/README.md @@ -0,0 +1,15 @@ +# caeserCipher Algorithm + +caeserCipher Algorithm is a type of substitution algorithm in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on + +## Complexity + +- **Time:** `O(|n|)` +- **Space:** `O(|n|)` +## Example + +The the following string `abcde` which is shifted by 1 will change to `bcdef` + +## References + +- [Wikipedia](https://en.wikipedia.org/wiki/Caesar_cipher) diff --git a/src/algorithms/string/caeserCipher/__test__/caeserCipher.test.js b/src/algorithms/string/caeserCipher/__test__/caeserCipher.test.js new file mode 100644 index 00000000..f852de99 --- /dev/null +++ b/src/algorithms/string/caeserCipher/__test__/caeserCipher.test.js @@ -0,0 +1,27 @@ +import caeserCipher from '../caeserCipher'; + +describe('caeserCipher', () => { + it('should subtitute each character by shifting up the alphabet by a given integer', () => { + + + expect(caeserCipher('abcd', 1)).toBe('bcde'); + }); + + + it('should wrap back to the beginning of the alphabet if it shifts more than the 26 english alphabets', () => { + + + expect(caeserCipher('xyz', 1)).toBe('yza'); + }); + it('should only shift letters and ignore non-alphabetic characters', () => { + + expect(caeserCipher('gurer ner 9 qbtf!', 13)).toBe('there are 9 dogs!'); + }); + + it('should ignore case sensitivity', () => { + + expect(caeserCipher('ABCD', 13)).toBe('bcde'); + }); + + +}) \ No newline at end of file diff --git a/src/algorithms/string/caeserCipher/caeserCipher.js b/src/algorithms/string/caeserCipher/caeserCipher.js new file mode 100644 index 00000000..47d6abf1 --- /dev/null +++ b/src/algorithms/string/caeserCipher/caeserCipher.js @@ -0,0 +1,30 @@ +/** + * @param {string} string + * @param {number} shift + * @return {string} result + */ + +export default function caeserCipher(string, shift) { + // convert all alphabets in english language to an array + const alphabetArr = "abcdefghijklmnopqrstuvwxyz".split("") + // converting all the alphabets in string to lowercase + string = string.toLowerCase() + + let result = "" + for (let i = 0; i < string.length; i++) { + const currentChar = string[i] + // checking index of character in the english alphabets + const idx = alphabetArr.indexOf(currentChar) + // skip character if the character is not an alphabet + if (idx === -1) { + result += currentChar + continue; + } + //wrap up index, incase the next shift is beyond the 26th character + const encodedIdx = (idx + shift) % 26 + result += alphabetArr[encodedIdx] + + } + // return the result of the shifted string + return result +}