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}}== |
||
⚫ | |||
⚫ | |||
import std. |
<lang d>import std.stdio, std.array, std.conv; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
writefln("\"%s\" == \"%s\", intermediary %s",rle,decoded,encoded); |
|||
assert(rle == decoded); |
|||
⚫ | |||
// |
// Very similar to the the look and say D function. |
||
string encode(in string input) { |
|||
char last = input[$-1]; |
char last = input[$ - 1]; |
||
string output; |
|||
int count |
int count; |
||
foreach_reverse(i;input) { |
foreach_reverse (i; input) { |
||
if (i == last) { |
if (i == last) { |
||
count++; |
count++; |
||
} else { |
} else { |
||
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; |
||
} |
} |
||
string decode(in string input) { |
|||
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"); |
|||
throw new Exception("Can not repeat a letter " ~ |
|||
"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 |
throw new Exception("'" ~ letter ~ "' is not alphanumeric"); |
||
} |
} |
||
} |
} |
||
return |
return result; |
||
⚫ | |||
⚫ | |||
⚫ | |||
writeln("Input: ", txt); |
|||
⚫ | |||
writeln("Encoded: ", encoded); |
|||
⚫ | |||
}</lang> |
}</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=== |
||
{{trans|Python}} regular expression version |
{{trans|Python}} regular expression version |
||
Line 888: | Line 905: | ||
}, |
}, |
||
"g") ; |
"g") ; |
||
⚫ | |||
⚫ | |||
<lang d>import std.stdio, std.algorithm, std.conv, std.array, std.string; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
}</lang> |
}</lang> |
||