Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Printing combinations, code review request

by integral (Hermit)
on Apr 26, 2003 at 13:27 UTC ( #253357=note: print w/replies, xml ) Need Help??

in reply to Printing combinations, code review request

Like demerphq I wanted to reduce the ammount of array copying in your code, but I also didn't like the way the input array had to be copied, so I came up with the following (although you may like to change my coding style in places):

#!/usr/bin/perl use strict; use warnings; my @list = (1 .. 5); my $take = 2; comb_integral(\@list, $take); sub comb_integral { my ($items, $group, $list, $next) = @_; $list ||= []; $next ||= 0; if ($group == 1) { my $prefix = join " ", @$list; print "$prefix $_\n" for @$items[$next..$#$items]; } else { for my $i ($next..$#$items) { push @$list, $$items[$i]; # the next line is an alternate which elimates the use of push/pop #comb_integral($items, $group - 1, [@$list, $$items[$i]], $i + 1); comb_integral($items, $group - 1, $list, $i + 1); pop @$list; } } }

integral, resident of freenode's #perl

Replies are listed 'Best First'.
Re: Re: Printing combinations, code review request
by demerphq (Chancellor) on Apr 26, 2003 at 15:16 UTC


    I tried something like this but couldn't quite get my head around it. I found the iterative approach a lot easier to grok in the end. I added your code to my benchmark., but I couldn't get the non push/pop version going. If you tell me what change I need to do (it should return an AoA instead of printing the list of elements) then I'll add that variant in as well.

    ++ to you for sure. :-)


    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

    • Update:  
    Once I looked at the code again I realized my error and added both versions to the benchmark. The push/pop variant is faster. Nice stuff.

      That comment points out a version of the code which instead of using the same array to hold all the elements which we're currently 'on', creates a separate array each time. To use it change the body of the for loop from:

      push @$list, $$items[$i]; comb_integral($items, $group - 1, $list, $i + 1); pop @$list;

      to the single line:

      comb_integral($items, $group - 1, [@$list, $$items[$i]], $i + 1);

      And here's the new-improved version which returns the combinations instead of printing them, although it does create and destroy a few anonymous arrays in the process:

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @list = (1 .. 5); my $take = 2; my @combs = comb_integral(\@list, $take); for my $comb (@combs) { print "@$comb\n"; } sub comb_integral { my ($items, $group, $next) = @_; $next ||= 0; if ($group == 1) { return map [$_], @$items[$next..$#$items]; } else { my @returns; for my $i ($next..$#$items) { push @returns, map [$$items[$i], @$_], comb_integral($items, $gr +oup - 1, $i + 1); } return @returns; } }

      In fact I've just thought of another variant of that, replace the push line in the for loop with:

      push @returns, my @combs = comb_integral($items, $group - 1, $i + 1); unshift @$_, $$items[$i] for @combs;

      Although some might prefer not to code quite so tersely ;-).

      integral, resident of freenode's #perl

      Update: I've just benchmarked this (as integral2) and don't bother with the variant suggested at the end as it's the slowest of my four versions. This does beat by two earlier ones however. Here's my benchmark results:

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://253357]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-07-28 11:16 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (428 votes). Check out past polls.