Skip to content

Instantly share code, notes, and snippets.

@ff8c00
Created April 4, 2018 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ff8c00/c020811d65266362fb5e57b1c28fdbea to your computer and use it in GitHub Desktop.
Save ff8c00/c020811d65266362fb5e57b1c28fdbea to your computer and use it in GitHub Desktop.
Reddit Daily Programmer 355 Easy
<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