chaz1975
0
Q:

ansible vagrant plugin

# Python program to merge 
# two sorted arrays 
# with O(1) extra space. 
  
# Merge ar1[] and ar2[] 
# with O(1) extra space 
def merge(ar1, ar2, m, n): 
  
    # Iterate through all 
    # elements of ar2[] starting from 
    # the last element 
    for i in range(n-1, -1, -1): 
      
        # Find the smallest element 
        # greater than ar2[i]. Move all 
        # elements one position ahead 
        # till the smallest greater 
        # element is not found  
        last = ar1[m-1] 
        j=m-2
        while(j >= 0 and ar1[j] > ar2[i]): 
            ar1[j+1] = ar1[j] 
            j-=1
   
        # If there was a greater element 
        if (j != m-2 or last > ar2[i]): 
          
            ar1[j+1] = ar2[i] 
            ar2[i] = last 
   
# Driver program 
  
ar1 = [1, 5, 9, 10, 15, 20] 
ar2 = [2, 3, 8, 13] 
m = len(ar1) 
n = len(ar2) 
  
merge(ar1, ar2, m, n) 
   
print("After Merging \nFirst Array:", end="") 
for i in range(m): 
    print(ar1[i] , " ", end="") 
  
print("\nSecond Array: ", end="") 
for i in range(n): 
    print(ar2[i] , " ", end="") 
  
# This code is contributed 
# by Anant Agarwal. 
0
# Python3 program to find Intersection of two 
# Sorted Arrays (Handling Duplicates) 
def IntersectionArray(a, b, n, m): 
    ''' 
    :param a: given sorted array a 
    :param n: size of sorted array a 
    :param b: given sorted array b 
    :param m: size of sorted array b 
    :return: array of intersection of two array or -1 
    '''
  
    Intersection = [] 
    i = j = 0
      
    while i < n and j < m: 
        if a[i] == b[j]: 
  
            # If duplicate already present in Intersection list 
            if len(Intersection) > 0 and Intersection[-1] == a[i]: 
                i+= 1
                j+= 1
  
            # If no duplicate is present in Intersection list 
            else: 
                Intersection.append(a[i]) 
                i+= 1
                j+= 1
        elif a[i] < b[j]: 
            i+= 1
        else: 
            j+= 1
              
    if not len(Intersection): 
        return [-1] 
    return Intersection 
  
# Driver Code 
if __name__ == "__main__": 
  
    arr1 = [1, 2, 2, 3, 4] 
    arr2 = [2, 2, 4, 6, 7, 8] 
      
    l = IntersectionArray(arr1, arr2, len(arr1), len(arr2)) 
    print(*l) 
  
# This code is contributed by Abhishek Kumar 
0
# Python3 program to find union of two  
# sorted arrays (Handling Duplicates)  
def UnionArray(arr1, arr2):  
      
    # Taking max element present in either array  
    m = arr1[-1]  
    n = arr2[-1]  
    ans = 0
          
    if m > n:  
        ans = m  
    else: 
        ans = n  
          
    # Finding elements from 1st array  
    # (non duplicates only). Using  
    # another array for storing union  
    # elements of both arrays  
    # Assuming max element present  
    # in array is not more than 10 ^ 7  
    newtable = [0] * (ans + 1)  
          
    # First element is always  
    # present in final answer  
    print(arr1[0], end = " ")  
          
    # Incrementing the First element's count  
    # in it's corresponding index in newtable  
    newtable[arr1[0]] += 1
          
    # Starting traversing the first  
    # array from 1st index till last  
    for i in range(1, len(arr1)):  
          
        # Checking whether current element  
        # is not equal to it's previous element  
        if arr1[i] != arr1[i - 1]:  
              
            print(arr1[i], end = " ")  
            newtable[arr1[i]] += 1
              
    # Finding only non common  
    # elements from 2nd array          
    for j in range(0, len(arr2)):  
          
        # By checking whether it's already  
        # present in newtable or not  
        if newtable[arr2[j]] == 0:  
              
            print(arr2[j], end = " ")  
            newtable[arr2[j]] += 1
      
# Driver Code  
if __name__ == "__main__":  
      
    arr1 = [1, 2, 2, 2, 3] 
    arr2 = [2, 3, 4, 5] 
          
    UnionArray(arr1, arr2)  
  
# This code is contributed by Rituraj Jain 
0
# Python program to find union of 
# two sorted arrays 
# Function prints union of arr1[] and arr2[] 
# m is the number of elements in arr1[] 
# n is the number of elements in arr2[] 
def printUnion(arr1, arr2, m, n): 
    i, j = 0, 0
    while i < m and j < n: 
        if arr1[i] < arr2[j]: 
            print(arr1[i]) 
            i += 1
        elif arr2[j] < arr1[i]: 
            print(arr2[j]) 
            j+= 1
        else: 
            print(arr2[j]) 
            j += 1
            i += 1
  
    # Print remaining elements of the larger array 
    while i < m: 
        print(arr1[i]) 
        i += 1
  
    while j < n: 
        print(arr2[j]) 
        j += 1
  
# Driver program to test above function 
arr1 = [1, 2, 4, 5, 6] 
arr2 = [2, 3, 5, 7] 
m = len(arr1) 
n = len(arr2) 
printUnion(arr1, arr2, m, n) 
  
# This code is contributed by Pratik Chhajer 
0
# Function to find intersection of two arrays 
  
def interSection(arr1,arr2): 
  
     # filter(lambda x: x in arr1, arr2)  --> 
     # filter element x from list arr2 where x 
     # also lies in arr1 
     result = list(filter(lambda x: x in arr1, arr2))  
     print ("Intersection : ",result) 
  
# Driver program 
if __name__ == "__main__": 
    arr1 = [1, 3, 4, 5, 7] 
    arr2 = [2, 3, 5, 6] 
    interSection(arr1,arr2) 
0
# Python program to merge three sorted arrays 
# by merging two at a time. 
  
def merge_two(a, b): 
    (m, n) = (len(a), len(b)) 
    i = j = 0
  
    # Destination Array 
    d = [] 
  
    # Merge from a and b together 
    while i < m and j < n: 
        if a[i] <= b[j]: 
            d.append(a[i]) 
            i += 1
        else: 
            d.append(b[j]) 
            j += 1
  
    # Merge from a if b has run out 
    while i < m: 
        d.append(a[i]) 
        i += 1
  
    # Merge from b if a has run out 
    while j < n: 
        d.append(b[j]) 
        j += 1
  
    return d 
  
def merge(a, b, c): 
    t = merge_two(a, b) 
    return merge_two(t, c) 
  
if __name__ == "__main__": 
    A = [1, 2, 3, 5] 
    B = [6, 7, 8, 9] 
    C = [10, 11, 12] 
    print(merge(A, B, C)) 
0

Tags

New to Communities?

Join the community