← Back to challenges

Swapping Two by Two

PythonHardregexstrings

Instructions

Write a function that swaps the first pair (1st and 2nd characters) with the second pair (3rd and 4th characters) for every quadruplet substring.

Examples

swap_two("ABCDEFGH") ➞ "CDABGHEF"

swap_two("AABBCCDDEEFF") ➞ "BBAADDCCFFEE"

swap_two("munchkins") ➞ "ncmuinhks"

swap_two("FFGGHHI") ➞ "GGFFHHI"

Notes

Keep leftover strings in the same order.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.