Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def check_coins(hand)
- hand.each_with_index do |coin,index|
- if coin != 0
- return index
- else
- nil
- end
- end
- return false
- end
- def wonky_coins(n)
- # you have a hand
- hand = []
- # the machine gives you 3 coins valued at n/2,n/3,n/4
- hand << n/2 << n/3 << n/4
- # you check the voins in your hand to see if any of the coins are non-zero
- while check_coins(hand)
- # check the position and value of the coin in your hand that is non-zero
- index = check_coins(hand)
- value = hand[index]
- # remove the coin from the position in your hand
- hand.delete_at(index)
- # insert that coin into the machine and retrieve 3 coins again into your hand
- hand << value/2 << value/3 << value/4
- end
- # count the number of coins in your hand
- hand.length
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement