2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
11
Task/Caesar-cipher/JavaScript/caesar-cipher-1.js
Normal file
11
Task/Caesar-cipher/JavaScript/caesar-cipher-1.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function caesar (text, shift) {
|
||||
return text.toUpperCase().replace(/[^A-Z]/g,'').replace(/[A-Z]/g, function(a) {
|
||||
return String.fromCharCode(65+(a.charCodeAt(0)-65+shift)%26);
|
||||
});
|
||||
}
|
||||
|
||||
// Tests
|
||||
var text = 'veni, vidi, vici';
|
||||
for (var i = 0; i<26; i++) {
|
||||
console.log(i+': '+caesar(text,i));
|
||||
}
|
||||
5
Task/Caesar-cipher/JavaScript/caesar-cipher-2.js
Normal file
5
Task/Caesar-cipher/JavaScript/caesar-cipher-2.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var caesar = (text, shift) => text
|
||||
.toUpperCase()
|
||||
.replace(/[^A-Z]/g, '')
|
||||
.replace(/[A-Z]/g, a =>
|
||||
String.fromCharCode(65 + (a.charCodeAt(0) - 65 + shift) % 26));
|
||||
43
Task/Caesar-cipher/JavaScript/caesar-cipher-3.js
Normal file
43
Task/Caesar-cipher/JavaScript/caesar-cipher-3.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
((key, strPlain) => {
|
||||
|
||||
// Int -> String -> String
|
||||
let caesar = (k, s) => s.split('')
|
||||
.map(c => tr(
|
||||
inRange(['a', 'z'], c) ? 'a' :
|
||||
inRange(['A', 'Z'], c) ? 'A' : 0,
|
||||
k, c
|
||||
))
|
||||
.join('');
|
||||
|
||||
// Int -> String -> String
|
||||
let unCaesar = (k, s) => caesar(26 - (k % 26), s);
|
||||
|
||||
// Char -> Int -> Char -> Char
|
||||
let tr = (base, offset, char) =>
|
||||
base ? (
|
||||
String.fromCharCode(
|
||||
ord(base) + (
|
||||
ord(char) - ord(base) + offset
|
||||
) % 26
|
||||
)
|
||||
) : char;
|
||||
|
||||
// [a, a] -> a -> b
|
||||
let inRange = ([min, max], v) => !(v < min || v > max);
|
||||
|
||||
// Char -> Int
|
||||
let ord = c => c.charCodeAt(0);
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
let range = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// TEST
|
||||
let strCipher = caesar(key, strPlain),
|
||||
strDecode = unCaesar(key, strCipher);
|
||||
|
||||
return [strCipher, ' -> ', strDecode];
|
||||
|
||||
})(114, 'Curio, Cesare venne, e vide e vinse ? ');
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
<html><head><title>Caesar</title></head>
|
||||
<body><pre id='x'></pre>
|
||||
<script type="application/javascript">
|
||||
function disp(x) {
|
||||
var e = document.createTextNode(x + '\n');
|
||||
document.getElementById('x').appendChild(e);
|
||||
}
|
||||
|
||||
function trans(msg, rot) {
|
||||
return msg.replace(/([a-z])/ig,
|
||||
function($1) {
|
||||
var c = $1.charCodeAt(0);
|
||||
return String.fromCharCode(
|
||||
c >= 97 ? (c + rot + 26 - 97) % 26 + 97
|
||||
: (c + rot + 26 - 65) % 26 + 65);
|
||||
});
|
||||
}
|
||||
|
||||
var msg = "The quick brown f0x Jumped over the lazy Dog 123";
|
||||
var enc = trans(msg, 3);
|
||||
var dec = trans(enc, -3);
|
||||
|
||||
disp("Original:" + msg + "\nEncoded: " + enc + "\nDecoded: " + dec);
|
||||
</script></body></html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue