diff --git a/challenges/easy/caesar-cipher/solutions/kengi/solution.js b/challenges/easy/caesar-cipher/solutions/kengi/solution.js new file mode 100644 index 0000000..f49382d --- /dev/null +++ b/challenges/easy/caesar-cipher/solutions/kengi/solution.js @@ -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")); +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' diff --git a/challenges/easy/caesar-cipher/solutions/kengi/solutionWithProxy.js b/challenges/easy/caesar-cipher/solutions/kengi/solutionWithProxy.js new file mode 100644 index 0000000..b18e7d9 --- /dev/null +++ b/challenges/easy/caesar-cipher/solutions/kengi/solutionWithProxy.js @@ -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' diff --git a/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/.DS_Store b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/.DS_Store new file mode 100644 index 0000000..3978d1b Binary files /dev/null and b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/.DS_Store differ diff --git a/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/easy.txt b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/easy.txt new file mode 100644 index 0000000..e0faaf8 --- /dev/null +++ b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/easy.txt @@ -0,0 +1 @@ +just a few words to see if it works \ No newline at end of file diff --git a/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/main.js b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/main.js new file mode 100644 index 0000000..1e683a8 --- /dev/null +++ b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/main.js @@ -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)) + + + + + diff --git a/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package-lock.json b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package-lock.json new file mode 100644 index 0000000..ff95bb1 --- /dev/null +++ b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package-lock.json @@ -0,0 +1,117 @@ +{ + "name": "cases", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "dependencies": { + "fetch": "^1.1.0", + "fs": "^0.0.1-security" + } + }, + "node_modules/biskviit": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/biskviit/-/biskviit-1.0.1.tgz", + "integrity": "sha1-A3oM1LcbnjMf2QoRIt4X3EnkIKc=", + "dependencies": { + "psl": "^1.1.7" + }, + "engines": { + "node": ">=1.0.0" + } + }, + "node_modules/encoding": { + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz", + "integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=", + "dependencies": { + "iconv-lite": "~0.4.13" + } + }, + "node_modules/fetch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fetch/-/fetch-1.1.0.tgz", + "integrity": "sha1-CoJ58Gvjf58Ou1Z1YKMKSA2lmi4=", + "dependencies": { + "biskviit": "1.0.1", + "encoding": "0.1.12" + } + }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" + }, + "node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/psl": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", + "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==" + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + } + }, + "dependencies": { + "biskviit": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/biskviit/-/biskviit-1.0.1.tgz", + "integrity": "sha1-A3oM1LcbnjMf2QoRIt4X3EnkIKc=", + "requires": { + "psl": "^1.1.7" + } + }, + "encoding": { + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz", + "integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=", + "requires": { + "iconv-lite": "~0.4.13" + } + }, + "fetch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fetch/-/fetch-1.1.0.tgz", + "integrity": "sha1-CoJ58Gvjf58Ou1Z1YKMKSA2lmi4=", + "requires": { + "biskviit": "1.0.1", + "encoding": "0.1.12" + } + }, + "fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "psl": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", + "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + } + } +} diff --git a/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package.json b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package.json new file mode 100644 index 0000000..081989f --- /dev/null +++ b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "fetch": "^1.1.0", + "fs": "^0.0.1-security" + } +} diff --git a/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solution.js b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solution.js new file mode 100644 index 0000000..203be9a --- /dev/null +++ b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solution.js @@ -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' diff --git a/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solutionWithProxy.js b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solutionWithProxy.js new file mode 100644 index 0000000..5e40a02 --- /dev/null +++ b/challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solutionWithProxy.js @@ -0,0 +1,63 @@ +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'