6 kyu

Closest Sum

322 of 567smepple
Description
Loading description...
Fundamentals
  • Please sign in or sign up to leave a comment.
  • ejini战神 Avatar

    Description should be language-agnostic

  • ejini战神 Avatar

    JS Node 18. should be enabled

  • Iluxmas Avatar

    5 years old issue is still not resolved. Bad kata

  • saudiGuy Avatar

    python new test framework is required. updated in this fork

  • cbserra Avatar

    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.

    Time: 903ms Passed: 1Failed: 2Errors: 2Exit Code: 2
    
    Test Results:
    
    solution
    
    should work for basic tests (PASSES)
    
    should not modify the array (FAILS)
    
    ReferenceError: ints is not defined
        at Context.<anonymous> (test.js:81:12)
        at process.processImmediate (node:internal/timers:471:21)
    Completed in 1ms
    
    should pass random tests (FAILS)
    
    ReferenceError: ints is not defined
        at Context.<anonymous> (test.js:114:12)
        at process.processImmediate (node:internal/timers:471:21)
    
  • benjaminzwhite Avatar

    This comment has been hidden.

  • Chrono79 Avatar

    Javascript, both -3 and -5 are 1 unit away of -4

    [ -7, 7, -1, -3, -9, -5, 1 ] -4
    Expected: -5, instead got: -3
    

    You may assume that each input would have exactly one solution.

    That isn't true.

  • pacuna Avatar

    Is it possible to solve this problem without libraries or brute force?

  • Bobke Avatar

    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.

  • kevin.du Avatar

    Some random test cases have more than one solution.

  • ChristopherBlair Avatar

    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.

  • JohanWiltink Avatar

    Code:

    function closestSum (ints, num) {
      var r = // expression that works flawlessly. (I think. But I _really_ think so!)
      console.log (ints, num, r);
      return r;
    }
    

    Output (excerpt):

    should pass random tests
    [ 2, 1, -4, -2, 4, -9, 3, -3 ] -6 -6
    Expected: -6, instead got: -5
    [ 8, -5, -7, 9, -3, 2 ] -8 -8
    Expected: -8, instead got: -4
    [ -2, -5, 2, 8, 9, -7, -1, 0, -9, -8 ] 4 4
    Expected: 4, instead got: 5
    [ 1, -6, -3, -8, -7, -5 ] 4 -7
    Expected: -7, instead got: -8
    

    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?

  • dtorrance Avatar

    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.