Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Rewrite this code, instead of .sort() implement manual merge sorting N = int(input())…

  1. User

    Rewrite this code, instead of .sort() implement manual merge sorting N = int(input()) robots = [] for index in range(N): main_num, aux_num = map(int, input().split()) robots.append((main_num, index, aux_num))

    robots.sort()

    for robot in robots: print(f"{robot[0]} {robot[2]}")

  2. Assistant
    N = int(input())
    robots = []
    for index in range(N):
        main_num, aux_num = map(int, input().split())
        robots.append((main_num, index, aux_num))
    
    def merge_sort(lst):
        if len(lst) > 1:
            mid = len(lst) //2
            L = lst[:mid]
            R = lst[mid:]
    
            merge_sort(L)
            merge_sort(R)
    
            i = j = k =0
            while i < len(L) and j < len(R):
                if L[i] <= R[j]:
                    lst[k] = L[i]
                    i +=1
                else:
                    lst[k] = R[j]
                    j +=1
                k +=1
            while i < len(L):
                lst[k] = L[i]
                i +=1
                k +=1
            while j < len(R):
                lst[k] = R[j]
                j +=1
                k +=1
    
    merge_sort(robots)
    
    for robot in robots:
        print(f"{robot[0]} {robot[2]}")