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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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/

In reply to Re^2: 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found