6 kyu
Closest Sum
322 of 567smepple
Loading description...
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Description should be language-agnostic
JS Node 18. should be enabled
5 years old issue is still not resolved. Bad kata
python new test framework is required. updated in this fork
Approved
Extra tests fail, JavaScript, despite using spread operator on
ints
argument. Don't see further instructions for how to handle when it's not defined.This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Not really an issue, no performance tag, no explicit mention of large arrays. A performant version may be created but I doubt if it is any different than myjinxin's three sum and four sum kata (potentially others ... ->
Javascript, both -3 and -5 are 1 unit away of -4
That isn't true.
Same for Python and presumably other languages, another occurrence:
Is it possible to solve this problem without libraries or brute force?
Here's an iterative stack-based solution
Haskell : https://www.codewars.com/kumite/5bd52256bf1018afd8000031?sel=5bd52256bf1018afd8000031
It seems like some random cases have multiple solutions, in which case it expects what seems like the last sum you found if you work from left to right (might have been a coincidence, but that's what passed the random tests in my case). Any of the sums should be correct though.
Which language?
Some random test cases have more than one solution.
What language?
JavaScript
One of the submission set of cases ([ .......... ], 7), 5) appears to have a second possible solution of 1 + 3 + 5 = 9 in addition to the official solution of 3 + 5 - 3 = 5. Both are 2 away from the target value of 7.
Which language?
Code:
Output (excerpt):
The last figure on every first line should be the function result - but it's being mangled somehow. This happens on random tests only.
Any ideas?
looks like the expected and actual are the wrong way around.
No, expected values are correct. Actual values have a disconnect with code returns somehow.
no, I am correct the expected and actual values are the wrong way around
Ah, I see. You are correct, and the reference values are wrong (the actual values that should have been the expected ones).
Boy, this is a mess.
Whoops! Should be fixed now. Thanks for finding the issue.
That fixed the actual/expected switch.
The tests are still expecting the wrong answers. Are you sure of your reference implementation? Quite sure? Or am I reading the spec wrong? Expected answer is the sum you can actually make, not the difference you have to accept.
That's right: the expected output is the sum you can actually make that is closest to the target.
Can you show me an example of a case that doesn't appear to give the right answer?
Some randoms, not all. See first case in original post (have to switch actual and expected): can make -6, ref algorithm makes -5. 1 + -4 + -3 == -6
Derp, problem was with my javascript and coffeescript solutions. I think I've fixed them now.
That fixed most of it! :]
New problem:
[ 0, 5, -5, -4 ] -2 0 Expected: -4, instead got: 0
0 is as close to -2 as -4 is. My answer is as good as yours. :P Why should I answer -4 instead of 0?
I'm getting stuck at "should pass random tests". Passes everything before that flawlessly, but once it gets to that, I get the error
Traceback: in in solution NameError: name 'combinations' is not defined
Any ideas? Is this something I'm doing wrong? It doesn't seem like it because I shouldn't need to have something named combinations.
You're right. Problem with the tests. Should be fixed if you try again. Thanks!
I'm still getting the same error, does it just need time to update the tests or is there still a problem?
EDIT: Never mind, works great now. Thanks smepple.