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:
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.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 }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: (Golf) Giving Change
by Trimbach (Curate) on Jun 12, 2001 at 06:37 UTC | |
by jeroenes (Priest) on Jun 12, 2001 at 14:54 UTC | |
by Trimbach (Curate) on Jun 12, 2001 at 15:24 UTC | |
by MeowChow (Vicar) on Jun 12, 2001 at 21:14 UTC | |
Re: (Golf) Giving Change
by iakobski (Pilgrim) on Jun 12, 2001 at 15:14 UTC | |
by MeowChow (Vicar) on Jun 12, 2001 at 20:22 UTC | |
Re: (Golf) Giving Change
by bikeNomad (Priest) on Jun 12, 2001 at 04:27 UTC | |
by tadman (Prior) on Jun 12, 2001 at 05:04 UTC | |
by no_slogan (Deacon) on Jun 12, 2001 at 04:34 UTC | |
Re: (Golf) Giving Change
by no_slogan (Deacon) on Jun 12, 2001 at 04:04 UTC | |
by ZZamboni (Curate) on Jun 12, 2001 at 04:23 UTC | |
by no_slogan (Deacon) on Jun 12, 2001 at 04:28 UTC | |
by tadman (Prior) on Jun 12, 2001 at 05:00 UTC | |
by no_slogan (Deacon) on Jun 12, 2001 at 05:21 UTC | |
by tadman (Prior) on Jun 12, 2001 at 05:26 UTC |
Back to
Meditations