X
Xah Lee
a year ago i wrote this perl program as part of a larger program.
as a exercise of fun, let's do a python version. I'll post my version
later today.
=pod
combo(n) returns a collection with elements of pairs that is all
possible combinations of 2 things from n. For example, combo(4)
returns {'3,4' => ['3',4],'1,2' => [1,2],'1,3' => [1,3],'1,4' =>
[1,4],'2,3' => ['2',3],'2,4' => ['2',4]}; Hash form is returned
instead of array for this program.
=cut
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;
}
use Data:umper;
$Data:umper::Indent=0;
print Dumper combo(5);
This is Perl-Python a-day. To subscribe, see
http://xahlee.org/perl-python/python.html
Xah
(e-mail address removed)
http://xahlee.org/PageTwo_dir/more.html
as a exercise of fun, let's do a python version. I'll post my version
later today.
=pod
combo(n) returns a collection with elements of pairs that is all
possible combinations of 2 things from n. For example, combo(4)
returns {'3,4' => ['3',4],'1,2' => [1,2],'1,3' => [1,3],'1,4' =>
[1,4],'2,3' => ['2',3],'2,4' => ['2',4]}; Hash form is returned
instead of array for this program.
=cut
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;
}
use Data:umper;
$Data:umper::Indent=0;
print Dumper combo(5);
This is Perl-Python a-day. To subscribe, see
http://xahlee.org/perl-python/python.html
Xah
(e-mail address removed)
http://xahlee.org/PageTwo_dir/more.html