创建字符表

作者:L. Grondin

http://rosalind.info/problems/ctbl/

示例输入

(dog,((elephant,mouse),robot),cat);

示例输出

00110
00111

源代码:ctbl-grondilu.pl

use v6;

sub MAIN(Str $input = "(dog,((elephant,mouse),robot),cat);") {
    my $line = $input;
    $line .= chop;
    my $names = rx / <.ident>+ | <?after <[(,]> > <?before <[),]> > /;
    my @name = $line.comb: $names;
    my @sorting = map *.value, sort *.key, (@name Z=> ^@name);
    $line ~~ s:g/$names/0/;
    $line ~~ s:g/','//;

    while $line ~~ / \( 0 ** 2..* \) / {
        my $array = join(
            '',
            .prematch,
            .subst(/0/, '1', :g),
            .postmatch,
        ).subst(/<[()]>/, '', :g) given $/;
        unless $array ~~ /^[ 0+ | 1+ ]$/ {
            say $array.comb[@sorting].join: '';
        }
        $line ~~ s/\( (0+) \)/$0/;
    }
}