Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: (Golf) Giving Change

by bikeNomad (Priest)
on Jun 12, 2001 at 04:27 UTC ( #87691=note: print w/ replies, xml ) Need Help??


in reply to (Golf) Giving Change

Assuming that your lowest denomination >= 0.000001, you can get around the scaling in your code using sprintf (94 chars here):

sub c{($t,$p,@r)=@_;while($v=pop@$p and$t>0) {while($t>=$v){push@r,$v;$t=sprintf"%f",$t-$v}}@r}

update: added reinit of @r, comment about limits on smallest denomination (change %f to "%9f" for 1e-9 limit), change %g to %f
Works for larger numbers:
print join(',',c(22500020,[.01,.05,.1,1,2,5,10,20,50,100,1e3,1e4,1e5,1e6])); 1000000,1000000,1000000,1000000,1000000,1000000,1000000, 1000000,1000000,1000000,1000000,1000000,1000000,1000000, 1000000,1000000,1000000,1000000,1000000,1000000,1000000, 1000000,100000,100000,100000,100000,100000,20


Comment on Re: (Golf) Giving Change
Select or Download Code
Re: Re: (Golf) Giving Change
by no_slogan (Deacon) on Jun 12, 2001 at 04:34 UTC
    You'll lose precision if $t has more than 6 digits.
Re^2: (Golf) Giving Change
by tadman (Prior) on Jun 12, 2001 at 05:04 UTC
    In the process of your function, you consume the array given to you. If this were a fixed array provided by reference, you would trash it, rendering it useless for subsequent operations. Penalty is 7 characters:      @p=@$p;
    Further, I don't know of any currencies which use millionths of a unit, so why 1e-9 is even relevant is beyond me. Yikes!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://87691]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-08-22 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (146 votes), past polls