Question: Write a recursive Python function called swapNeighbours that accepts a list as a parameter and returns a copy of the list with consecutive pairs of
Write a recursive Python function called "swapNeighbours" that accepts a list as a parameter and returns a copy of the list with consecutive pairs of elements swapped. The function should not change the parameter (it should only create a copy). If the list has an odd number of elements, the last element is not swapped and can remain in its original location - the other elements would be swapped.
Example:
>>> nums = [2,4,5,6] >>> swappedLis = swapNeighbours(nums) >>> swappedLis >>> [4,2,6,5] #this is the swapped list.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
