Created
April 4, 2018 10:38
-
-
Save ff8c00/c020811d65266362fb5e57b1c28fdbea to your computer and use it in GitHub Desktop.
Reddit Daily Programmer 355 Easy
This file contains 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
<html> | |
<head> | |
<script> | |
function encode(keyword, message) { | |
var alphabet, table, i, result, j, k; | |
alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); | |
table = []; | |
for (i = 0; i < alphabet.length; i++) { | |
table.push(alphabet.slice()); | |
alphabet.push(alphabet.shift()); | |
} | |
result = []; | |
for (i = 0; i < message.length; i++) { | |
j = alphabet.indexOf(keyword[i % keyword.length]); | |
k = alphabet.indexOf(message[i]); | |
result.push(table[j][k]); | |
} | |
return result; | |
} | |
function decode(keyword, message) { | |
var alphabet, table, i, result, j, k; | |
alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); | |
table = []; | |
for (i = 0; i < alphabet.length; i++) { | |
table.push(alphabet.slice()); | |
alphabet.push(alphabet.shift()); | |
} | |
result = []; | |
for (i = 0; i < message.length; i++) { | |
j = alphabet.indexOf(keyword[i % keyword.length]); | |
k = table[j].indexOf(message[i]); | |
result.push(alphabet[k]); | |
} | |
return result; | |
} | |
function run() { | |
var keyword = "python".split(""); | |
var message = "pjphmfamhrcaifxifvvfmzwqtmyswst".split(""); | |
return decode(keyword, message).join(""); | |
} | |
</script> | |
</head> | |
<body onload="document.write(run())"> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment