Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Straddling-checkerboard/D/straddling-checkerboard-1.d
Normal file
36
Task/Straddling-checkerboard/D/straddling-checkerboard-1.d
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import std.stdio, std.algorithm, std.string, std.array;
|
||||
|
||||
immutable T = ["79|0|1|2|3|4|5|6|7|8|9", "|H|O|L||M|E|S||R|T",
|
||||
"3|A|B|C|D|F|G|I|J|K|N", "7|P|Q|U|V|W|X|Y|Z|.|/"]
|
||||
.map!(r => r.split("|")).array;
|
||||
|
||||
enum straddle = (in string s) pure /*nothrow @safe*/ =>
|
||||
toUpper(s)
|
||||
.split("")
|
||||
.cartesianProduct(T)
|
||||
.filter!(cL => cL[1].canFind(cL[0]))
|
||||
.map!(cL => cL[1][0] ~ T[0][cL[1].countUntil(cL[0])])
|
||||
.join;
|
||||
|
||||
string unStraddle(string s) pure nothrow @safe {
|
||||
string result;
|
||||
for (; !s.empty; s.popFront) {
|
||||
immutable i = [T[2][0], T[3][0]].countUntil([s[0]]);
|
||||
if (i >= 0) {
|
||||
s.popFront;
|
||||
immutable n = T[2 + i][T[0].countUntil([s[0]])];
|
||||
if (n == "/") {
|
||||
s.popFront;
|
||||
result ~= s[0];
|
||||
} else result ~= n;
|
||||
} else
|
||||
result ~= T[1][T[0].countUntil([s[0]])];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable O = "One night-it was on the twentieth of March, 1888-I was returning";
|
||||
writeln("Encoded: ", O.straddle);
|
||||
writeln("Decoded: ", O.straddle.unStraddle);
|
||||
}
|
||||
75
Task/Straddling-checkerboard/D/straddling-checkerboard-2.d
Normal file
75
Task/Straddling-checkerboard/D/straddling-checkerboard-2.d
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import std.stdio, std.algorithm, std.ascii;
|
||||
|
||||
struct StraddlingCheckerboard {
|
||||
private:
|
||||
string[char] table;
|
||||
char[10] first, second, third;
|
||||
const int rowU, rowV;
|
||||
|
||||
public:
|
||||
this(in string alphabet, in int u, in int v) pure nothrow {
|
||||
rowU = min(u, v);
|
||||
rowV = max(u, v);
|
||||
|
||||
int j = 0;
|
||||
foreach (immutable i; 0 .. 10) {
|
||||
if (i != u && i != v) {
|
||||
first[i] = alphabet[j];
|
||||
table[alphabet[j]] = digits[i .. i + 1];
|
||||
j++;
|
||||
}
|
||||
|
||||
second[i] = alphabet[i + 8];
|
||||
table[alphabet[i + 8]] = [digits[rowU], digits[i]];
|
||||
|
||||
third[i] = alphabet[i + 18];
|
||||
table[alphabet[i + 18]] = [digits[rowV], digits[i]];
|
||||
}
|
||||
}
|
||||
|
||||
string encode(in string plain) const pure nothrow {
|
||||
string r;
|
||||
foreach (immutable char c; plain) {
|
||||
if (c.isLower) r ~= table[c.toUpper];
|
||||
else if (c.isUpper) r ~= table[c];
|
||||
else if (c.isDigit) r ~= table['/'] ~ c;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
string decode(in string cipher) const pure nothrow {
|
||||
string r;
|
||||
int state = 0;
|
||||
|
||||
foreach (immutable char c; cipher) {
|
||||
immutable int n = c - '0';
|
||||
char next = '\0';
|
||||
|
||||
if (state == 1) next = second[n];
|
||||
else if (state == 2) next = third[n];
|
||||
else if (state == 3) next = c;
|
||||
else if (n == rowU) state = 1;
|
||||
else if (n == rowV) state = 2;
|
||||
else next = first[n];
|
||||
|
||||
if (next == '/')
|
||||
state = 3;
|
||||
else if (next != 0) {
|
||||
state = 0;
|
||||
r ~= next;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable orig =
|
||||
"One night-it was on the twentieth of March, 1888-I was returning";
|
||||
writeln("Original: ", orig);
|
||||
const sc = StraddlingCheckerboard("HOLMESRTABCDFGIJKNPQUVWXYZ./",
|
||||
3, 7);
|
||||
const en = sc.encode(orig);
|
||||
writeln("Encoded: ", en);
|
||||
writeln("Decoded: ", sc.decode(en));
|
||||
}
|
||||
28
Task/Straddling-checkerboard/D/straddling-checkerboard-3.d
Normal file
28
Task/Straddling-checkerboard/D/straddling-checkerboard-3.d
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import std.stdio, std.string, std.algorithm, std.regex, std.array, std.range, std.typecons;
|
||||
|
||||
immutable string[const string] val2key, key2val;
|
||||
|
||||
static this() pure /*nothrow @safe*/ {
|
||||
immutable aa = ["A":"30", "B":"31", "C":"32", "D":"33", "E":"5", "F":"34", "G":"35",
|
||||
"H":"0", "I":"36", "J":"37", "K":"38", "L":"2", "M":"4", ".":"78", "N":"39",
|
||||
"/":"79", "O":"1", "0":"790", "P":"70", "1":"791", "Q":"71", "2":"792",
|
||||
"R":"8", "3":"793", "S":"6", "4":"794", "T":"9", "5":"795", "U":"72",
|
||||
"6":"796", "V":"73", "7":"797", "W":"74", "8":"798", "X":"75", "9":"799",
|
||||
"Y":"76", "Z":"77"];
|
||||
val2key = aa;
|
||||
key2val = aa.byKeyValue.map!(t => tuple(t.value, t.key)).assocArray;
|
||||
}
|
||||
|
||||
string encode(in string s) pure /*nothrow*/ @safe {
|
||||
return s.toUpper.split("").map!(c => val2key.get(c, "")).join;
|
||||
}
|
||||
|
||||
string decode(in string s) /*pure nothrow*/ @safe {
|
||||
return s.matchAll("79.|3.|7.|.").map!(g => key2val.get(g[0], "")).join;
|
||||
}
|
||||
|
||||
void main() @safe {
|
||||
immutable s = "One night-it was on the twentieth of March, 1888-I was returning";
|
||||
s.encode.writeln;
|
||||
s.encode.decode.writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue