You are viewing a single comment's thread. Return to all comments →
Simple Python solution:
def almostSorted(arr): # Write your code here sorted_arr = sorted(arr) if arr == sorted_arr: print("yes") return wrong = [] for i, (num, sorted_num) in enumerate(zip(arr, sorted_arr)): if num == sorted_num: continue wrong.append(i+1) if len(wrong) == 2: print("yes") print(f"swap {wrong[0]} {wrong[1]}") return if arr[wrong[0]-1:wrong[-1]] == sorted_arr[wrong[0]-1:wrong[-1]][::-1]: print("yes") print(f"reverse {wrong[0]} {wrong[-1]}") return print("no") return
Seems like cookies are disabled on this browser, please enable them to open this website
Almost Sorted
You are viewing a single comment's thread. Return to all comments →
Simple Python solution: