logo CodeStepByStep logo

collapse_pairs

Language/Type: Python lists traversals

Write a function named collapse_pairs that accepts a list of integers as a parameter and modifies the list so that each of its pairs of neighboring integers (such as the pair at indexes 0-1, and the pair at indexes 2-3, etc.) are combined into a single sum of that pair. The sum will be stored at the even index (0,2,4, etc.) if the sum is even and at the odd index (1,3,5, etc.) if the sum is odd. The other index of the pair will change to 0.

For example, if a list named a stores the values [7, 2, 8, 9, 4, 22, 7, 1, 9, 10], then the call of collapse_pairs(a) should modify the list to contain the values [0, 9, 0, 17, 26, 0, 8, 0, 0, 19]. The first pair from the original list is collapsed into 9 (7 + 2), which is stored at the odd index 1 because 9 is odd. The second pair is collapsed into 17 (8 + 9), stored at the odd index 3 the third pair is collapsed into 26 (4 + 22), stored at the even index 4 and so on. The figure below summarizes the process for this example list:

before: index  0   1   2   3   4   5   6   7   8   9
        value [7,  2,  8,  9,  4, 22,  7,  1,  9, 10]
               \   /   \   /   \   /   \   /   \   /
                \ /     \ /     \ /     \ /     \ /
                 V       V       V       V       V

after:  index  0   1   2   3   4   5   6   7   8   9
        value [0,  9,  0, 17, 26,  0,  8,  0,  0, 19]
Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.