0

I found a partial solution to the problem; however, it seems that I'm getting extra numbers from my array than what it should be. This is the question I'm trying to find out:

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. Example:

Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3

Output: [1,2,2,3,5,6]

I'm practicing some coding challenges to the hang of Python3 language and prepare myself for an interview. I have tried a few methods like using pop when the beginning of the array are 0s. But it seems that after new test case showed up, I should've expected more. I'm pretty new with the language.

def mergeArrays(nums1, m, nums2, n):
    nums1[:] = sorted(nums1 + nums2)
    i = 0
    while (i < len(nums1[:-1])):
        if nums1[i] is 0:
            nums1.pop(i)
        if i > len(nums1):
            break
        i += 1
    print(nums1)

nums1 = [-49,-48,-48,-47,-45,-42,-39,-36,-33,-33,-28,-28,-23,-23,-7,-4,-3,0,0,4,6,21,29,29,31,34,36,38,40,43,45,46,47,0,0,0,0,0,0,0,0]
m = len(nums1)

nums2 = [-16,-5,-3,26,33,35,38,41]
n = len(nums2)
mergeArrays(nums1, m, nums2, n);

My expected output should be of both arrays sorted and go through. Results should be this: [-49,-48,-48,-47,-45,-42,-39,-36,-33,-33,-28,-28,-23,-23,-16,-7,-5,-4,-3,-3,0,0,4,6,21,26,29,29,31,33,34,35,36,38,38,40,41,43,45,46,47]

However, I'm getting a couple extra zeros, which should look like this: [-49,-48,-48,-47,-45,-42,-39,-36,-33,-33,-28,-28,-23,-23,-16,-7,-5,-4,-3,-3,0,0,0,0,0,4,6,21,26,29,29,31,33,34,35,36,38,38,40,41,43,45,46,47]

EDIT: added more information to make the problem clear.

Zeid Tisnes
  • 396
  • 5
  • 22
  • 1
    One thing: never do `if nums1[i] is 0` ([that tests for identity, not equality](https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce)), always use `== 0`. Otherwise your code might happen to work on small integers, but break on strings or other objects. – smci Jan 29 '19 at 08:06
  • I don't understand. You should be getting 10 zero's as you have ten in nums1, but you are getting less. Is there any constraints, such as max occurence is two? – Osman Mamun Jan 29 '19 at 08:10
  • I don't understand the intent of the code either. If you want to simultaneously sort, merge and limit the multiplicity of elements to 2, you could use `collections.Counter`. – smci Jan 29 '19 at 08:13
  • 2
    Please clarify what you are trying to do. If you just want to merge the arrays and sort them `nums1 = sorted(nums1 + nums2)` is enough and you should not mess with the output further. Also that code is downright strange.. why do you have a break statement if the range ensures you'll never reach it? – KGS Jan 29 '19 at 08:16
  • 3
    The while-loop seems cryptic and unnecessary, you could use a list comprehension if you simply want to get all non-zero results: `[x for x in sorted(nums1 + nums2) if x != 0]` – smci Jan 29 '19 at 08:17
  • 1
    Also you don't need to pass lengths `m` and `n` into `mergeArrays()`, they're never used. Possibly you don't even need a function `mergeArrays()`, just a one-liner... Please tell us precisely what your code is intending to do? Why do you need to treat zero specially? – smci Jan 29 '19 at 08:20
  • @smci correct, but I'm not comparing strings or objects in this case, just integers. For the first method, I tried was a loop comprehension as well but no luck because I want to conserve the 0s from the middle. In general, yes this looks cryptic (after having a good sleep and realizing it is not readable enough and clear question) – Zeid Tisnes Jan 29 '19 at 18:06
  • @mamun what I think it is is because I'm sorting it first without checking the if the last number is greater than zero I think. – Zeid Tisnes Jan 29 '19 at 18:06
  • @ZeidTisnes: you're missing my point. In general never use `x is 0`, it's not a test for equality, that's `==`. Also that way you'll avoid writing bad code that wrongly passes on small integers then mysteriously breaks on anything else. – smci Jan 29 '19 at 20:48
  • Ok back to the cryptic intent of your code and where your unwanted mystery zeros might be coming from: it seems your arrays `nums1, nums2` were zero-padded, and can be longer than length m,n respectively. It's a mistake to reference the padded entries. So you want `x for x in sorted(nums1[:m] + nums2[:n]) if x != 0]` . It's still a one-liner. – smci Jan 29 '19 at 20:51
  • **`sorted(nums1 + nums2)` is your mistake, you included the zero-padding on both arrays**. Instead you should have written `sorted(nums1[:m] + nums2[:n])` – smci Jan 29 '19 at 21:02

3 Answers3

0

As per my understanding you want to sort the two sorted array without having any duplicate element. You can refer the below code:

first_list = [-49,-48,-48,-47,-45,-42,-39,-36,-33,-33,-28,-28,-23,-23,-7,-4,-3,0,0,4,6,21,29,29,31,34,36,38,40,43,45,46,47,0,0,0,0,0,0,0,0]
second_list = [-16,-5,-3,26,33,35,38,41]
merged_list = list(set(first_list+second_list))
merged_list.sort()
print(merged_list)
Bishal Dubey
  • 150
  • 7
0

With one of the old methods that I used was a loop comprehension. Basically, what I did was array splice from beginning to end and do the sort inside of the loop:

def mergeArrays(nums1, m, nums2, n):
    nums1[0: m + n] = [x for x in sorted(nums1[:m] + nums2[:n])]

If you have a different explanation than what I just did, please feel free :)

Zeid Tisnes
  • 396
  • 5
  • 22
0

After much back-and-forth on the intent of your code and where your unwanted mystery zeros come from, seems you want to do the following: merge-sort your two arrays, preserving duplicates:

  • your input is arrays nums1, nums2 which are zero-padded, and can be longer than length m,n respectively

    • But to avoid picking up those padded zeros, you should only reference the entries 0..(m-1), i.e. nums1[:m], and likewise nums2[:n]
    • Your mistake was to reference all the way up to nums1[:-1]
  • Your solution is: sorted(nums1[:m] + nums2[:n]). It's a one-liner list comprehension and you don't need a function.

    • There is no reason whatsoever that zero entries need special treatment. There's no need for your while-loop.
  • Also btw even if you wanted to (say) exclude all zeros, you can still use a one-liner list-comprehension: x for x in sorted(nums1[:m] + nums2[:n]) if x != 0]
  • List comprehensions are a neat idiom and super-powerful! Please read more about them. Often you don't need while-loops in Python; list comprehensions, iterators or generators are typically cleaner, shorter code and more efficient.
smci
  • 32,567
  • 20
  • 113
  • 146