in reply to recursively generating permutations
Beauty is in the eye of the beholder, but I prefer this arrangement of the same algorithm:
#! perl -slw use strict; sub permutations { return !@_ ? [] : map{ my( $elem, $n ) = ( $_, 1 ); map{ [ $elem, @$_ ] } permutations( grep{ !/$elem/ or !$n-- } @_ ); } @_; } print @$_ for permutations @ARGV;
Or better, this one:
sub permutations { return !@_ ? [] : map{ my $next = $_; map{ [ $_[ $next ], @$_ ] } permutations( @_[ 0 .. $_-1, $_+1 .. $#_ ] ); } 0 .. $#_; } print @$_ for permutations @ARGV;
|
|---|