Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: find combinations

by holli (Abbot)
on Sep 06, 2005 at 11:02 UTC ( [id://489433]=note: print w/replies, xml ) Need Help??


in reply to Re: find combinations
in thread find combinations

I have discovered that module while you were writing the node and came up with the following code (which is similar to yours).

But thanks for your effort.
use strict; use warnings; use Math::Combinatorics; use Algorithm::Permute; use Data::Dumper; my %mechs; while (<DATA>) { chomp; my ($mech, $weight, $faction) = split /\s+/; $mechs{$mech} = { weight => $weight, faction => $faction }; } # find all lances with 3 members and 150t weight my @possible_lances = find_lances ( 3, 150, 10, grep { $mechs{$_}->{faction} eq "clan" } keys %mechs ); for my $lance ( @possible_lances ) { print join (",", @{$lance->{names}}), "(", $lance->{weight}, " ton +s)\n"; } sub find_lances { 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; } __DATA__ Flea 20 clan Commando 25 clan UrbanMech 30 clan Hollander 35 clan Jenner 35 clan Raven 35 clan Wolfhound 35 clan Black 50 Hawk 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


holli, /regexed monk/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found