-
Notifications
You must be signed in to change notification settings - Fork 18
Cracking Shakespeare #79
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
Open
KengiCo
wants to merge
3
commits into
tomorrowdevs-projects:main
Choose a base branch
from
KengiCo:cracking-shakespear
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")); | ||
| 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
61
challenges/easy/caesar-cipher/solutions/kengi/solutionWithProxy.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 added
BIN
+6 KB
challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions
1
challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/easy.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| just a few words to see if it works |
57 changes: 57 additions & 0 deletions
57
challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/main.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
117 changes: 117 additions & 0 deletions
117
challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "dependencies": { | ||
| "fetch": "^1.1.0", | ||
| "fs": "^0.0.1-security" | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
challenges/intermediate/crack-caesar-cipher/cases/solutions/kengi/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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