Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

merge two sorted arrays python

def merge_two_sorted_arrays(nums1, nums2)
  while m > 0 and n > 0 : 
    if nums1[m-1] >= nums2[n-1]:
      nums1[n+m-1] = nums1[m-1]
      m = m - 1 
      else:
        nums1[n+m-1] = nums2[n-1]
        n = n - 1 
        if n > 0 :
          nums1[0:n] = nums2[:n]

          return 
Comment

How to merge Two Sorted Arrays in Python

def merge(num1, num2):
    arr3 = num1+num2
    arr3.sort()
    return arr3
    
arr1 = [3, 5, 6, 10]
arr2 = [1, 2, 7, 8, 11, 12]
assert merge(arr1, arr2) == [1, 2, 3, 5, 6, 7, 8, 10, 11, 12]
arr1 = [1, 3, 4, 5]
arr2 = [2, 4, 6, 8]
assert merge(arr1, arr2) == [1, 2, 3, 4, 4, 5, 6, 8]
arr1 = [5, 8, 9]
arr2 = [4, 7, 8]
assert merge(arr1, arr2) == [4, 5, 7, 8, 8, 9]
Comment

merge two arrays python

[1,2,3] + [4,5,6] # [1,2,3,4,5,6]
Comment

How to merge Two Sorted Arrays in Python

def merge(num1, num2):
    arr3 = num1+num2
    arr3.sort()
    return arr3
    
arr1 = [3, 5, 6, 10]
arr2 = [1, 2, 7, 8, 11, 12]
assert merge(arr1, arr2) == [1, 2, 3, 5, 6, 7, 8, 10, 11, 12]
arr1 = [1, 3, 4, 5]
arr2 = [2, 4, 6, 8]
assert merge(arr1, arr2) == [1, 2, 3, 4, 4, 5, 6, 8]
arr1 = [5, 8, 9]
arr2 = [4, 7, 8]
assert merge(arr1, arr2) == [4, 5, 7, 8, 8, 9]
Comment

How to merge Two Sorted Arrays in Python

def merge(num1, num2):
    arr3 = [None]*(len(num1)+len(num2))
    i, j, k = 0, 0, 0
    
    while i < len(arr1) and j < len(arr2):
        if arr1[i] < arr2[j]:
            arr3[k] = arr1[i]
            k += 1
            i += 1
        else:
            arr3[k] = arr2[j]
            k += 1
            j += 1
            
    while i < len(num1):
        arr3[k] = arr1[i];
        k += 1
        i += 1
        
    while j < len(num2):
        arr3[k] = arr2[j];
        k += 1
        j += 1
    
    return arr3
                
arr1 = [3, 5, 6, 10]
arr2 = [1, 2, 7, 8, 11, 12]
assert merge(arr1, arr2) == [1, 2, 3, 5, 6, 7, 8, 10, 11, 12]
arr1 = [1, 3, 4, 5]
arr2 = [2, 4, 6, 8]
assert merge(arr1, arr2) == [1, 2, 3, 4, 4, 5, 6, 8]
arr1 = [5, 8, 9]
arr2 = [4, 7, 8]
assert merge(arr1, arr2) == [4, 5, 7, 8, 8, 9]
Comment

PREVIOUS NEXT
Code Example
Python :: python regions 
Python :: add list python 
Python :: python string to list of chars 
Python :: # extract images from pdf file 
Python :: combining strings in python 
Python :: python program to display fibonacci sequence using recursion 
Python :: Static Language Programmers 
Python :: string.format() with {} inside string as string 
Python :: Socket Programming Server Side 
Python :: style django forms with crisp 
Python :: sequence with numbers in python 
Python :: deleting an object in python 
Python :: python how to delete a variable 
Python :: get length of string python 
Python :: create new columns pandas from another column 
Python :: python server 
Python :: django rest framework serializers 
Python :: turn off colorbar seaborn heatmap 
Python :: demonstrating polymorphism in python class 
Python :: generate hmach sha256 hash in python 
Python :: pandas shape 
Python :: boolien in python 
Python :: random playing card generator python 
Python :: how to remove new line in python 
Python :: instalar sympy en thonny 
Python :: heading none in pandas import 
Python :: combine column in csv python pandas 
Python :: import csv as dic 
Python :: Resource stopwords not found 
Python :: python unicode function 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =