Reputation: 25
I'm trying to merge two arrays and return the sorted array (#88 on leetcode) and this the code I used:
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
if (m > 0 and n > 0):
merged = nums1[0:m] + nums2[0:n]
result = sorted(merged)
print(result)
return result
Input is
[1,2,3,0,0,0,0]
3
[2,5,6]
3
Can someone please explain why print and return are giving different outputs?
Upvotes: 1
Views: 428
Reputation: 4409
Going to the question and choosing python3 as the language, we are greeted with the following stub:
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
Note the comment - the testing framework is looking at the state of nums1 and not the value you return - in fact, the specification is to NOT return anything.
You're printing out the return variable, which is, indeed, what you return. But the variable actually used for the purposes of this question is not the return value of this function.
Reading the specification, in whatever format it is presented is always a good first step to take before writing anything.
Upvotes: 2