|more useful options|
(Golf) Giving Changeby tadman (Prior)
|on Jun 12, 2001 at 03:39 UTC||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:
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.