[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fwd: Please Forward: Ruby Quiz Submission

James Gray

1/28/2008 3:42:00 AM

Begin forwarded message:

> From: Alexander Stedile <as_news@gmx.at>
> Date: January 27, 2008 6:41:47 PM CST
> To: submission@rubyquiz.com
> Subject: Please Forward: Ruby Quiz Submission
>
> Making Change (#154)
>
> Hi, I want to give it a try for the first time. I am curious what
> the other Ruby coders will show.
> I hope my solution is not too straight forward and handles most of
> the ugly cases. ;-)
>
> Cheers,
> Alexander Stedile
> ___
>
> #!/usr/bin/ruby
>
> # Usage: make_change.rb <amount> [<coins_separated_by_commas>]
>
> def make_change(amount, coins = [25, 10, 5, 1])
> coins = coins.sort.reverse
> solution_change = nil
> solution_remainder = nil
>
> until coins.empty?
> remainder = amount
> change = []
>
> coins.each { |c|
> until remainder < c do
> # Take as many big coins as possible.
> remainder -= c
> change << c
> end
> }
>
> if solution_change.nil? || change.size < solution_change.size
> solution_change = change # Found first or better solution.
> solution_remainder = remainder
> end
>
> coins.shift # Try without highest value coin until empty.
> end
> puts "Missing coin: #{solution_remainder}." unless
> solution_remainder == 0
> solution_change
> end
>
> if __FILE__ == $0 then
> if ARGV[1]
> coins = ARGV[1].split(",").collect{ |coin| coin.to_i }
> p make_change(ARGV[0].to_i, coins)
> else
> p make_change(ARGV[0].to_i)
> end
> end