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!
Top comments (17)
x86_64 assembly (System V ABI, GNU assembler), for those concerned about speed.
change.S:
change.h:
I am concerned about speed
we need to get that stuff off the streets smh
causin' good folk to think they need to write stuff in assembly, fcol >.>
ruby <3
Rust, iterative style:
Go - with tests:
change.go:
change_test.go
change_test_cases.go
My solution in js
JavaScript with reduce:
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.Just my two cents, in Gwion
prints
It probably could be written better, but looks like it gets the job done.
Python
that guard clause
Some people may say "are the comments really necessary?"
I say to them, "Is capitalism?"