Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Printing combinations, code review request

by demerphq (Chancellor)
on Apr 26, 2003 at 15:16 UTC ( [id://253370]=note: print w/replies, xml ) Need Help??


in reply to Re: Printing combinations, code review request
in thread Printing combinations, code review request

Nice!

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. :-)


---
demerphq

<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.


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

    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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://253370]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-03-27 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (69 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.