Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: generating permutations

by BrowserUk (Patriarch)
on Mar 29, 2017 at 21:21 UTC ( [id://1186437] : note . print w/replies, xml ) Need Help??


in reply to Re^2: generating permutations
in thread generating permutations

Try this. It's a re-casting of the Python algorithm to operate on arrays:

#! perl -slw use strict; sub permute3 { my( $in, $pre ) = ( @_, [] ); return print join '-', @$pre unless @$in; permute3( [ @{ $in }[ 0 .. $_-1, $_+1 .. $#$in ] ], [ @$pre, $in->[ $_ ] ] ) for 0 .. $#$in; } permute3( [ qw[ apple banana orange ] ] );; __END__ C:\test>junk36 apple-banana-orange apple-orange-banana banana-apple-orange banana-orange-apple orange-apple-banana orange-banana-apple

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.