Question

In Python, implement a function that takes in two sorted lists and merges them into one...

In Python, implement a function that takes in two sorted lists and merges them into one list, the new list must be sorted. The function MUST run in O(m+n), where m is the length of list 1 and n the length of list 2. In other words, the function cannot do more than one pass on list 1 and list 2.

Homework Answers

Answer #1

CODE IN PYTHON:

def merge(list1, list2):
    i = 0
    j = 0
    m = len(list1)
    n = len(list2)
    resultList = []
    while(i < m and j < n):
        if(list1[i] <= list1[j]):
            resultList.append(list1[i])
            i += 1
        else:
            resultList.append(list2[j])
            j += 1
    if i < m:
        while i < m:
            resultList.append(list1[i])
            i += 1
    else:
        while j < n:
            resultList.append(list2[j])
            j += 1
    return resultList

list1 = [1, 4, 7, 12, 16, 19]
list2 = [2, 3, 5, 8, 10]
print("list1 : ", list1)
print("list2 : ", list2)
print("after merging sorted list : ", merge(list1, list2))

INDENTATION:

OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function intersect(L, M) that consumes two sorted lists of distinct integers L and M,...
Write a function intersect(L, M) that consumes two sorted lists of distinct integers L and M, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. Example: intersect([3, 7, 9, 12, 14], [1, 2, 5, 7, 10, 11, 12]) => [7, 12] Hint: As the title hints at, you are...
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses...
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses the list. You should mutate the original list, without creating any new lists. Do NOT return anything. Do not use any built-in list functions such as reverse(). def reverse(lst):    """Reverses lst in place (i.e. doesn't create new lists).     >>> L = [1, 2, 3, 4]     >>> reverse(L)     >>> L     [4, 3, 2, 1]     """
In Python language: Create a function that takes as input two numbers, m and n, m<n,...
In Python language: Create a function that takes as input two numbers, m and n, m<n, and returns an m×n list-of-list-of-numbers. Each element of the outer list will be a list of consecutive integers, beginning with 1 and ending with n−1. If you're feeling bold, try to use list comprehension.
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please
Python 3 Implement the function invert_and_merge, which takes any number of input dictionaries via a star...
Python 3 Implement the function invert_and_merge, which takes any number of input dictionaries via a star parameter, and inverts and merges them into a single result dictionary. When inverting a dictionary, the keys and values are flipped, so that each value maps to a set containing the corresponding key(s) in the original dictionary. When merging the inverted dictionaries, sets corresponding to the same key are combined. Examples: invert_and_merge({'a': 1, 'b': 2, 'c': 1, 'd': 1, 'e': 2}) should return {1:...
Implement function subset that takes a set of positive integers and returns asubset of it that...
Implement function subset that takes a set of positive integers and returns asubset of it that includes only those numbers that can form a sequence. It's ok if more than one sequences co-exist in the subset. You may not use lists, tuples, strings, dictionaries; only set functions/methods are allowed. Examples: subset({0,4,11,5,3,2,7,9})    ->    {4,5,3,2} subset({3,1,6,8,2,12,9})      ->    {3,1,2,8,9} True False In Python Please
Write a Python function count_bigger that takes two parameters, a nested list of objects and a...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a threshold number. It returns an integer specifying how many of the objects anywhere in the nested list are numbers that are larger than the threshold. (For our purposes, "numbers" are either integers or floats.) There may be objects in the list other than numbers, in which case you would simply ignore them. Here are a couple of examples of how the function should behave...
Implement function swap that takes a 2D list (a list of list of integers) and modifies...
Implement function swap that takes a 2D list (a list of list of integers) and modifies it in-place by swapping the first element in each row with the last element in that row. The return value is None. You may not use slicing. You maynot use any list method like append, insert, etc. Modification must be in-place, you may not move the list itself or any of its sublists to a new memory location. Examples: reverse([[1,2,3],[4,5,6]])        ->    [[3,2,1],[6,5,4]] reverse([[1,2,3,4],[5,6,7,8]])    ->    [[4,2,3,1],[8,6,7,5]]...
python 3 For this exercise you are to implement the function poly_iter in the array-backed list...
python 3 For this exercise you are to implement the function poly_iter in the array-backed list class, which, when called with positive integer parameters a, b, c, returns an iterator over the values in the underlying list at indexes a*i2+b*i+c, for i=0, 1, 2, ... E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98, 99], the following code: for x in lst.poly_iter(2, 3, 4): print(x) will produce the output: 4 9 18 31...