Run-length encoding: Difference between revisions

Content added Content deleted
(Updated two of the four D versions)
Line 724: Line 724:


=={{header|D}}==
=={{header|D}}==
===Basic Imperative Version===
<lang d>import std.stdio;
import std.string;
<lang d>import std.stdio, 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);
}


// this is essentially an exact copy of the look and say D function
// Very similar to the the look and say D function.
char[]encode(char[]input) {
string encode(in string input) {
char last = input[$-1];
char last = input[$ - 1];
char[]output;
string output;
int count = 0;
int count;

foreach_reverse(i;input) {
foreach_reverse (i; input) {
if (i == last) {
if (i == last) {
count++;
count++;
} else {
} else {
output = toString(count)~last~output;
output = text(count) ~ last ~ output;
count = 1;
count = 1;
last = i;
last = i;
}
}
}
}

output = toString(count)~last~output;
return output;
return text(count) ~ last ~ output;
}
}


char[]decode(char[]input) {
string decode(in string input) {
char[]i = "";
string i, result;

char[]ret;
foreach(letter;input) {
foreach (letter; input) {
if (letter <= 'Z' && letter >= 'A') {
if (letter <= 'Z' && letter >= 'A') {
// this is the letter to be repeated
// 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([letter],atoi(i));
throw new Exception("Can not repeat a letter " ~
i = null;
"without a number of repetitions");
result ~= replicate([letter], to!int(i));
i.length = 0;
} else if (letter <= '9' && letter >= '0') {
} else if (letter <= '9' && letter >= '0') {
// this is a digit to mark the number of repetitions
// this is a digit to mark the number of repetitions
i ~= letter;
i ~= letter;
} else {
} else {
throw new Exception("'"~letter~"' is not capalphanumeric");
throw new Exception("'" ~ letter ~ "' is not alphanumeric");
}
}
}
}

return ret;
return result;
}

void main() {
const txt = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
writeln("Input: ", txt);
const encoded = encode(txt);
writeln("Encoded: ", encoded);
assert(txt == decode(encoded));
}</lang>
}</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 join(map!(t => replicate(""~cast(char)t[0], t[1]))(r));
}

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]].
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: Line 883:


'''NOTE :''' some characters in this section use Chinese font.
'''NOTE :''' some characters in this section use Chinese font.
===Utf String Version with Regular Expression===
===UTF String Version with Regular Expression===
{{trans|Python}} regular expression version
{{trans|Python}} regular expression version


Line 888: Line 905:
},
},
"g") ;
"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>
}</lang>