Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference in python execution on LeetCode and locally

** Update: This was a typo on my part, but I was struggling with the issue all morning. Sometimes something this obvious can elude you even if it is right in your face.

I have solved a question on LeetCode and I was trying to run the same solution that I had developed for the problem locally. I see a weird issue where LeetCode runs my solution perfectly and all my test cases pass but when I run the same solution, just slightly modified to give inputs and display results in the command line I do not get the same output.

After debugging I see that the "not in" part of the if statement is not working the same as the LeetCode website. I would appreciate an explanation of this issue since I usually code LeetCode problems locally and I am not sure what is causing this issue.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Python version: Python 3.11.5

PC: Windows 11

LeetCode Solution:

class Solution:
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        nums1 = set(nums1)
        nums2 = set(nums2)
        tmp1 = []
        tmp2 = []

        for i in nums1:
            if i not in nums2:
                tmp1.append(i)

        for i in nums2:
            if i not in nums1:
                tmp2.append(i)
                
        return [tmp1, tmp2]

Output: [[1,3],[4,6]]

Local Solution:

class Solution:
    def findDifference(self, nums1, nums2):
        nums1 = set(nums1)
        nums2 = set(nums2)
        tmp1 = []
        tmp2 = []

        for i in nums1:
            if i not in nums2:
                tmp1.append(i)

        for i in nums2:
            if i not in nums1:
                tmp2.append(i)

        return [tmp1, tmp2]


nums1 = [1,2,3]
nums2 = [2,4,6]
execute = Solution()
print(execute.findDifference(nums1, nums1))

Output: [[], []]

Thanks for your response in advance.

>Solution :

You have a typo in your print statement, both parameters are nums1:

      print(execute.findDifference(nums1, nums1))

It should be:

      print(execute.findDifference(nums1, nums2))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading