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,...

  • ****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

  • 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...

  • This is python coding recursive function question. Could anyone help me figuring out these not using...

    This is python coding recursive function question. Could anyone help me figuring out these not using built-in function? Thanks. Write a recursive function factorial(n) that returns the value of n!=1⋅2⋅3⋅⋯⋅n. 2. Write a recursive function reverse(text) that returns the reverse of the string named text. 3. Write a recursive function countUpper(s) that returns the number of uppercase letters in the string s. 4. Write a recursive function equal(list1, list2) that returns a Boolean value indicating whether the two lists are...

  • (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.

  • Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount,...

    Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount, an integer specifying the number of “buckets” to return and numberLs, a list of numbers ) buckets should split the range of values in numberLs into bucketCount sub-ranges. Specifically buckets should return a list-of-lists of length bucketCount. Each list in the returned list-of-lists represents a “bucket”. Each bucket should contain the numbers from numberLs whose values are greater than or equal to min(numberLs) +...

  • Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of...

    Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of its elements in some order. For example (1,3, 2) and (3, 2, 1) are two different permutations of L=(1,2,3). Implement the following function: def permutations (lst, low, high) The function is given a list 1st of integers, and two indices: low and high (lows high), which indicate the range of indices that need to be considered The function should return a list containing all...

  • PYTHON CODE In a program, write a function that accepts two arguments: a list, and a...

    PYTHON CODE In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. Output should look like: Number: 5 List of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List of numbers that are larger than 5: [6, 7, 8, 9, 10]

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
ADVERTISEMENT