Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found