We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defmakeSwaps(arr):# sort list of values while keeping their original indexbeautiful_arr=(sorted(list(enumerate(arr)),key=lambdax:x[1]))num_swaps=0foridxinrange(len(arr)):whileTrue:# while the original won't correspond to the "correct" by order index, swapifbeautiful_arr[idx][0]==idx:breakelse:num_swaps+=1swapped_idx=beautiful_arr[idx][0]beautiful_arr[idx],beautiful_arr[swapped_idx]=beautiful_arr[swapped_idx],beautiful_arr[idx]returnnum_swapsdeflilysHomework(arr):returnmin(makeSwaps(arr),makeSwaps(arr[::-1]))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →