Question: Write a function called interleave that takes two lists as parameters and returns a new list that contains the result of interleaving the elements

Write a function called interleave that takes two lists as parameters and

Write a function called interleave that takes two lists as parameters and returns a new list that contains the result of interleaving the elements of the two lists. It should not alter either of its parameters. Two lists are interleaved by taking elements in the following order: 1st element of 1st list 1st element of 2nd list 2nd element of 1st list 2nd element of 2nd list 3rd element of 1st list 3rd element of 2nd list and so on The following table shows the results of calling interleave given the lists defined below: list1= [1, 8, 3, 9] list2= [2, 12, 6, 14] list3 = [82, 7, 4, 2, 10, 20, 30, 401 call interleave (listl, list2) interleave (list2, listl) interleave (listl, list3) returned list [1, 2, 8, 12, 3, 6, 9, 14] [2, 1, 12, 8, 6, 3, 14, 9] [1, 82, 8, 7, 3, 4, 9, 2, 10, 20, 30, 401 If the lists are not the same length, the elements of the longer list get appended after the interleaved elements as shown in last example above. If either list is empty, the result should contain the values from the other list.

Step by Step Solution

3.42 Rating (152 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The detailed ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!