From 040cfc00ac3686f88a72444e812439b0e9327a05 Mon Sep 17 00:00:00 2001 From: KengiCo Date: Sun, 24 Oct 2021 14:52:54 +0200 Subject: [PATCH 1/2] tried 2 solutions, the second one implements proxy to have an easier life handling negative array indexes and has some better formatting --- .../caesar-cipher/solutions/kengi/solution.js | 58 ++++++++++++++++++ .../solutions/kengi/solutionWithProxy.js | 61 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 challenges/easy/caesar-cipher/solutions/kengi/solution.js create mode 100644 challenges/easy/caesar-cipher/solutions/kengi/solutionWithProxy.js 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' From 7810248e9792d247a1a5e972ee32dfbd14b20af0 Mon Sep 17 00:00:00 2001 From: KengiCo Date: Mon, 1 Nov 2021 15:02:37 +0100 Subject: [PATCH 2/2] not a very refined solution but it works nonetheless, takes the most frequent letter of an encrypted text and find the key for decrypting it by confronting it with the most frequent letter that appears in english --- .../cases/solutions/kengi/.DS_Store | Bin 0 -> 6148 bytes .../cases/solutions/kengi/easy.txt | 1 + .../cases/solutions/kengi/main.js | 57 +++++++++ .../cases/solutions/kengi/package-lock.json | 117 ++++++++++++++++++ .../cases/solutions/kengi/package.json | 6 + .../cases/solutions/kengi/solution.js | 58 +++++++++ .../solutions/kengi/solutionWithProxy.js | 63 ++++++++++ 7 files changed, 302 insertions(+) create mode 100644 challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/.DS_Store create mode 100644 challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/easy.txt create mode 100644 challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/main.js create mode 100644 challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package-lock.json create mode 100644 challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package.json create mode 100644 challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solution.js create mode 100644 challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solutionWithProxy.js 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 0000000000000000000000000000000000000000..3978d1ba59a7794aa107d6a8bcf3ea955324dc84 GIT binary patch literal 6148 zcmeHKOHRWu5S@V{1%Xi5vGkT@ZxE_*f*b%;Qk6M2o9L>Y!4i;@sACtZ6Qd;sLI#}zfyQB522iH&}v zNoqf)HdMW!?fMtg`{lH{>6&#Y{N((7S8UUj<=l63#AMIq%Uk~Rda*mc{#N|@cB_`t zM=9x$derT~_C2kjwbVaUcdhT*CA|1WOpF0zz!)$FjDZ9LdP8JK37?sa0b}5QGa%=K z!w@VKhlt_mz!X{l-~{F%(507<9H&?)4iVvja7+cpRMr-QV>;|k;|j$gVoWF2=7aUg zpUn%0r(=H?!ikHBHX8%Rz?gykxXk7Lzr1AmMGo|IKN!%OMi`sMZH xu5}pK7$OokhysB=`Ut>6&XKd=jD9CN;tItfqU<8}kPfsDfk22&#=s9S@Byt6KBWKv literal 0 HcmV?d00001 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'