[Rio-pm] Novo Golf

Eden Cardim edencardim em gmail.com
Sexta Junho 20 06:07:14 PDT 2008


2008/6/20 Gabriel Vieira <gabriel.vieira em gmail.com>:
> Mas ainda não funciona.
> Tente qw(a b c), deve retornar:
> a
> a b
> a b c
> a c
> a c b
> b
> b a
> b a c
> b c
> b c a
> c
> c a
> c a b
> c b
> c b a

sub perm {
    my($k, @s) = @_;
    for my $j (1.. em s-1) {
        $k/=$j;
        my $l = ($k % ($j+1));
        @s[$l, $j]=@s[$j, $l];
    }
    return @s;
}
sub fac {
    my $fac = 1;
    $fac *= $_ for 1..shift;
    return $fac;
}
sub comb {
    my($h, @tail) = @_;
    return [$h],[] unless @tail;
    return map { [$h, @$_], $_ } comb(@tail);
}
for my $c (comb(@ARGV)) {
    for my $i (0..fac(scalar @$c)-1) {
        print join(', ', perm($i, @$c)), "\n";
    }
}

-- 
edenc.vox.com


Mais detalhes sobre a lista de discussão Rio-pm