Skip to content

Hacker Rank, Codewars and project Euler sites and Cracking the Code Interview book.

Notifications You must be signed in to change notification settings

neohed/hacker-rank

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Readme

  • Hacker Rank
  • Codewars
  • Project Euler
  • Cracking the Code Interview book.

First Variation on Caesar Cipher

challenge

const CAESAR_V = 5;
const [A_CODE, Z_CODE, a_CODE, z_CODE] = ['A', 'Z', 'a', 'z'].map(c => c.charCodeAt(0));
const mod26 = n => (n % 26 + 26) % 26;
const nShift = (sign, shift) => (c, offset) => {
    const code = c.charCodeAt(0);
    let base;

    if (A_CODE <= code && code <= Z_CODE) {
        base = A_CODE;
    } else if (a_CODE <= code && code <= z_CODE) {
        base = a_CODE;
    } else {
        return c;
    }

    return String.fromCharCode(mod26(code - base + (shift + offset) * sign) + base);
}
const applyCipher = cipherStrategy => text => text.split('').map(cipherStrategy).join('');
const splitBuckets = (text, bucketSize) => text.match(new RegExp(`.{1,${bucketSize}}`, 'g'));

const encode = applyCipher(nShift(1, CAESAR_V));
const decode = applyCipher(nShift(-1, CAESAR_V));
const plainText = 'This is a secret...';
const cipherText = encode(plainText);
const decoded = decode(cipherText);
const buckets = splitBuckets(decoded, 5);

full code

Shortcut keys (hotkeys) allowed are:

alt/option + R : Run code alt/option + Enter : Submit code alt/option + F : Enable full screen Esc : Restore full screen

Resources

About

Hacker Rank, Codewars and project Euler sites and Cracking the Code Interview book.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published