wanna_code_perl has asked for the wisdom of the Perl Monks concerning the following question:
I've been scouring CPAN directly and via Google for a module that will iterate permutations in reverse order, but have come up empty. I would rather not roll my own.
I ask because I'm working on a search algorithm where I need to find the maximum permutation that meets the search criteria, but with ~8.7e10 permutations, searching them all is a huge waste.
I want to be able to do something like the below code. Of course Algorithm::Permute has no prev() method (nor does it actually guarantee any order), and neither do any of the other permutation/combinatorics modules I could find.
Alternatively, is there a clever way to (ab)use ascending permutations to get descending ones that I can't see right now?
use Algorithm::Permute; say join ",",search(reverse (1..14)); sub search { my $p = new Algorithm::Permute(\@_); while (my @a = $p->prev) { return @a if copasetic(@a); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Permutations in reverse order (easy)
by tye (Sage) on Mar 02, 2013 at 02:04 UTC | |
by wanna_code_perl (Friar) on Mar 02, 2013 at 09:19 UTC | |
|
Re: Permutations in reverse order
by Khen1950fx (Canon) on Mar 02, 2013 at 00:52 UTC | |
|
Re: Permutations in reverse order
by BrowserUk (Patriarch) on Mar 02, 2013 at 00:17 UTC | |
|
Re: Permutations in reverse order
by BrowserUk (Patriarch) on Mar 02, 2013 at 13:03 UTC |