This solution acts pretty much the same way humans act: It starts with the big number (the quarters), and once it determines that adding another quarter will put it past the target value, it moves on to the next smaller denomination and tries it. ...at least that's how I do it. ;)

use strict; use warnings; my $a_coins = [ 25, 25, 25, 10, 10, 10 , 5, 1, 1, 1, 1 ]; my $wanted = 72; my @needed = @{ make_change( $a_coins, $wanted ) }; print "@needed = $wanted\n"; sub make_change { my( $a_coins, $dest ) = @_; my @coins = sort { $b <=> $a } @{ $a_coins }; my @need; my $tally = 0; while ( @coins ) { my $current = shift @coins; next if $tally + $current > $dest; $tally += $current; push @need, $current; return \@need if $tally == $dest; } shift ( @{$a_coins} ); @need = @{ make_change( $a_coins, $dest ) }; my $check=0; foreach ( @need ) { $check += $_; } if( $check == $dest ) { return \@need; } else { die "Can't make change.\n"; } }

Updated: I've updated make_change() to use recursion to support cases such as @coins = ( 25, 10, 10, 10 ) and $wanted = 30. Now, if it is found that 25+10 exceeds 30 (which it does), 25 is discarded from the change queue, and the sub is re-run without the quarter getting in the way.


Dave


In reply to Re: making change by davido
in thread pathsearch/backtracking in Perl - how to solve a problem apparently requiring it? (comparison of Perl with Standard ML) by metaperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.