Reverse Pairs

Advanced

Reverse Pairs

Welcome to the magical world of reversing pairs! This is a fun task for you to practice looping through lists and flipping things around.

Imagine you have a long line of students, and each one has a partner. Now, for fun, you decide to switch the order of each pair.

The task is simple: Given a list of numbers, you need to switch every two consecutive numbers in place. If there's an odd number of elements, the last one stays where it is.

Let's see an example:

Example 1:

reverse_pairs([1, 2, 3, 4])
  • Output: [2, 1, 4, 3]

Example 2:

reverse_pairs([10, 9, 8])
  • Output: [9, 10, 8]

In the second example, the 10 and 9 are swapped, but since 8 has no partner, it stays in its place.

Your task is to implement the reverse_pairs function that takes a list of numbers as input and returns a new list with every pair reversed, while respecting any solo number that might appear.

Geek
Inputs

Arg 1:

[1,2,3,4]
Expected Output
[2, 1, 4, 3]
Actual Output