Created
August 25, 2021 21:02
-
-
Save venelrene/77c8634482cb185dd0f53fbf24e563c0 to your computer and use it in GitHub Desktop.
Create a function that will take any amount of money and break it down to the smallest number of bills as possible. Only integer amounts will be input, NO floats. This function should output a sequence, where each element in the array represents the amount of a certain bill type. The array will be set up in this manner:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# array[0] ---> represents $1 bills | |
# array[1] ---> represents $5 bills | |
# array[2] ---> represents $10 bills | |
# array[3] ---> represents $20 bills | |
# array[4] ---> represents $50 bills | |
# array[5] ---> represents $100 bills | |
# In the case below, we broke up $365 into 1 $5 bill, 1 $10 bill, 1 $50 bill, and 3 $100 bills. | |
def give_change(amount) | |
bills = [100, 50, 20, 10, 5, 1] | |
bills.each_with_object([]) do |bill, obj| | |
obj.unshift(amount / bill) | |
amount %= bill | |
end | |
end | |
### Test | |
input_output = { | |
365 => [0, 1, 1, 0, 1, 3], | |
217 => [2, 1, 1, 0, 0, 2] | |
}.freeze | |
input_output.each do |input, output| | |
result = give_change(input) | |
puts "[#{result == output ? 'PASS' : 'FAIL'}] #{input} => '#{result}' (expected: #{output})" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment