Question: Please provide the Python code for the following question: Task 2 - Adjacent element swap for a list (Topics: list, loops, functions, string manipulation. assert}

Please provide the Python code for the following question:

Please provide the Python code for the following question: Task 2 -Adjacent element swap for a list (Topics: list, loops, functions, string manipulation.

Task 2 - Adjacent element swap for a list (Topics: list, loops, functions, string manipulation. assert} [20pnts] The user will input a string as a series of characters separated by spaces. The input string should be converted to a list of characters. Task 2 is to swap the values of euer'III two items in the list; for example items at positions 0 and 1 are swapped; items 2 and 3 are swapped; items 4 and 5 are swapped. and so on. until the end of the list. You can assume that the list has at least two items if not. your swap function should raise an Asse rtionEr'r'on {see details below). Note that there are two cases: {1} the list has an even number of elements; [2] the list has an odd number of elements. For case 1, swap all elements. For case 2, swap all elements but leave the lost element unchanged. See the diagram below for examples. Case 1 - even number of elements Case 2 - odd number of elements initial list 'n't'al \"5\" '-a' '-b' 'c' f. 'H' 'e' .1. .1. .1. 'd' .1. .1. .. 'o' 'hl' .1. 'r' 'd' alter i.adjacency: element swap after adjaceni:I element swap Last element is unchanged. Require function In addition to the task2() function, you need to implement the following function: 5wapAdjacentE1ernents( pa r'arrIs: alist) ) no return The parameter alist is a list of characters. The function should modify the list as described above. Note that you should modify the list passed to the function and not create a new list. The function has no return. ASSER'I': This function should assert with the following error ifthe list has less than two {2} characters: "Must enter- two or- Inore characters! \" Task 2 should behave as follows: [1) Prompt the user to input a sequence of two or more characters separated by spaces. [2) Convert the in put string into a list of characters. (3) Print the list. [4) Print the list as a string with no spaces between the characters. [5) Call function swapAdj acentElements II) to modify the list. See instructions above. This function does not create a new list; instead, it should modify the list passed as an argument. (6) Print the list again to show that the list has been modified. [7!) Print the list as a string again with no spaces between the characters. Example of task 2 running (user input in red) - see video link for more examples. - - Task2: Swap Adjacent List Elements Input two or more chars separated by spaces: a b a b a b a b Initial list ['a', 'b', 'a', 'b', 'a', 'b' , 'a', 'b'] Example of case 1 (even # of items) String version: 'abababab Modified list [ 'b' , 'a', 'b' , 'a' , 'b' , String version: 'babababa' Task2: Swap Adjacent List Elements - Input two or more chars separated by spaces: th i s i s a test Initial list ['t' , 'h', 'i', 's', 'i', 's', 'a', 'e', 's', 't'] Example of case 2 (odd # of items) String version: 'thisisatest' Modified list ['h', 't', 's', 'i', 's', 'i', 'a' "'e', 't'] String version: 'htsisitaset' Task2: Swap Adjacent List Elements Input two or more chars separated by spaces: 1 2 3 4 5 Initial list ['1' , '2', '3', '4', '5'] String version: '12345' Modified list ['2', '1', '4', , '3', '5'] String version: '21435' Task2: Swap Adjacent List Elements Input two or more chars separated by spaces: 1 Example of that causes an Initial list AssertionError ['1'] String version: '1' Modified list Traceback (most recent call last): File "final.py", line 119, in main( ) File "final.py", line 110, in main task2() File "Cfinal.py", line 71, in task2 swapAdjancentElements(theList) File "final.py", line 55, in swapAdjancentElements assert len(theList) >= 2, "Error" AssertionError: Error

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!