in reply to permutation algorithm

Use backtracking, aka the Perl regex machine. Give your set of number as command line arguments to the following program:
#!/usr/bin/perl use strict; use warnings 'all'; use re 'eval'; use vars '%seen'; my $regex = <<'--'; (?{ local $x = 0 }) (?{ local @x = (-1) x @ARGV }) -- my $i = 0; foreach my $number (@ARGV) { $regex .= "(?:(?{ local \$x = \$x + $number; local \$x [$i] = $i } +)|)\n"; $i ++ } $regex .= <<'--'; (?(?{ $x }) fail | ) (?(?{ grep {$_ >= 0} @x }) | fail) (?{ local $str = join " + " => @ARGV [grep {$_ >= 0} @x] }) (?(?{ $seen {$str} ++ }) fail | ) (?(?{ print "$str = 0\n" }) fail | ) -- "" =~ /$regex/x;

Abigail

Replies are listed 'Best First'.
Re: Re: permuation algorithm
by BrowserUk (Patriarch) on Jul 11, 2002 at 16:39 UTC

    ....eagerly awaiting the tutorial.


    Anyone know of an abbottoire going cheap?