Return amount of denominations
I'm having trouble constructing the logic behind returning bills. Am I on the right path? What am I missing?
16 Replies
Isnt the expected output something like... "you get 1x 500, 2x 20, 1x 2 in return"
retur += denominations[i];
seems very curious to me
retur, before being modified, holds how much change you need to give back, as an intYou're right, that's what the output is supposed to look like.
I admit I am very confused. Should I create another array to hold the bills? Am I silly for thinking it can be done with 1 single 'retur' variable?
I find myself removing and starting over and writing the same thing as before.
it can't be done with a single int, no
imagine it like so: the expected return for 752 should total 752
but its not
752
itself
its 1x 500, 1x 200, 1x 50, 1x 2
My first instinct is to have your calculate method return a Dictionary<int,int>
where the key is your denomination, and your value the countI will look into it. My beginner instinct is to create variables for each of the denominations but that feels so.. unsatisfactory.
yeah don't do that
the alternative to a dict would be an
int[]
, where each position of the array matches the position of the denominationThat I can wrap my head around.
I fear it's too simple.
do you know about unit tests?
if you wrote this as a pure function instead of as a void, you could slap together a test suite that would let you verify that things work as expected
I do not know of unit tests.
Would you like to learn?
Sure
I can jump on a voice call for a while if you have the time
Yeah, join a channel and I'll pop in
#dev-vc-0
Thanks for your help. I did it like this. Now I just need to learn how to deal with Dictionaries to create pretty print statements for the bills. (=
when looping over a dictionary with foreach, you get access to both key and value
š
Wonderful. You're an asset to this community.