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]     """
Python Implement function swapFL() that takes a list as input and swaps the first and last...
Python Implement function swapFL() that takes a list as input and swaps the first and last ele- ments of the list. You may assume the list will be nonempty. The function should return the new list. >>> ingredients = ['flour', 'sugar', 'butter', 'apples'] >>> swapFL(ingredients) >>> ingredients ['apples', 'sugar', 'butter', 'flour']
Python Implement function allEven() that takes a list of integers and returns True if all integers...
Python Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False
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.
Implement a function that reverses a singly linked list in one pass of the list. The...
Implement a function that reverses a singly linked list in one pass of the list. The function accepts a pointer to the beginning of the list and returns a pointer to the beginning of the reversed list. The function must not create any new nodes. Be sure to test your function! You may start with code that you have written for other labs. This is a common interview question!
Python Write function words() that takes one input argument—a file name—and returns the list of actual...
Python Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
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
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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT