Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Odud's recent posting on Junior Perl came with a little puzzle to do with giving change, which is simple enough, and is certainly golf material. Suppose you are trying to write a function c that, give the amount you want, and an array reference to the units of currency that you have available in order from lowest to highest, will return a list of the required change.

For instance, if $22.50 was specified, along with a US currency specification, assuming no $2 bills are available in the area:     print join(',',c(22.50,[.01,.05,.1,.25,1,5,10,20,50,100]));
Would output:     20,1,1,0.25,0.25
Of course, other countries use different units, such as:     print join(',',c(22.50,[.01,.05,.1,.20,1,2,5,10,20,50,100]));
Which would show:     20,2,0.2,0.2,0.1
Amounts less than the smallest unit of currency are not handled, so they can be ignored.

Here's my baseline, which is 115 characters, not including linebreaks required for presentation:
sub c{ ($t,$p,@r)=@_;@p=map{int($_*100)}@$p; $t=int($t*100);while($v=pop@p and$t>0 ){while($t>=$v){push@r,$v/100;$t-=$v; }}@r }
You will note the use of integer math only. I am perplexed by the floating point math. As Obdud says, this exercise should teach "simple arithmetic, looping, lists, sorting, etc." where 'etc.' apparently refers to floating point idiosyncrasies, such as the following:      print "$t != $v\n" if ($t != $v);
Which surprisingly shows:      0.1 != 0.1
Where the values were actually: $t = 0.099999999999999977795540, $v = 0.100000000000000005551115, due to some minor floating point issues in the 17th decimal place.

In reply to (Golf) Giving Change by tadman

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (6)
    As of 2018-06-20 10:23 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (116 votes). Check out past polls.