Solutions for Arrange files in ascending order

    • @JavierVow
    • @jacob, submitted in Python 2, achived 100% score
    # Complete the minimumSwaps function below.
    def solve(arr):
        ref_arr = sorted(arr)
        index_dict = {v: i for i,v in enumerate(arr)}
        swaps = 0
    
        for i,v in enumerate(arr):
            correct_value = ref_arr[i]
            if v != correct_value:
                to_swap_ix = index_dict[correct_value]
                arr[to_swap_ix],arr[i] = arr[i], arr[to_swap_ix]
                index_dict[v] = to_swap_ix
                index_dict[correct_value] = i
                swaps += 1
    
        return swaps
    
    
    • @JavierVow
    • @jacob, submitted in Python 3, achived 100% score
    # Complete the minimumSwaps function below.
    def solve(arr):
        ref_arr = sorted(arr)
        index_dict = {v: i for i,v in enumerate(arr)}
        swaps = 0
    
        for i,v in enumerate(arr):
            correct_value = ref_arr[i]
            if v != correct_value:
                to_swap_ix = index_dict[correct_value]
                arr[to_swap_ix],arr[i] = arr[i], arr[to_swap_ix]
                index_dict[v] = to_swap_ix
                index_dict[correct_value] = i
                swaps += 1
    
        return swaps