-
-
Notifications
You must be signed in to change notification settings - Fork 547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change: Add test for incorrect greedy algorithm #882
Conversation
This test invalidates an incorrect greedy algorithm which takes as many of the larger coins as possible as long as remainder is greater than or equal to the smallest coin. Currently such an algorithm will pass all of the tests.
|
Take a look at this greedy algorithm here. |
Interesting! I ran this test case against the Java and Kotlin track example solutions; Java passed while Kotlin failed! Given that, this is definitely a worthy addition. If possible I'd like for us to better understand exactly how this case differs from |
The Kotlin example solution has an error on line 48. For some reason it's converting a bad result (ie A Although this test catches a problem with the Kotlin algorithm, it's not really a good example of what the test was intended to catch. It's testing greedy algorithms that will assume they can continue to use larger coins as long as the remainder is still greater than or equal to the value of the smallest coin. I think the solution I linked to earlier is a better example of this. Perhaps we could simply call the test |
So is the distinguishing factor this:
Unlike "possible change without unit coins available", at the point at
which you must stop using some coin C, the remainder still exceeds the coin
C + the smallest coin.
|
@petertseng, yes, that's good way to express the difference. |
I'll need to return to this with a clearer head in the morning to make sure I understand the distinction; thanks for the input, all! |
Gotcha, this makes sense! So that's why |
when you stop using the coin 15, the remainder (8) does not exceed 16 (= 15 + 1)?
Correct.
Something that makes me curious... what about Lower Elbonia coins? At the
point at which you must stop using 25, the remainder (63) is certainly
larger than 25 + 1. I was wondering whether the greedy algorithm would thus
also fail on that case, but maybe some implementations have a provision for
when the remainder is exactly a multiple of some coin.
So I do like the addition of the proposed case. Sorry I can't Approve in
the GitHub sense of the word via email (I have attempted), but I approve
the colloquial sense.
|
Using Lower Elbonia coins In any case, with a couple approvals I'm going to go ahead and merge this. Thanks for the add! |
Were you wanting (and do you still want) a description that describes the difference, rather than simply "another"? |
I think that's a nice to have, given that the other test cases for this problem are also description-less. But I wouldn't turn down a PR that added them throughout, that's for sure! |
I missed a distinguishing factor of the incorrect algorithm: It knew to check the smallest 1 coin, the smallest 2 coins, the smallest 3 coins ... all the coins. So a test case needs to require the use of the largest coin, which is true of the |
I'd like to propose adding a missing edgecase test. This test invalidates an incorrect greedy algorithm which takes as many of the larger coins as possible as long as the remainder is greater than or equal to the value or the smallest coin. Currently such an algorithm will pass all of the tests.
If required, I can point to some solutions which fail this test.