The function interleave_lists in python takes two parameters, L1 and L2, both lists. Notice that the lists may have different lengths. The function accumulates a new list by appending alternating items from L1 and L2 until one list has been exhausted.  The remaining items from the other list are then appended to the end of the new list, and the new list is returned. For example, if L1 = ["hop", "skip", "jump", "rest"] and L2 = ["up", "down"], then the function would return the list: ["hop", "up", "skip", "down", "jump", "rest"]. HINT: Python has a built-in function min() which is helpful here.    Initialize accumulator variable newlist to be an empty list Set min_length = min(len(L1), len(L2)), the smaller of the two list lengths Use a for loop to iterate k over range(min_length) to do the first part of this function's work. On each iteration, append to newlist the item from index k in L1, and then append the item from index k in L2 (two appends on each iteration). AFTER the loop finishes, append the remaining items (if any) to newlist; use a decision statement to determine which list has remaining items -- then it just takes one line of code to append those items to newlist (using a slice of L1 or L2) return newlist   For example: Test Result L1 = ["hop", "skip", "jump"] L2 = ["A", "B", "C", "D", "E"] print(interleave_lists(L1, L2)) ['hop', 'A', 'skip', 'B', 'jump', 'C', 'D', 'E'] L1 = ["dive"] L2 = ["hop", "skip", "jump"] print(interleave_lists(L1, L2)) ['dive', 'hop', 'skip', 'jump'] cats = ["Ziggy", "Troll Boy"] dogs = ["Ruthie", "Ame"] print(interleave_lists(cats, dogs)) ['Ziggy', 'Ruthie', 'Troll Boy', 'Ame

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

The function interleave_lists in python takes two parameters, L1 and L2, both lists. Notice that the lists may have different lengths.

The function accumulates a new list by appending alternating items from L1 and L2 until one list has been exhausted.  The remaining items from the other list are then appended to the end of the new list, and the new list is returned.

For example, if L1 = ["hop", "skip", "jump", "rest"] and L2 = ["up", "down"], then the function would return the list: ["hop", "up", "skip", "down", "jump", "rest"].

HINT: Python has a built-in function min() which is helpful here. 

 

  • Initialize accumulator variable newlist to be an empty list
  • Set min_length = min(len(L1), len(L2)), the smaller of the two list lengths
  • Use a for loop to iterate k over range(min_length) to do the first part of this function's work. On each iteration, append to newlist the item from index k in L1, and then append the item from index k in L2 (two appends on each iteration).
  • AFTER the loop finishes, append the remaining items (if any) to newlist; use a decision statement to determine which list has remaining items -- then it just takes one line of code to append those items to newlist (using a slice of L1 or L2)
  • return newlist

 

For example:

Test Result
L1 = ["hop", "skip", "jump"] L2 = ["A", "B", "C", "D", "E"] print(interleave_lists(L1, L2)) ['hop', 'A', 'skip', 'B', 'jump', 'C', 'D', 'E']
L1 = ["dive"] L2 = ["hop", "skip", "jump"] print(interleave_lists(L1, L2)) ['dive', 'hop', 'skip', 'jump']
cats = ["Ziggy", "Troll Boy"] dogs = ["Ruthie", "Ame"] print(interleave_lists(cats, dogs)) ['Ziggy', 'Ruthie', 'Troll Boy', 'Ame']

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education