[perl-python] combinatorics fun
axel at white-eagle.invalid.uk
axel at white-eagle.invalid.uk
Thu Feb 10 20:25:45 EST 2005
In comp.lang.perl.misc Xah Lee <xah at xahlee.org> wrote:
> a year ago i wrote this perl program as part of a larger program.
> sub combo ($) {
> my $max=$_[0];
> my %hh=();
> for (my $j=1; $j < $max; ++$j) {
> for (my $i=1; $i <= $max; ++$i) {
> my $m = (($i+$j)-1)%$max+1;
> if ($i < $m){ $hh{"$i,$m"}=[$i,$m];}
> }
> }
> return \%hh;
> }
Well, it's not obfuscated Perl. It is, however, an obfuscated algorithm.
Sane people would use something along the lines of:
sub combo {
my $max = shift;
my %hh=();
for my $i ( 1 .. $max ) {
for my $j ( $i + 1 .. $max ) {
$hh{"$i,$j"} = [$i, $j];
}
}
return \%hh;
}
Axel
More information about the Python-list
mailing list