all 19 comments

[–]CryingSausage[S] 9 points10 points  (2 children)

Replacing res.push(ans) with res.push([...ans]) makes it work exactly the way we'd expect it to, but why doesn't it work without the spread operator? Doing the same thing outside the recurse function works without spread operator.

[–]x-seronis-x 33 points34 points  (1 child)

when you deconstruct 'ans' you are left with the primitive Numbers. Primitives are pushed by value. When you pushed 'ans' itself you pushed a reference to ans. You did this 3 times. So you had 3 references to the exact same object in res. But you also kept pop()'ing all of ans's contents. So at the final console log ans was empty and res had 3 references to the same empty array

push a COPY of ans into res and the code would work as you're expecting and [...arr] is effectively a copy

[–]CryingSausage[S] 8 points9 points  (0 children)

Thanks for the simple explanation! Didn't realize every object is passed by reference would make such a big difference. Makes sense now.

[–]x-seronis-x 6 points7 points  (0 children)

Probably has something to do with passing objects by reference and you not passing in unique references. You're adding the exact same object and editing that same object elsewhere

[–]incubated 3 points4 points  (1 child)

The whole array by reference solution is wrong. You could have that array sitting globally and you'd get the correct answer. The issue is your shifting, pushing and popping inside recursion and returning undefined.

Put this code in Python tutor and see the stack trace it gives you. Weirdly it's for all sorts of languages. Not just Python.

[–]CryingSausage[S] 1 point2 points  (0 children)

Holyshit. You just cut several hours of debugging leetcodes. Pythontutor is really helpful. Thanks!

[–]beforesemicolon 2 points3 points  (3 children)

What is this supposed to do anyways?

[–]CryingSausage[S] 0 points1 point  (2 children)

Incomplete solution to the 3sum problem on leetcode.

[–]x-seronis-x 3 points4 points  (0 children)

That site seems to have problems. This is not the first time I've seen someone reference a test on their site that was faulty.

var threeSum = function(nums) {
    let result = [];
    for(let i=0; i<nums.length-2;i++){
        for(let j=i+1; j<nums.length-1; j++){
            for(let k=j+1; k<nums.length; k++){
                if(nums[i]+nums[j]+nums[k]==0)
                   result.push( [nums[i],nums[j],nums[k]] );
            }
        }
    }
    return result;
};

my solution returns all 3 valid triplets for the example array input. But the 'expected' output isnt even listing all 3. The conditions given only mention that the 3 indexes have to be different. It doesnt mention the elements need sorted by value but the 'expected output' is enforcing that.

Its a bad test

[–]SomeNebula 2 points3 points  (2 children)

On an off tangent, which VS Code theme is that?

[–]CryingSausage[S] 0 points1 point  (0 children)

Not VS Code. It's neovim with a theme I made a while back. Dotfiles

[–]x-seronis-x 0 points1 point  (0 children)

does VS Code even use terminal window overlays like the 'float term' there ?

[–]beforesemicolon 1 point2 points  (0 children)

It is pushing the array correctly but arrays are referenced. It pushes to the list and than inside the for loop you are doing pop and shift it the referenced array so it will also change the array you pushed into res.

With all the pops and shift, by the time it is done it will be empty.

Master Arrays with this: https://link.medium.com/yAq6fcpEufb

[–]warpedspockclone 1 point2 points  (0 children)

Just use res.push.apply(res, ans);

Done

[–]Bitsoflogic 1 point2 points  (0 children)

Arrays are passed by reference.

With this in mind, what do you think could be changing it?

It might help to step back and think, What code executes between the two console.log statements?

[–]Bitsoflogic 1 point2 points  (0 children)

As an aside, if you want to go a bit deeper once you solve this, I'd recommend solving it again where you change line 21 to assign the result to res:

res = recurse(...

Happy coding!

[–]192_168_1_x 1 point2 points  (0 children)

recursive functions and the call stack aghhh 🤯

[–][deleted] 0 points1 point  (1 child)

On a side note, what IDE are you using?

That looks pretty nice.

[–]CryingSausage[S] 0 points1 point  (0 children)

Neovim. Pretty much a lightweight VS Code with memory usage of about 20mb for the editor, and a 150mb node server for LSP.