Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: knapsack problem solved by regex

by blokhead (Monsignor)
on Mar 14, 2010 at 16:02 UTC ( [id://828586]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my $max_weight = 10;
    my %items      = (      # value, weight: positive integers
      'a'  => { v => 1, w => 2 },
      'b'  => { v => 11,  w => 9 },
    );
    
  2. or download this
               first half           second half
               $1  $2
    attempt 1: v   vvvvvvvvvvv  --> ww + wwwwwwwww = 11, not enough w's!
    backtrack
    attempt 2: v   <empty>      --> ww = 2, enough w's for this, so accept
    +ed
    
  3. or download this
    # used to be:
    # my $re = sprintf "%s0\n(?=\n%s)\n", $left, $right;
    ...
    my $re = sprintf qq[%s0\n(?{ print "trying " . Dumper \\%%+})\n(?=\n%s
    +)\n],
        $left, $right;
    print $re;
    
  4. or download this
    STRING:
    vvvvvvvvvvvv0wwwwwwwwww
    ...
              'a' => '',
              'b' => 'vvvvvvvvvvv'
            };
    
  5. or download this
    STRING:
    vvvvvvvvvvvv0wwwwwwwwww
    ...
              'a' => '',
              'b' => 'vvvvvvvvvvv'
            };
    
  6. or download this
    ^
    (?<a>(?:v)?)
    (?<b>(?:vvvvvvvvvvv)?)
    v*
    0
    
  7. or download this
    (?<a>(?:v)?)
    (?<b>(?:vvvvvvvvvvv)?)
    0
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-04-18 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found