Straddling checkerboard: Difference between revisions

Content added Content deleted
(+ third D entry)
(Updated D entry)
Line 728: Line 728:
===Dictionary-Based Version===
===Dictionary-Based Version===
{{trans|Java}}
{{trans|Java}}
<lang d>import std.stdio, std.string, std.algorithm, std.regex, std.array, std.range;
<lang d>import std.stdio, std.string, std.algorithm, std.regex, std.array, std.range, std.typecons;


immutable string[const string] val2key, key2val;
immutable string[const string] val2key, key2val;


static this() pure /*@safe*/ {
static this() pure /*nothrow @safe*/ {
val2key = ["A":"30", "B":"31", "C":"32", "D":"33", "E":"5", "F":"34", "G":"35",
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",
"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",
"/":"79", "O":"1", "0":"790", "P":"70", "1":"791", "Q":"71", "2":"792",
Line 739: Line 739:
"6":"796", "V":"73", "7":"797", "W":"74", "8":"798", "X":"75", "9":"799",
"6":"796", "V":"73", "7":"797", "W":"74", "8":"798", "X":"75", "9":"799",
"Y":"76", "Z":"77"];
"Y":"76", "Z":"77"];
val2key = aa;

key2val = val2key.byValue.zip(val2key.byKey).assocArray;
key2val = aa.byKeyValue.map!(t => tuple(t.value, t.key)).assocArray;
}
}


string encode(in string s) pure @safe {
string encode(in string s) pure /*nothrow*/ @safe {
return s.toUpper.split("").map!(c => val2key.get(c, "")).join;
return s.toUpper.split("").map!(c => val2key.get(c, "")).join;
}
}


string decode(in string s) /*pure*/ @safe {
string decode(in string s) /*pure nothrow*/ @safe {
return s.matchAll("79.|3.|7.|.").map!(g => key2val.get(g[0], "")).join;
return s.matchAll("79.|3.|7.|.").map!(g => key2val.get(g[0], "")).join;
}
}