Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue