I hacked a benchmark together that compares tye's, demerphq's and my version:
use strict; use warnings; use Math::Combinatorics; use Algorithm::Loops qw( NestedLoops ); use Data::Dumper; use Benchmark qw(:all) ; my %mechs; my @holli; my @tye; my @demerphq; my $iter; while (<DATA>) { chomp; my ($mech, $weight, $faction) = split /\s+/; $mechs{$mech} = { name => $mech, weight => $weight, faction => $fa +ction }; } timethese (10, { 'tye' => sub { $iter = find_lances_tye ( \%mechs, 5, 2 +40, 10 ) }, 'holli' => sub { @holli = find_lances_holli (5, 240, 10, ke +ys %mechs ) }, 'demerphq' => sub { @demerphq = find_lances_demerphq ( \%mechs +, 5, 240, 10 ) }, } ); @holli = map { join (",", sort @{$_->{names}}) . "(" . $_->{weight} . +" tons)\n" } @holli; @holli = sort @holli; while ( my $lance = $iter->() ) { push @tye, join (",", sort @{$lance->{names}}) . "(" . $lance->{we +ight} . " tons)\n" ; } @tye = sort @tye; @demerphq = map { join (",", sort @{$_->{names}}) . "(" . $_->{weight} + . " tons)\n" } @demerphq; @demerphq = sort @demerphq; print "output identical:!\n" if (join ("", @holli) eq join ("", @tye)) + && (join ("", @demerphq) eq join ("", @ty +e)) && (join ("", @holli) eq join ("", @demer +phq)) ; sub find_lances_holli { my $players = shift; my $weight = shift; my $treshold = shift; my @all_lances = Math::Combinatorics::combine ($players, @_); my @valid_lances; for my $lance ( @all_lances ) { my $sum; $sum += $mechs{$_}->{weight} for @{$lance}; push @valid_lances, { names=>$lance, weight=>$sum } if ($weight - $sum <= $treshold) && ($weight - $sum) >= 0 +; } return @valid_lances; } sub find_lances_tye { my( $hvMechs, $members, $maxWeight, $treshold )= @_; my @mechs= keys %$hvMechs; my $iter= NestedLoops ( [ [0..$#mechs], (sub{[$_+1..$#mechs]})x($members-1) ], { OnlyWhen => sub { return if @_ != $members; my $weight= 0; $weight += $_->{weight} for @$hvMechs{@mechs[@_]}; #return $weight == $maxWeight; return 1 if ($maxWeight - $weight <= $treshold) && ($maxWei +ght - $weight) >= 0 ; }, } ); return sub { my @idx= $iter->(); my $w = 0; return unless @idx; $w+=$hvMechs->{$_}->{weight} for @mechs[ @idx ]; return { weight=>$w, names => [@mechs[ @idx ]] } if @idx; }; } # make_choose_iter( $choose, @list ) # ------------------------------------------------------ # Returns an anonymous subroutine to act as an iterator # over the set of combinations, returning each in turn # every time it is called. If the iter is called with an # argument or the list has been exhausted on a previous run # it resets itself and returns undef. # sub make_choose_iter { my ( $choose, @list )= @_; die "Can't do ".(0+@list)." choose ".($choose||0)."\n" if $choose>@list or !@list or !$choose; $choose--; # 1 based number passed in, but need it 0 based # done is used as a flag, and @idx is the first n positions my ( $done, @idx )= ( 0, 0 .. $choose ); return sub { if ( $done || @_ ) { # if we finished the list last call, or they are # forcing a reset via an argument # then we reset everthing and return undef/empty list ($done , @idx)= ( 0, 0 .. $choose ); return; } # we get the apropriate elements of the list for the # current state of @idx. this is what we will return my @ret= @list[@idx]; # assume that this is the last element. $done= 1; # Loop over the elements of @idx from the right # looking for the correct element: # We are simulating the behaviour of N # nested loops. This means we increment the # rightmost element whose increment does not # overflow and whose final value is sufficiently # low that the elements following it can be filled # with its successors without also overflowing for ( my $i= $choose ; $i >= 0 ; $i-- ) { if ( $idx[$i] < $#list && (my $end= $idx[$i] + 1 + $choose - $i) < @list ) { $idx[$i]++; @idx[ $i + 1 .. $choose ]= ( $idx[$i] + 1 .. $end ); $done= 0; last; } } return wantarray ? @ret : \@ret; }; } sub find_lances_demerphq { my ($mechs,$count,$weight,$treshold)=@_; #print Dumper ($mechs);<STDIN>; my $iter= make_choose_iter( $count, sort { $b->{weight} <=> $a->{weight} } values %$mechs ); my @ret; while (my $items=$iter->()) { my $sum=0; #print Dumper ($items);<STDIN>; $sum+=$_->{weight} for @$items; my $combo = { weight => $sum, names => [ map { $_->{name} } @{ +$items} ] }; push @ret, $combo if ($weight - $sum <= $treshold) && ($weight - $sum) >= 0 +; } return @ret; } __DATA__ Flea 20 clan Commando 25 clan UrbanMech 30 clan Hollander 35 clan Jenner 35 clan Raven 35 clan Wolfhound 35 clan BlackHawk 50 clan Hunchback 50 clan Rifleman 60 clan Catapult 65 clan Loki 65 clan Thor 70 clan MadCat 75 clan Gargoyle 80 clan Victor 80 clan Zeus 80 clan Longbow 85 clan Warhawk 85 clan Mauler 90 clan Atlas 100 clan
And this is the result:
Benchmark: timing 10 iterations of demerphq, holli, tye... demerphq: 7 wallclock secs ( 6.28 usr + 0.00 sys = 6.28 CPU) @ 1 +.59/s (n=10) holli: 30 wallclock secs (29.53 usr + 0.03 sys = 29.56 CPU) @ 0 +.34/s (n=10) tye: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) (warning: too few iterations for a reliable count) output identical:!
tye's version is the fastest, demerphq's is a bit slower and I am at the last position with my brute force attempt. All solutions produce the same output, just the sort order is differtent. Now guess what I will use ;-)


holli, /regexed monk/

In reply to Re: find combinations by holli
in thread find combinations by holli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.