Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

The detailed ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Java Programs A Back To Basics Approach

Authors: Stuart Reges, Marty Stepp

5th Edition

013547194X, 978-0135471944

More Books

Students also viewed these Algorithms questions