in reply to permutation algorithm
Here is the loop structure to your problem:
use strict; my @list = (-25, 14, 50, 20, -7, -8, -10); my @result = (); my $sum; for my $elem (@list) { for (@{[@result]}) { push @result, [ @$_, $elem ]; $sum = eval join '+' => @{$result[-1]}; print join(',', @{$result[-1]}), "\n" unless $sum; } push @result, [ $elem ]; print $elem, "\n" unless $elem; }
Result:
-25,50,-7,-8,-10You will need some additional work in order to obtain array indexes instead of values.
How it works:
That's it!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: permutation algorithm
by Abigail-II (Bishop) on Jul 12, 2002 at 13:39 UTC | |
by fglock (Vicar) on Jul 12, 2002 at 14:45 UTC |