Merge Sorted Array - Python - Runtime Error - too many positional arguments provided
427
Sep 23, 2021

The below runtime error is returned when I run my code via the Leetcode web console. I created test cases using the examples provided in the exercise and all tests pass when I run them in my IDE. I've included a link to my code and test cases below. Yes, I am aware that the exercise specifies not to return a value from merge(). I did include a return statement in my code for testing purposes but removed it from the code that I pasted into the Leetcode web console (pasted code below).

My question: Is this runtime error something that I need to troubleshoot as part of this exercise, or have I left the "safe area" of the exercises intended purpose? I.e. Is this something that I should try to troubleshoot and correct or have I somehow "broken" the Leetcode web console?

image

Code I pasted into the Leetcode Console (my test cases file is availabe for review via github)

from typing import List

class Solution:
    def merge(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """

        # nums1 pointer
        h = 0

        # nums2 pointer
        j = 0

        complete = False
        while not complete:
            # edgecase - nums2 is empty
            if not nums2:
                complete = True
                continue

            # edgecase - nums1 only contains placeholder elements
            if m == 0:
                nums1[:] = nums2
                complete = True
                continue

            while h < m + n:
                # case - the end of nums1 is found. Add the remaining nums2 elements to nums1
                if nums1[h] == 0:
                    nums1[h:] = nums2[j:]
                    h += len(nums2[j:])
                    break

                # case - nums2 element needs to be merged into nums1
                if nums1[h] > nums2[j]:
                    nums1.insert(nums2[j], h)
                    j += 1
                h += 1
            complete = True
Comments (4)