Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: (Golf) Giving Change

by Trimbach (Curate)
on Jun 12, 2001 at 06:37 UTC ( [id://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}

    Jeroen
    "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.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-18 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found