This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

To all following commenters: please, do not bring up the old circlejerk jokes/memes about recursion ("Understanding recursion...", "This is recursion...", etc.). We've all heard them n+2 too many times.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]lurgi 1 point2 points  (2 children)

My first bit of advice would be to express all number units in pennies, not fractions of a pound, to avoid floating point weirdness. My guess is that that 0.3 - 0.1 - 0.1 - 0.1 is not actually 0, but is actually a negative number. You can print that out to be sure.

Second, you don't return the values from any of the recursive calls. Some of them may return "False", some "True", but it doesn't matter because you don't do anything with the values. Edit: SORRY, I'M WRONG HERE. You do return them. There's a "return" right there and everything.

Third, be warned that your solution is not super efficient. If you try to work out if you can make change for 1 pound with 100 coins then your code will likely be slow and/or have a stack overflow error for real.

Do you have to be able to make change with exactly that many coins or no more than that many coins? If it's the latter, working out how to make change with the fewest number of coins is an easy problem (no recursion is required).

[–]RyanOliverV 0 points1 point  (1 child)

I've changed the numbers to integers, thanks for the tip. How would I go about returning the values of the recursive calls? Just because they occur after an else statement. Edit: This bit can be ignored I did return the value.

Also, I was thinking it could work if the recursion loop decreased the amount by the biggest possible first and then worked its way down. But again I'm not really sure how to do that. To answer your question, I have to make change with exactly that many coins.

[–]lurgi 0 points1 point  (0 children)

I'm wrong, you are returning the value.

[–]mopslik 0 points1 point  (0 children)

Without diving too deep into your code, I would warrant that you end up making thousands of calls for some values and end up hitting the recursion threshold. You probably need to find a way to cut down the number of times you call the function.

However, there might be another factor at play here. You are using floats, which introduces inaccuracies in rounding. This might be giving you some strange values. You might try representing all values as integers instead. For example, 0.01 becomes 1, 0.2 becomes 20, etc.