Today's challenge is to implement a change function that will accept an integer parameter that represents cents. The function should return the optimal change using the least number of coins.
The function should also return a key for each coin of US currency (specifically 25¢, 10¢, 5¢, and 1¢ coins). The value of each coin should represent the count of each coin in the change. The value for each coin that is not included should return 0.
Ex.
change(31)
{ 25 => 1, 10 => 0, 5 => 1, 1 => 1 }
Good luck!
This challenge comes from user Lordnibbler on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (17)
Python
My solution in js
[gist.github.com/devparkk/e5a9ebbe8...]
Greedy solution in JS
Another javascript solution:
Just my two cents, in Gwion
prints
It probably could be written better, but looks like it gets the job done.
Rust, iterative style:
This is a nice solution in Java. But don't forget to remove the
number = 74;, otherwise the function will always write the same results.that guard clause
Some people may say "are the comments really necessary?"
I say to them, "Is capitalism?"
ruby <3