Last active
April 11, 2017 15:50
-
-
Save cmanny/7a14485aabb37df8d4dc0ac97562e712 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_swaps(nums): | |
swaps = 0 | |
nn = [x for x in nums] | |
l = len(nn) | |
for i, j in zip(range(l - 2, -1, -1), range(l - 1, 0, -1)) * l: | |
if nn[i] > nn[j]: | |
nn[j], nn[i] = nn[i], nn[j] | |
swaps += 1 | |
return swaps | |
print(count_swaps([1,2,3,4,5,6])) | |
print(count_swaps([1,5,3,4,6,2])) | |
print(count_swaps([6,5,4,3,2,1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment