Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: (Golf) Giving Change

by Trimbach (Curate)
on Jun 12, 2001 at 06:37 UTC ( #87713=note: print w/replies, xml ) Need Help??

in reply to (Golf) Giving Change

By taking a few liberties with the output format here's my entry at 98:
sub c {($a,$c)=@_;@c=@$c;return map{$a>$_? &{sub{$a-=$_*($b=int($a/$_));"$b x $_"}}:""} reverse @c}
The output format this way looks like this:
,,1 x 20,,,2 x 1,2 x 0.25,,,
...which is both cooler (it tells you how many of each denomination) and uglier (empty commas are bad) at the same time. You could always reformat when the sub is called, but I felt I was taking lots of liberties as it was. :-D

I don't normally bother golfing, but this was educational: I found out you can't coerce a trinary operator to do more than one thing at a time in the "then" part without resorting to an anonymous sub. (At least, I think that's right. That's why it's a trinary operator, I guess. :-D) It was fun, and educational...

Gary Blackburn
Trained Killer

Replies are listed 'Best First'.
Re:{2} (Golf) Giving Change
by jeroenes (Priest) on Jun 12, 2001 at 14:54 UTC
    Nice solution. There is room for improvement, though. No need for return, use that for filtering out empty items:
    sub c {($a,$c)=@_;@c=@$c;grep$_,map{$a>$_? &{sub{$a-=$_*($b=int($a/$_));"$b x $_"}}:""} reverse @c}
    Char-neutral, but leaving the grep out gains 7 chars.

    You don't need the parens after the int, and leave out the spaces around the reverse. Gains you 4 char:

    sub c{($a,$c)=@_;@c=@$c;grep$_,map{$a>$_? &{sub{$a-=$_*($b=int$a/$_);"$b x $_"}}:""}reverse@c}

    "We are not alone"(FZ)

      Ooooooooo... I like the grep idea. Duh. Can you tell I don't golf much? :-D Spiffy!

      Gary Blackburn
      Trained Killer

Re: Re: (Golf) Giving Change
by MeowChow (Vicar) on Jun 12, 2001 at 21:14 UTC
    You don't need to use an anonymous sub; use a do { ... } block instead.
                   s aamecha.s a..a\u$&owag.print

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://87713]
[Discipulus]: be your path light Pirsig

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-04-25 11:02 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (449 votes). Check out past polls.