P11 - 改进的游程编码。

作者:David Romano

规范

P11 (*) Modified run-length encoding.
    Modify the result of problem P10 in such a way that if an element has
    no duplicates it is simply copied into the result list. Only elements
    with duplicates are transferred as (N E) lists.

示例

> encode_modified(<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"])

源代码: P11-unobe.pl

use v6;

my @l = <a a a a b c c a a d e e e e>;
sub prob11 (@in) {
    return gather loop {
        last if [email protected];
        my $val = @in[0];
        my @a = gather loop {
            last if [email protected];
            @in[0] ~~ $val ?? take(shift @in) !! last
        };
        take @a.end ?? [@a.elems, $val] !! $val;
    };
}
say ~@l;
say prob11(@l).list.perl;