Question
PYTHON: MUST NOT USE PYTHONS BUILT IN FUNCTIONS SUCH AS result.append MUST BE SOLVED OUT WITHOUT SUCH FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1. Write a function interleave(vals1, vals2)
PYTHON:
MUST NOT USE PYTHONS BUILT IN FUNCTIONS SUCH AS result.append MUST BE SOLVED OUT WITHOUT SUCH FUNCTIONS
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1. Write a function interleave(vals1, vals2) that takes as inputs two lists vals1 and vals2 and uses recursion to construct and return a new string that is formed by interleaving the elements in the lists vals1 and vals2 to create a single list. In other words, the new list should alternate elements from the two input lists: the first element from vals1, followed by the first element from vals2, followed by the second element from vals1, followed by the second element from vals2, etc. If one of the lists is longer than the other, its extra elements the ones with no counterparts in the shorter list should appear immediately after the interleaved elements (if any) in the new list. For example:
>>> interleave([1, 1, 1], [2, 2, 2]) result: [1, 2, 1, 2, 1, 2] >>> interleave([3, 4, 5, 6], [7, 8, 9, 0]) result: [3, 7, 4, 8, 5, 9, 6, 0] >>> interleave([0, 0, 0, 0], [1, 1]) # two extra elements in first list result: [0, 1, 0, 1, 0, 0] >>> interleave([2, 1, 0], [3, 4, 5, 6]) # one extra element in second list result: [2, 3, 1, 4, 0, 5, 6]) >>> interleave([1, 2], []) # all of the first list's values are extra! result: [1, 2] >>> interleave([], [3, 4, 5]) # all of the second list's values are extra! result: [3, 4, 5] >>> interleave([], []) result: []
Hint: You will need more than one base case.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started