Skip to content

Latest commit

 

History

History
26 lines (16 loc) · 551 Bytes

Run-length_encoding.md

File metadata and controls

26 lines (16 loc) · 551 Bytes

Note that Raku regexes don't care about unquoted whitespace, and that backrefs count from 0, not from 1.

sub encode($str) { $str.subst(/(.) $0*/, { $/.chars ~ $0 }, :g) }

sub decode($str) { $str.subst(/(\d+) (.)/, { $1 x $0 }, :g) }

my $e = encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW');
say $e;
say decode($e);

Output:

12W1B12W3B24W1B14W 
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW