Question

ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

ANSWER USING PYTHON

Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of lists with the slice operator (if x = [0, 1, 2, 3, 4], x[1:] = [1, 2, 3, 4]). You could also use the other list methods that are available if you think they are applicable (e.g., pop). Remember to think about the base case(s) and how you will move toward a smaller problem at each step.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

########################################PGM START ###################################

def mergeList(l1,l2):

    #if both the list are non empty then

    if l1 and l2:

        #if first element of list 1 greater than first element of list 2

        #swap the first and second list

        if l1[0] > l2[0]:

            l1, l2 = l2, l1

        #return list of smallest elemet + call the merge list funtion with remaining element of list 1 and list 2

        return [l1[0]] + mergeList(l1[1:], l2)

    #finally return combined list l1 and l2 if atleast one of the list is empty

    return l1 + l2

#main function

if __name__=="__main__":

    #list1 and list2

    l1=[0,2,4,6,8]

    l2=[1,3,5,7,9]

    print("List 1 ==> "+str(l1))

    print("List 2 ==> "+str(l2))

    #calling the mergeList funtion with list1 & list 2

    print("Merged List ==> "+str(mergeList(l1,l2)))

############################### PGM END #########################################

def mergeList(1,12): #if both the list are non empty then if 11 and 12: # if first element of list 1 greater than first element of list 2 #swap the first and second list if 1[0] >12[0] 11, 12-12, 11 #return list of smallest elemet + call the merge list funtion with remaining element of list 1 and list 2 return [1[O mergeList(l1[1:], 12) #finally return combined list 11 and 12 if atleast one of the list is empty return l112 #main function if__name____main__ #list1 and list2 1-10,2,4,6,8 12-[1,3,5,7,9] print(List 1>+str(1)) print(List 2>str(12)) #calling the mergeList funtion with list! & list 2 print(Merged List => +str(mergeList(11,12))) OUTPUT List 1 ==> [0, 2, 4, 6, 8] List 2 -> [1, 3, 5, 7, 9] Merged List [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Add a comment
Know the answer?
Add Answer to:
ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...

    IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a character list and the other is the corresponding frequencies, and sorts the characters in ascending order of frequencies. The function should return a list consististing of two lists: the first list is the list of characters and the second is the list of corresponding frequencies, consistent with the rearranged characters. Name this function sortCharacters(characterList, frequencyList). For example, >>>sortCharacters(["a", "b", "c", "d"], [5, 8, 3,...

  • Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns...

    Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns a list consisting of the elements of L multiplied by the index number of the element at odd positions. (Use list comprehensions) >>> times_i_at_odd([1,2,3,4,5,6,7,8,9,10]) [2, 12, 30, 56, 90] Problem 2. Write a recursive function sum_cols(grid, n) that takes a list of lists of integers grid and integer n and returns the sum of column n in grid. For example, the call sum_cols([[1,2,3,4], [10,20,30,40],...

  • 1. Define a function in python that returns the sum of the following 4 lists. Remember...

    1. Define a function in python that returns the sum of the following 4 lists. Remember to store and re- use your code instead of summing the values for each list 4 times. Hence, define a function that takes as an argument a list and returns the sum of values. Copy the following lists and paste them in a Python file after you define the sum_of_values function Call the function and pass to it a different list each call as...

  • With using Python . Write a function that: 1. Takes two arguments and returns the product...

    With using Python . Write a function that: 1. Takes two arguments and returns the product of the arguments. The return value should be of type integer. 2. Takes two arguments and returns the quotient of the arguments. The return value should be of type float. 3. Takes one argument and squares the argument. It then uses the two values and returns the product of the two arguments.(Reuse function in 1.) 4. Takes one argument and prints the argument. Use...

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Using Python: write a function that takes two arguments: (1) a target item to find, and...

    Using Python: write a function that takes two arguments: (1) a target item to find, and (2) a list. Your function should return the index (offset from 0) of the last occurrence of the target item or None if the target is not found. E.g. the last occurrence of 33 is at offset 3 in the list [ 42, 33, 21, 33 ] because 42 is offset 0, the first 33 is at offset 1, 21 is offset 2, and...

  • (20 points) ) Write a Python program which merges two sorted singly linked lists and return...

    (20 points) ) Write a Python program which merges two sorted singly linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Example: Input: 1->2- >4, 1->3->4 Output: 1->1->2->3 ->4->4

  • (for python) [27] Write a recursive function to calculate the following for a given input value...

    (for python) [27] Write a recursive function to calculate the following for a given input value for x. result = 1+ 2+ 3 ..... + x For example: if the input for x is 5, the result will be 1+2+3+4+5 = 15 if the input for x is 8, the result will be 1+2+3+4+5+6+7+8 = 36 Write recursive function.

  • PYTHON: Using Lists and Arrays! 5. In a program, write a function that accepts two arguments:...

    PYTHON: Using Lists and Arrays! 5. In a program, write a function that accepts two arguments: a list, and a number n. The program should generate 20 random numbers, in the range of 1 through 100, and assign each number to the list. The program should also ask the user to pick a number from 1 through a 100 and assign that number to n. The function should display all of the number in the list that are greater than...

  • Python. Write a function swap_lists that takes in two lists and swap their elements in place....

    Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT