Question: Please use PYTHON3 and follow the instructions to answer the question. 4. [2 points] Shuffling lists In shuffle.py, you will write a Python function shuffle(listl,
Please use PYTHON3 and follow the instructions to answer the question.
4. [2 points] Shuffling lists In shuffle.py, you will write a Python function shuffle(listl, list2) that takes two lists list1 and list2 as parameters and returns a list where the elements from list1 and list2 are shuffled together into one list The elements in the output list must be drawn alternately from list1 and list2. If one list is longer than the other, the extra elements should appear at the end of the output list. Write shuffle(listl, list2) as a recursive function. Use this algorithm: I. If list2 is empty, then return list1 as the shuffled list II. Otherwise, if list1 is empty, then return list2 as the shuffled list. II. Otherwise, do the following: A. Create a new list that is empty B. Since we know that list1 and list2 are both non-empty, append the first element of list1 to your new list and then append the first element of list2 to your new list Next, make a recursive call, passing the tail of list1 and the tail of list2 as the arguments. Append the shuffled list that is returned from this recursive call to your new list. (See hint below.) Return your new list as the resulting shuffled list. C. D. List hint: To append a whole list to another list, use the operator instead of the append function >>> L1-Ll L2 Example: >> python3 -i shuffle.py >>> shuffle ([1,2,3], [4,5,6]) >>> shuffle(,04,5,6]) [4, 5, 6] >>> shuffle([1,2,3],[) >>> shuffle([1,3,8,4,5,6,7]) [1, 8, 3, 4, 5, 6, 7] >>> shuffle([1,3,5,7],[2,9,6]) [1, 2, 3, 9, 5, 6, 7] @IMIaoll02 335422Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
