Skip to content
Open
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
58 changes: 58 additions & 0 deletions challenges/easy/caesar-cipher/solutions/kengi/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use a constant when you're not willing to re-instantiate the variable values

let alphabetCapital = alphabet.map((x) => x.toUpperCase());

const encode = (string, shift) => {
// set empty array to push resulted encrypted letters or symbol
let cypher = [];
for (let i = 0; i < string.length; i++) {
const letter = string[i];

// if the shift coefficient plus the letter index are over 26 reduce them in the 0 - 25 range
let num = (alphabet.indexOf(letter.toLowerCase()) + shift) % 26;

// everything that is not a letter, push it in the encrypted solution as it is
if (!alphabet.includes(letter.toLowerCase())) {
cypher.push(letter);
}
// if the index of the letter summed with shift number provided is positive
else if (num >= 0) {
// finds in the alphabet the letter with the index of
// the given letter plus the shift number.
// the nested if statement is for taking care of capital letters
if (alphabet.includes(letter)) {
cypher.push(alphabet[num]);
} else if (alphabetCapital.includes(letter)) {
cypher.push(alphabetCapital[num]);
}
}
// if the index of the letter minus the shift number provided is negative
else if (num < 0) {
if (alphabet.includes(letter)) {
console.log(num);
console.log(alphabet[alphabet.length + num]);
cypher.push(alphabet[alphabet.length + num]);
} else if (alphabetCapital.includes(letter)) {
cypher.push(alphabetCapital[alphabet.length + num]);
}
}
}
return cypher.join("");
};

const decode = (string, shift) => {
let flipShift = shift > 0 ? -shift : Math.abs(shift);
return encode(string, flipShift);
};

console.log(encode("f", -10));
console.log(alphabet.indexOf("f"));
encode("Hello, world!", 7); // => 'Olssv, Dvysk!'
encode("I love pizza.", -7); // => 'B ehox ibsst.'
encode("HytyQapgnr kyicq qclqc. Qmkcrgkcq.", 50);
// => 'HytyQapgnr kyicq qclqc. Qmkcrgkcq.'
encode("abc", 24); // => 'yza'

decode("CxvxaaxfMneb Axltb!", 9); // => 'TomorrowDevs Rocks!'
decode("Gtdflw Defotz Nzop td rcple!!!", -15); // => 'Visual Studio Code is great!!!'
decode("Buj'i mhyju jxu syfxuh.", 120); // => 'Let's write the cipher.'
decode("Vriwzduh Hqjlqhhulqj", 3); // => 'Software Engineering'
61 changes: 61 additions & 0 deletions challenges/easy/caesar-cipher/solutions/kengi/solutionWithProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",];
let alphabetCapital = alphabet.map((x) => x.toUpperCase());


// https://medium.com/uncaught-exception/javascript-array-negative-index-using-proxies-ed096dc84416
// create a proxy for the properties of alphabet, check if the prop is negative and transform it
// in a valid array index value
const proxy = new Proxy(alphabet, {
get(target, prop) {
if (!isNaN(prop)) {
//converts in number as prop is passed as a string
prop = parseInt(prop, 10);
if (prop < 0) {
prop += target.length;
}
}
return target[prop];
}
});

const encode = (string, shift) => {
// set empty array to push resulted encrypted letters or symbol
let cypher = [];
for (letter of string) {

// if the shift coefficient plus the letter index are over 26 reduce them in the 0 - 25 range
let num = (alphabet.indexOf(letter.toLowerCase()) + shift) % 26;

// everything that is not a letter, push it in the encrypted solution as it is
if (!alphabet.includes(letter.toLowerCase())) {
cypher.push(letter);
}
// lowercase
else if (alphabet.includes(letter)) {
cypher.push(proxy[num]);
}
// uppercase
else if (alphabetCapital.includes(letter)) {
cypher.push(proxy[num].toUpperCase());
}
}
return cypher.join("");
};

// flip sign at the shift parameter to use encode() to decode
const decode = (string, shift) => {
let flipShift = shift > 0 ? -shift : Math.abs(shift);
return encode(string, flipShift);
};

console.log(encode("abcdefghiABCDEFGHI", -10));
console.log(encode("Hello, World!", 7)); // => 'Olssv, Dvysk!'
encode("I love pizza.", -7); // => 'B ehox ibsst.'
console.log(decode("HytyQapgnr kyicq qclqc. Qmkcrgkcq.", 50))
// => 'JavaScript makes sense. Sometimes.'
encode("abc", 24); // => 'yza'

decode("CxvxaaxfMneb Axltb!", 9); // => 'TomorrowDevs Rocks!'
decode("Gtdflw Defotz Nzop td rcple!!!", -15); // => 'Visual Studio Code is great!!!'
decode("Buj'i mhyju jxu syfxuh.", 120); // => 'Let's write the cipher.'
decode("Vriwzduh Hqjlqhhulqj", 3); // => 'Software Engineering'
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
just a few words to see if it works
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",];
let alphabetCapital = alphabet.map((x) => x.toUpperCase());

// Requiring fs module in which
// readFile function is defined.
const fs = require('fs')
import {decode} from "./solutionWithProxy.js"

let text = []
const readFileLines = filename =>
fs.readFileSync(filename)
.toString('UTF8')
.toLowerCase()
.split("");

// Calling the readFiles function with file name
// let words = readFileLines('../../Shakespeare-Macbeth.txt'); // key = 4
// let words = readFileLines('../../Shakespeare-Romeo-And-Juliet.txt'); // key = 12
let words = readFileLines('../../Shakespeare-Hamlet.txt'); // key = 20



// Printing the response array
let occurrences = {};
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (alphabet.includes(word)){
if(occurrences[word]) {
occurrences[word] += 1
}
else {
occurrences[word] = 1
}}
}

// FROM DIFFERENT RESOURCES LETTER "E" IS THE ONE THAT APPEARS THE MOST ON AVERAGE IN A LONG ENGLISH TEXT

let mostFrequentLetterInEnglish = "e"

//TAKE ALL THE LETTERS AND THEIR OCCURENCIES, SORT THEM AND GRAB THE LETTER THAT OCCURS THE MOST

const topLetterInText = Object.entries(occurrences).sort((a,b) => b[1] - a[1])[0][0]

console.log(topLetterInText);

// THE DIFFERENCE BETWEEN THE POSITION OF THE MOST FREQUENT LETTER IN TEXT WITH THE POSITION
// OF THE MOST FREQUENT LETTER IN ENGLISH IS OUR KEY TO DECODE THE TEXT

let key = alphabet.indexOf(topLetterInText) - alphabet.indexOf(mostFrequentLetterInEnglish)

console.log(key)
console.log(decode(words, key))





Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"fetch": "^1.1.0",
"fs": "^0.0.1-security"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",];
let alphabetCapital = alphabet.map((x) => x.toUpperCase());

const encode = (string, shift) => {
// set empty array to push resulted encrypted letters or symbol
let cypher = [];
for (let i = 0; i < string.length; i++) {
const letter = string[i];

// if the shift coefficient plus the letter index are over 26 reduce them in the 0 - 25 range
let num = (alphabet.indexOf(letter.toLowerCase()) + shift) % 26;

// everything that is not a letter, push it in the encrypted solution as it is
if (!alphabet.includes(letter.toLowerCase())) {
cypher.push(letter);
}
// if the index of the letter summed with shift number provided is positive
else if (num >= 0) {
// finds in the alphabet the letter with the index of
// the given letter plus the shift number.
// the nested if statement is for taking care of capital letters
if (alphabet.includes(letter)) {
cypher.push(alphabet[num]);
} else if (alphabetCapital.includes(letter)) {
cypher.push(alphabetCapital[num]);
}
}
// if the index of the letter minus the shift number provided is negative
else if (num < 0) {
if (alphabet.includes(letter)) {
console.log(num);
console.log(alphabet[alphabet.length + num]);
cypher.push(alphabet[alphabet.length + num]);
} else if (alphabetCapital.includes(letter)) {
cypher.push(alphabetCapital[alphabet.length + num]);
}
}
}
return cypher.join("");
};

const decode = (string, shift) => {
let flipShift = shift > 0 ? -shift : Math.abs(shift);
return encode(string, flipShift);
};

console.log(encode("f", -10));
console.log(alphabet.indexOf("f"));
console.log(encode("Hello, world!", 7)); // => 'Olssv, Dvysk!'
encode("I love pizza.", -7); // => 'B ehox ibsst.'
encode("HytyQapgnr kyicq qclqc. Qmkcrgkcq.", 50);
// => 'HytyQapgnr kyicq qclqc. Qmkcrgkcq.'
encode("abc", 24); // => 'yza'

decode("CxvxaaxfMneb Axltb!", 9); // => 'TomorrowDevs Rocks!'
decode("Gtdflw Defotz Nzop td rcple!!!", -15); // => 'Visual Studio Code is great!!!'
decode("Buj'i mhyju jxu syfxuh.", 120); // => 'Let's write the cipher.'
decode("Vriwzduh Hqjlqhhulqj", 3); // => 'Software Engineering'
Loading