in reply to Re^3: Cartesian Cross-Products
in thread Cartesian Cross-Products
sub cartesian { my @sets = @_; # base case if (@sets == 0) { return ([]); } my @first = @{@sets[0]}; # recursive call shift @sets; my @rest = cartesian(@sets); my @result = (); foreach my $element (@first) { foreach my $product (@rest) { my @newSet = @{$product}; unshift (@newSet, $element); push (@result, \@newSet); } } return @result; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Cartesian Cross-Products
by psini (Deacon) on Aug 09, 2010 at 17:54 UTC | |
|
Re^5: Cartesian Cross-Products
by drt (Initiate) on Feb 15, 2012 at 21:54 UTC |