Different output in leetcode and visual studio code for python (4. Median)

Question:

Recently I’ve just taken an interest in learning python, however, this one has really defeated me.
The specific question on Leetcode is "4. Median of Two Sorted Arrays":

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

I have tried to crake it by forming half of the final list (length would be "con") of the combined two arrays first by the use of accumulator "coffin" (an empty list to put elements in) and "pos" (counting the length of the generated list, if it is more than half of "con", the loop stops and returns the median), then find the median using list methods. However, to do this I relied on len(), which works in visual studio but apparently gives wrong output in Leetcode’s checking system.

def fn(nums1,nums2):
    pos = -1
    coffin = []
    con = len(nums1) + len(nums2)
    for i in nums1:
        for x in nums2:
            if x > i:
                coffin.append(i)
                pos += 1
                break
            #if x == i: # Median doesn't account for repeated number? for leetcode
                coffin.append(i)
                coffin.append(x)
                nums2 = nums2[1:]
                pos += 2
                break
            else:
                coffin.append(x)
                nums2 = nums2[1:]
                pos += 1
            if pos >= con / 2:
                if con % 2 == 0:
                    return (coffin[int(con / 2 -1)] + coffin[int(con / 2)]) / 2
                else:
                    return coffin[int(con/2)]
    for e in nums2:
        coffin.append(e)
        pos += 1
        if pos >= con / 2:
                if con % 2 == 0:
                    return (coffin[int(con / 2 -1)] + coffin[int(con / 2)]) / 2
                else:
                    return coffin[int(con/2)]

fn([1,2],[3,4) gives 2.5 in visual studio code, but gives 2.0 in Leetcode.

I suspect that the Leetcode stored the "con" value from previous test (input: [1,2], [3]), but since the values are defined inside the function they should be renewed every time new input is given…?

apparently people in the comment section are getting the correct answers in leetcode, I should post the one on leetcode as well then.

class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
    pos = -1
    coffin = []
    con = len(nums1) + len(nums2)
    for i in nums1:
        for x in nums2:
            if x > i:
                coffin.append(i)
                pos += 1
                break
            #if x == i: # Median doesn't account for repeated number? for leetcode
                coffin.append(i)
                coffin.append(x)
                nums2 = nums2[1:]
                pos += 2
                break
            else:
                coffin.append(x)
                nums2 = nums2[1:]
                pos += 1
            if pos >= con / 2:
                if con % 2 == 0:
                    return (coffin[int(con / 2 -1)] + coffin[int(con / 2)]) / 2
                else:
                    return coffin[int(con/2)]

    for e in nums2:
        coffin.append(e)
        pos += 1
        if pos >= con / 2:
                if con % 2 == 0:
                    return (coffin[int(con / 2 -1)] + coffin[int(con / 2)]) / 2
                else:
                    return coffin[int(con/2)]
Asked By: Monty

||

Answers:

You are running python2

Change your divisions by 2 from /2 to float divisions /2.0

You have two places where you are taking an average of two numbers, so divide by the floating number 2.0 instead of dividing by the integer number 2

So change

return (coffin[int(con / 2 -1)] + coffin[int(con / 2)]) / 2

for

return (coffin[int(con / 2 -1)] + coffin[int(con / 2)]) / 2.0
Answered By: Sembei Norimaki
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.