Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 7d9c16311e
Fetching contributors…

Cannot retrieve contributors at this time

27 lines (21 sloc) 0.826 kB
use v6;
# Specification:
# P13 (**) Run-length encoding of a list (direct solution).
# Implement the so-called run-length encoding data compression method
# directly. I.e. don't explicitly create the sublists containing the
# duplicates, as in problem P09, but only count them. As in problem P11,
# simplify the result list by replacing the singletons [1,X] by X.
# Example:
# > encode_direct(<a a a a b c c a a d e e e e>).perl.say
# ([4, "a"], "b", [2, "c"], [2, "a"], "d", [4, "e"])
multi infix:<compress> ( $a, $b ) { $a ~~ $b ?? [[2, $a]] !! [ [1, $a], [1, $b] ] }
multi infix:<compress> ( @a, $b ) {
if @a[*-1][1] ~~ $b {
@a[*-1][0]++;
return @a;
} else {
return [ @a, [1, $b] ];
}
}
say ([compress] <a a a a b c c a a d e e e e>).perl;
# vim:ft=perl6
Jump to Line
Something went wrong with that request. Please try again.