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 | |
by Anonymous Monk on Mar 10, 2005 at 13:14 UTC |
In Section
Meditations