Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. def check_coins(hand)
  2. hand.each_with_index do |coin,index|
  3. if coin != 0
  4. return index
  5. else
  6. nil
  7. end
  8. end
  9. return false
  10. end
  11.  
  12. def wonky_coins(n)
  13. # you have a hand
  14. hand = []
  15. # the machine gives you 3 coins valued at n/2,n/3,n/4
  16. hand << n/2 << n/3 << n/4
  17. # you check the voins in your hand to see if any of the coins are non-zero
  18. while check_coins(hand)
  19. # check the position and value of the coin in your hand that is non-zero
  20. index = check_coins(hand)
  21. value = hand[index]
  22. # remove the coin from the position in your hand
  23. hand.delete_at(index)
  24. # insert that coin into the machine and retrieve 3 coins again into your hand
  25. hand << value/2 << value/3 << value/4
  26. end
  27. # count the number of coins in your hand
  28. hand.length
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement