Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: knapsack problem solved by regex

by blokhead (Monsignor)
on Mar 14, 2010 at 16:02 UTC ( #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
Node Status?
node history
Node Type: note [id://828586]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2015-07-31 01:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls