Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: The greedy change-making problem using regexes

by Anonymous Monk
on Mar 10, 2005 at 10:45 UTC ( #438204=note: print w/replies, xml ) Need Help??

in reply to Re: The greedy change-making problem using regexes
in thread The greedy change-making problem using regexes

No need for code blocks, and it works very fast as well (as you'd expect from a greedy algorithm).
my $dime = '(?:' . ('1' x 10) . ')'; my $halfdozen = '(?:' . ('1' x 6) . ')'; my $nickel = '(?:' . ('1' x 5) . ')'; my $cent = '(?:' . ('1' x 1) . ')'; sub greedy_change { my $change = shift; my ($c10, $c6, $c5, $c1) = ('1' x $change) =~ /^($dime*)($halfdozen*)($nickel*)($cent*)$/; return (length ($c10 || "") / 10, length ($c6 || "") / 6, length ($c5 || "") / 5, length ($c1 || "") / 1); }

Replies are listed 'Best First'.
Re^3: The greedy change-making problem using regexes
by japhy (Canon) on Mar 10, 2005 at 12:32 UTC
    No, that doesn't work: it ends up using three coins (a dime and two pennies) for 12 cents, instead of two six-cent coins.
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Which is the correct answer for the greedy algorithm. But see my posting elsewhere in this thread for a more general solution.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://438204]
[holli]: forgot that thing exists for a moment.
LanX welcome to the twilight zone ...
[Eily]: LanX well since the onion is your eye you should see it everywhere
[marto]: sounds eye watering
[LanX]: oh your talking about my pic?
[Eily]: marto #SoSad
[holli]: duh
[marto]: I expect you to take a nice photo in Glasgow to replace this one :P
[Eily]: LanX yup, saw it when I checked your current rank
[LanX]: obviously the message is not clear enough if you only notice the onion now. ...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2017-12-14 16:20 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (398 votes). Check out past polls.