Pablo Zea Aranibar wrote:
Hi, realy i need your help with this algorithm.
what I need is another algorithm in order to improve my own.
example:
my $firsSet = [ 'a', 'b', 'c', 'd', 'e' ];
my $secondSet = [ 'x', 'y', 'z' ];
my $thirdSet = [ 'a', 'x', 'b', 'y' ];
my $fourthSet = [ 'b', 'd', 'z' ];
my $counter = 0;
foreach my $elm1 (@{$firstSet})
{
foreach my $elm2 (@{$secondSet})
{
foreach my $elm3 (@{$thirdSet})
{
foreach my $elm4 (@{$fourthSet})
{
print "$counter : $elm1, elm2 , elm3 , elm4\n";
$counter ++;
}
}
}
}
# It prints each possible combination taken one element for each set.
another (better way):
my @Set = ();
my $elements = {a=>1, b=>1, c=>1, d=>1, e=>1, x=>1, y=>1, z=>1};
$Set[0] = [ 'a', 'b', 'c', 'd', 'e' ];
$Set[1] = [ 'x', 'y', 'z' ];
$Set[2] = [ 'a', 'x', 'b', 'y' ];
$Set[3] = [ 'b', 'd', 'z' ];
my $counter = 0;
@result = @{$Set[0]};
for (1 .. (scalar @Set) - 1) {
my $temp = [];
foreach my $result (@result) {
foreach my $elm (@{$Set[$_]}) {
if ($elements=>{$elm}) { #if I dont want repetitions
push @{$temp}, $result.', '.$elm;
$elements=>{$elm}=0;
$counter++;
};
};
};
@result = @{$temp};
};
print "@result\n";
But, both are still too slow.
So, can you give me an idea to improve it?.
someone tell me about mask, and bit operations. but i really dont know how
to do it. so.. any suggestion?.
there was a thread in a forum
(here<http://forums.devshed.com/software-design-43/algorithm-design-536480.html>),
but the solution was for python (recursive using "yield").
Perhaps this may be what you want:
my $firstSet = [ qw/a b c d e/ ];
my $secondSet = [ qw/x y z/ ];
my $thirdSet = [ qw/a x b y/ ];
my $fourthSet = [ qw/b d z/ ];
for my $counter ( 0 .. @$firstSet * @$secondSet * @$thirdSet *
@$fourthSet - 1 ) {
print "$counter : $firstSet->[ ($counter / (@$fourthSet *
@$thirdSet * @$secondSet)) % @$firstSet ], $secondSet->[ ($counter /
(@$fourthSet * @$thirdSet)) % @$secondSet ], $thirdSet->[ ($counter /
@$fourthSet) % @$thirdSet ], $fourthSet->[ $counter % @$fourthSet ]"
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/