Run-length encoding: Difference between revisions

Updated two of the four D versions
(Updated two of the four D versions)
Line 724:
 
=={{header|D}}==
===UtfBasic StringImperative Version===
<lang d>import std.stdio;
<lang d>import std.stringstdio, std.array, std.conv;
void main() {
char[]rle = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
char[]encoded = encode(rle);
char[]decoded = decode(encoded);
writefln("\"%s\" == \"%s\", intermediary %s",rle,decoded,encoded);
assert(rle == decoded);
 
// thisVery issimilar essentiallyto an exact copy ofthe the look and say D function.
char[]string encode(char[]in string input) {
char last = input[$ - 1];
char[]string output;
int count = 0;
 
foreach_reverse (i; input) {
if (i == last) {
count++;
} else {
output = toStringtext(count) ~ last ~ output;
count = 1;
last = i;
}
}
 
output = toString(count)~last~output;
return text(count) ~ last ~ output;
}
 
char[]string decode(char[]in string input) {
char[]istring =i, ""result;
 
char[]ret;
foreach (letter; input) {
if (letter <= 'Z' && letter >= 'A') {
// this is the letter to be repeated
if (i.empty)
if (!i.length) throw new Exception("Can not repeat a letter without a number of repetitions");
ret ~= repeat throw new Exception(["Can not repeat a letter],atoi(i)); " ~
i = null "without a number of repetitions");
result ~= replicate([letter], to!int(i));
i.length = 0;
} else if (letter <= '9' && letter >= '0') {
// this is a digit to mark the number of repetitions
i ~= letter;
} else {
throw new Exception("'" ~ letter ~ "' is not capalphanumericalphanumeric");
}
}
 
return retresult;
 
void main() {
char[]rleconst txt = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
writeln("Input: ", txt);
char[]const encoded = encode(rletxt);
writeln("Encoded: ", encoded);
char[]decodedassert(txt == decode(encoded));
}</lang>
===Short Functional Version===
<lang d>import std.stdio, std.algorithm, std.conv, std.array;
 
alias group encode;
===Utf String Version===
 
string decode(Range)(Range r) {
return arrayjoin(map!((t){ return=> repeatreplicate(""~cast(char)t[0], t[1]); })(r)).join();
 
void main() {
auto s = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
assert(decode(encode(s)) == s);
}</lang>
===UTF String Version===
D's native string is utf-encoded. This version work for utf string. This code use a [[Variable-length_quantity|Variable-length Quantity]] [[Variable-length_quantity#D|module]].
 
Line 866 ⟶ 883:
 
'''NOTE :''' some characters in this section use Chinese font.
===UtfUTF String Version with Regular Expression===
{{trans|Python}} regular expression version
 
Line 888 ⟶ 905:
},
"g") ;
}</lang>
===Short Functional Version===
<lang d>import std.stdio, std.algorithm, std.conv, std.array, std.string;
 
alias group encode;
 
string decode(Range)(Range r) {
return array(map!((t){ return repeat(""~cast(char)t[0], t[1]); })(r)).join();
 
void main() {
auto s = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
assert(decode(encode(s)) == s);
}</lang>
 
Anonymous user