β back to unit
Challenge
# sorting advanced
One Swap Away?
The vending machine only lets you swap TWO items β is that enough to fix the whole row? π§
You get a list of nums. Print sorted already if it's in order, one swap if exactly one swap of two items can fix it, or needs more otherwise.
Example:
Input: nums = [1, 5, 3, 4, 2, 6]
Output: one swap
The first break is at position 1 (5 > 3) and the last break at position 4 (4 > 2). Swapping the values 5 and 2 gives [1, 2, 3, 4, 5, 6] β perfectly sorted with one swap.
π‘ need a hint?
sort-one-swap-away.pyπ given lines are locked β write your code in between
loading...