Task 2- Adjacent element swap for a list (Topics: list, loops, functions, string manipulation, assert) 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 every 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 AssertionError (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 last element unchanged. See the diagram below for examples. Case 1- even number of elements Case 2- odd number of elements initial list 2 3 initial list 6 7 8 9 10 'W' 'o' O 1 1 2 3 4 5 4 5 'b' 'c' 'd' 'H' 'e' '1' 'l' 'o' '1' 'd' O 1 2 3 4 5 'b' 'a' 'd' 'c' 'f' 'e O 1 2 3 4 5 6 7 8 9 10 'e' 'H' 'l' 'l' ' ' 'o' 'W' '1' 'r' 'd' after adjacency element swap after adjacency element swap Last element is unchanged. Require function In addition to the task2() function, you need to implement the following function: swapAdjacentElements(params: 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. ASSERT: This function should assert with the following error if the list has less than two (2) characters: "Must enter two or more 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 input 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 swapAdjacentElements() 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.
Task 2- Adjacent element swap for a list (Topics: list, loops, functions, string manipulation, assert) 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 every 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 AssertionError (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 last element unchanged. See the diagram below for examples. Case 1- even number of elements Case 2- odd number of elements initial list 2 3 initial list 6 7 8 9 10 'W' 'o' O 1 1 2 3 4 5 4 5 'b' 'c' 'd' 'H' 'e' '1' 'l' 'o' '1' 'd' O 1 2 3 4 5 'b' 'a' 'd' 'c' 'f' 'e O 1 2 3 4 5 6 7 8 9 10 'e' 'H' 'l' 'l' ' ' 'o' 'W' '1' 'r' 'd' after adjacency element swap after adjacency element swap Last element is unchanged. Require function In addition to the task2() function, you need to implement the following function: swapAdjacentElements(params: 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. ASSERT: This function should assert with the following error if the list has less than two (2) characters: "Must enter two or more 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 input 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 swapAdjacentElements() 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.
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 2PE
Related questions
Question
In Python please
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps with 7 images
Knowledge Booster
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.Recommended textbooks for you
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT