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

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

by blokhead (Monsignor)
on Mar 10, 2005 at 02:00 UTC ( #438134=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

Ooh, that is quite elegant indeed. One of my lessons here was "Know in which order thine regex engine tries things" but I completely missed this little optimization. ;) It really matches up temporaly with the spirit of the problem.

I suppose I could also use (?>pattern) to prevent backtracking and perform the greedy match without the negative lookaheads?

(1 x $money) =~ /^(?>(1{10})*)(?>(1{6})*)(?>(1{5})*)(?>(1{1})*)$/
Although I can't say I've ever really used (?>pattern)...

Anyway, the pain in the butt is counting the number of coins. I like my little (?{$^R+1}) idiom!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://438134]
[ambrus]: "could evacuate to open schoolyard and then call or go back to school" => wait what? like power cycling the school building by removing all the kids and then reentering them? how would that fix the problems?
[ambrus]: https://www.xkcd. com/723/ is relevant about earthquakes and phones
[ambrus]: must be this earthquake
[ambrus]: (It's convenient that I live in a place with no earthquakes or hurricanes. One less bad thing to look out for.)

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2017-01-18 12:40 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (161 votes). Check out past polls.