Created
September 23, 2018 14:33
-
-
Save 9terz/efa8d2b13840a7dfba07f5228af144e1 to your computer and use it in GitHub Desktop.
One time swap
This file contains 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 isAbleToOneTimeSwapSort(arr): | |
print(arr) | |
sortArray = sorted(arr) | |
count = 0 | |
for i in range(0, len(sortArray)): | |
sortedOne = sortArray[i] | |
oldOne = arr[i] | |
if sortedOne != oldOne: | |
count += 1 | |
if count <= 2: | |
return True | |
else: | |
return False | |
print(isAbleToOneTimeSwapSort([1, 2, 4, 5, 3])) | |
print(isAbleToOneTimeSwapSort([1, 3, 3, 4, 5])) | |
print(isAbleToOneTimeSwapSort([1, 3, 2, 5, 4])) | |
print(isAbleToOneTimeSwapSort([1, 2, 3, 4, 5])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment