Rubyquiz: Making Change (#154)

Discussion in 'Ruby' started by Ian Evans, Jan 30, 2008.

  1. Ian Evans

    Ian Evans Guest

    This is my first rubyquiz solution. I think it does as intended although it=
    doesn't have much error checking for the arguments. Enjoy!


    def make_change(amount, coins =3D [25, 10, 5, 1])
    return "no solution" if amount=3D=3D0
    init_amount=3Damount
    possible_change =3D []
    change =3D []
    coins.sort!
    =09
    #iterates through each coin to make the change, making all possible sets o=
    f change
    (coins.length-1).downto(0) do |start_coin|
    start_coin.downto(0) do |x|
    while amount>=3D coins[x]
    if (amount - coins[x])>=3D 0
    amount -=3D coins[x]
    change<< coins[x]
    else
    next
    end
    end
    =09
    if amount=3D=3D0 then
    possible_change< 0
    change =3D possible_change.sort[0][1]
    else
    change =3D ["no solution"]
    end

    #print some pretty text
    print "#{init_amount}: #{change.join(', ')}\n"
    end

    Ian
    _________________________________________________________________
    Shed those extra pounds with MSN and The Biggest Loser!
    http://biggestloser.msn.com/=
    Ian Evans, Jan 30, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Fulton

    Facebook... only 154 to go?

    Hal Fulton, Oct 11, 2005, in forum: Ruby
    Replies:
    15
    Views:
    169
    Matt Lawrence
    Oct 12, 2005
  2. Hal Fulton

    Possible RubyQuiz idea

    Hal Fulton, Apr 4, 2006, in forum: Ruby
    Replies:
    11
    Views:
    119
    Logan Capaldo
    Apr 5, 2006
  3. femto gary
    Replies:
    1
    Views:
    126
    femto gary
    Sep 8, 2006
  4. Ruby Quiz

    [QUIZ] Making Change (#154)

    Ruby Quiz, Jan 25, 2008, in forum: Ruby
    Replies:
    102
    Views:
    833
    Amey Dhoke
    Jan 31, 2008
  5. Ruby Quiz

    [SUMMARY] Making Change (#154)

    Ruby Quiz, Jan 31, 2008, in forum: Ruby
    Replies:
    0
    Views:
    115
    Ruby Quiz
    Jan 31, 2008
Loading...

Share This Page