Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Run-length-encoding/D/run-length-encoding-1.d
Normal file
13
Task/Run-length-encoding/D/run-length-encoding-1.d
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import std.algorithm, std.array;
|
||||
|
||||
alias encode = group;
|
||||
|
||||
auto decode(Group!("a == b", string) enc) {
|
||||
return enc.map!(t => [t[0]].replicate(t[1])).join;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable s = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWW" ~
|
||||
"WWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
|
||||
assert(s.encode.decode.equal(s));
|
||||
}
|
||||
53
Task/Run-length-encoding/D/run-length-encoding-2.d
Normal file
53
Task/Run-length-encoding/D/run-length-encoding-2.d
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import std.stdio, std.array, std.conv;
|
||||
|
||||
// Similar to the 'look and say' function.
|
||||
string encode(in string input) pure nothrow @safe {
|
||||
if (input.empty)
|
||||
return input;
|
||||
char last = input[$ - 1];
|
||||
string output;
|
||||
int count;
|
||||
|
||||
foreach_reverse (immutable c; input) {
|
||||
if (c == last) {
|
||||
count++;
|
||||
} else {
|
||||
output = count.text ~ last ~ output;
|
||||
count = 1;
|
||||
last = c;
|
||||
}
|
||||
}
|
||||
|
||||
return count.text ~ last ~ output;
|
||||
}
|
||||
|
||||
string decode(in string input) pure /*@safe*/ {
|
||||
string i, result;
|
||||
|
||||
foreach (immutable c; input)
|
||||
switch (c) {
|
||||
case '0': .. case '9':
|
||||
i ~= c;
|
||||
break;
|
||||
case 'A': .. case 'Z':
|
||||
if (i.empty)
|
||||
throw new Exception("Can not repeat a letter " ~
|
||||
"without a number of repetitions");
|
||||
result ~= [c].replicate(i.to!int);
|
||||
i.length = 0;
|
||||
break;
|
||||
default:
|
||||
throw new Exception("'" ~ c ~ "' is not alphanumeric");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable txt = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWW" ~
|
||||
"WWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
|
||||
writeln("Input: ", txt);
|
||||
immutable encoded = txt.encode;
|
||||
writeln("Encoded: ", encoded);
|
||||
assert(txt == encoded.decode);
|
||||
}
|
||||
79
Task/Run-length-encoding/D/run-length-encoding-3.d
Normal file
79
Task/Run-length-encoding/D/run-length-encoding-3.d
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import std.stdio, std.conv, std.utf, std.array;
|
||||
import vlq;
|
||||
|
||||
struct RLE { // for utf string
|
||||
ubyte[] encoded;
|
||||
|
||||
RLE encode(const string s) {
|
||||
validate(s); // check if s is well-formed utf, throw if not
|
||||
encoded.length = 0; // reset
|
||||
if (s.length == 0) return this; // empty string
|
||||
string last;
|
||||
VLQ count;
|
||||
for (int i = 0; i < s.length; ) {
|
||||
auto k = s.stride(i);
|
||||
auto ucode = cast(string)s[i .. i + k];
|
||||
if (i == 0) last = ucode;
|
||||
if (ucode == last)
|
||||
count++;
|
||||
else {
|
||||
encoded ~= count.toVLQ ~ cast(ubyte[])last;
|
||||
last = ucode;
|
||||
count = 1;
|
||||
}
|
||||
i += k;
|
||||
}
|
||||
encoded ~= VLQ(count).toVLQ ~ cast(ubyte[])last;
|
||||
return this;
|
||||
}
|
||||
|
||||
int opApply(int delegate(ref ulong c, ref string u) dg) {
|
||||
VLQ count;
|
||||
string ucode;
|
||||
|
||||
for (int i = 0; i < encoded.length; ) {
|
||||
auto k = count.extract(encoded[i .. $]);
|
||||
i += k;
|
||||
if (i >= encoded.length)
|
||||
throw new Exception("not valid encoded string");
|
||||
k = stride(cast(string) encoded[i .. $], 0);
|
||||
if (k == 0xff) // not valid utf code point
|
||||
throw new Exception("not valid encoded string");
|
||||
ucode = cast(string)encoded[i .. i + k].dup;
|
||||
dg(count.value, ucode);
|
||||
i += k;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
string toString() {
|
||||
string res;
|
||||
foreach (ref i, s ; this)
|
||||
if (indexOf("0123456789#", s) == -1)
|
||||
res ~= text(i) ~ s;
|
||||
else
|
||||
res ~= text(i) ~ '#' ~ s;
|
||||
return res;
|
||||
}
|
||||
|
||||
string decode() {
|
||||
string res;
|
||||
foreach (ref i, s; this)
|
||||
res ~= replicate(s, cast(uint)i);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
RLE r;
|
||||
auto s = "尋尋覓覓冷冷清清淒淒慘慘戚戚\nWWWWWWWWWWWWBWWWWWWWWWWW" ~
|
||||
"WBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW\n" ~
|
||||
"11#222##333";
|
||||
auto f = File("display.txt", "w");
|
||||
f.writeln(s);
|
||||
r.encode(s);
|
||||
f.writefln("-----\n%s\n-----\n%s", r, r.decode());
|
||||
auto sEncoded = RLE.init.encode(s).encoded ;
|
||||
assert(s == RLE(sEncoded).decode(), "Not work");
|
||||
}
|
||||
32
Task/Run-length-encoding/D/run-length-encoding-4.d
Normal file
32
Task/Run-length-encoding/D/run-length-encoding-4.d
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import std.stdio, std.conv, std.array, std.regex, std.utf,
|
||||
std.algorithm;
|
||||
|
||||
string reEncode(string s) {
|
||||
validate(s); // Throw if it's not a well-formed UTF string
|
||||
static string rep(Captures!string m) {
|
||||
auto c = canFind("0123456789#", m[1]) ? "#" ~ m[1] : m[1];
|
||||
return text(m.hit.length / m[1].length) ~ c;
|
||||
}
|
||||
return std.regex.replace!rep(s, regex(`(.|[\n\r\f])\1*`, "g"));
|
||||
}
|
||||
|
||||
|
||||
string reDecode(string s) {
|
||||
validate(s); // Throw if it's not a well-formed UTF string
|
||||
static string rep(Captures!string m) {
|
||||
string c = m[2];
|
||||
if (c.length > 1 && c[0] == '#')
|
||||
c = c[1 .. $];
|
||||
return replicate(c, to!int(m[1]));
|
||||
}
|
||||
auto r=regex(`(\d+)(#[0123456789#]|[\n\r\f]|[^0123456789#\n\r\f]+)`
|
||||
, "g");
|
||||
return std.regex.replace!rep(s, r);
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto s = "尋尋覓覓冷冷清清淒淒慘慘戚戚\nWWWWWWWWWWWWBWWWWWWWWWWW" ~
|
||||
"WBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW\n" ~
|
||||
"11#222##333";
|
||||
assert(s == reDecode(reEncode(s)));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue