Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Classics #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
ashokdey merged 12 commits into master from classics
Sep 22, 2019
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
12f9ba2
--update: initial implementation
ashokdey Sep 8, 2019
c376de5
--fix: edge case of upper case alphabet
ashokdey Sep 8, 2019
c801b3e
--fix: missing case for -ve new index
ashokdey Sep 8, 2019
76b9ace
--update: final implementation
ashokdey Sep 8, 2019
408dc13
--fix: spell fix
ashokdey Sep 8, 2019
479e925
--update: get pair of index for two numbers sum to N
ashokdey Sep 9, 2019
20280c8
--update: replaced indexof with Map(), throw error on missing arguments
ashokdey Sep 9, 2019
c2eec66
--update: added fibonacci
ashokdey Sep 9, 2019
dbd3dbd
--update: added optimized fibonacci using cache
ashokdey Sep 9, 2019
8a9d8b8
--update: 32-bit signed int reversal
ashokdey Sep 10, 2019
f04d86c
--fix: removed outdated files
ashokdey Sep 22, 2019
602df1f
--fix-conflict: got files from master
ashokdey Sep 22, 2019
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
47 changes: 47 additions & 0 deletions src/_Classics_/caeser_cipher/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Most simplest encryption scheme. Read more: [http://practicalcryptography.com/ciphers/caesar-cipher/]
* @param {String} str
* @param {Number} num
*/

function caeserCipher(str, num) {
const lowerCaseString = str.toLowerCase();
const alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('');
const totalAlphabets = alphabets.length;
let result = '';

// handle large number, like 300 or -300
num %= totalAlphabets;

for (let index in lowerCaseString) {
// get the current character
const currentCharacter = lowerCaseString[index];

// if character is space, add it to the result and continue to next
if (currentCharacter === ' ') {
result += currentCharacter;
continue;
}

// determine the new index
const currentIndex = alphabets.indexOf(currentCharacter);
Copy link
Member

@TheSTL TheSTL Sep 8, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use Map to find index of currentCharacter instead of indexOf.

  • With indexOf complexity will be O(n*26).
  • With Map complexity will be O(n).

Copy link
Member Author

@ashokdey ashokdey Sep 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheSTL, any example snippet would be helpful.

Copy link
Member

@TheSTL TheSTL Sep 8, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ashokdey reacted with thumbs up emoji
let newIndex = currentIndex + num;

// if the index passes 25, restart from 0
if (newIndex > totalAlphabets - 1) {
newIndex -= totalAlphabets;
}

if (newIndex < 0) {
newIndex = totalAlphabets + newIndex;
}

// check if the character in original string was upper case
if (str[index] === str[index].toUpperCase()) {
result += alphabets[newIndex].toUpperCase();
} else {
result += alphabets[newIndex];
}
}
return result;
}

AltStyle によって変換されたページ (->オリジナル) /