This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -1,20 +1,21 @@
import std.stdio, std.traits;
pure S rot(S)(in S s, in int key) if (isSomeString!S) {
auto res = s.dup;
pure S rot(S)(in S s, in int key) pure /*nothrow*/
if (isSomeString!S) {
auto res = s.dup; // Not nothrow.
foreach (i, ref c; res) {
foreach (immutable i, ref c; res) {
if ('a' <= c && c <= 'z')
c = ((c - 'a' + key) % 26 + 'a');
else if ('A' <= c && c <= 'Z')
c = ((c - 'A' + key) % 26 + 'A');
}
return cast(S) res;
return res;
}
void main() {
int key = 3;
auto txt = "The five boxing wizards jump quickly";
enum key = 3;
immutable txt = "The five boxing wizards jump quickly";
writeln("Original: ", txt);
writeln("Encrypted: ", txt.rot(key));
writeln("Decrypted: ", txt.rot(key).rot(26 - key));

View file

@ -0,0 +1,17 @@
import std.stdio, std.ascii, std.string, std.algorithm;
string rot(in string s, in int key) pure /*nothrow*/ {
auto uppr = uppercase.dup.representation;
bringToFront(uppr[0 .. key], uppr[key .. $]);
auto lowr = lowercase.dup.representation;
bringToFront(lowr[0 .. key], lowr[key .. $]);
return s.translate(makeTrans(letters, cast(char[])(uppr ~ lowr)));
}
void main() {
enum key = 3;
immutable txt = "The five boxing wizards jump quickly";
writeln("Original: ", txt);
writeln("Encrypted: ", txt.rot(key));
writeln("Decrypted: ", txt.rot(key).rot(26 - key));
}

View file

@ -0,0 +1,29 @@
class Caesar {
function : native : Encode(enc : String, offset : Int) ~ String {
offset := offset % 26 + 26;
encoded := "";
enc := enc->ToLower();
each(i : enc) {
c := enc->Get(i);
if(c->IsChar()) {
j := (c - 'a' + offset) % 26;
encoded->Append(j + 'a');
}
else {
encoded->Append(c);
};
};
return encoded;
}
function : Decode(enc : String, offset : Int) ~ String {
return Encode(enc, offset * -1);
}
function : Main(args : String[]) ~ Nil {
enc := Encode("The quick brown fox Jumped over the lazy Dog", 12);
enc->PrintLine();
Decode(enc, 12)->PrintLine();
}
}

View file

@ -0,0 +1,19 @@
#lang racket
(define A (char->integer #\A))
(define Z (char->integer #\Z))
(define a (char->integer #\a))
(define z (char->integer #\z))
(define (rotate c n)
(define cnum (char->integer c))
(define (shift base) (integer->char (+ base (modulo (+ n (- cnum base)) 26))))
(cond [(<= A cnum Z) (shift A)]
[(<= a cnum z) (shift a)]
[else c]))
(define (caesar s n)
(list->string (for/list ([c (in-string s)]) (rotate c n))))
(define (encrypt s) (caesar s 1))
(define (decrypt s) (caesar s -1))