Question

Search Exercise 11.A: Mergesort with a Comparator CS 1410 Background The sort algorithms we have looked at in Module 8 have a
alist = [4.3.7.1.9) mergesort (alist, lambda x.y: x > y) print(alist) # 19. 7. 4. 3. 11 In another python module, exiia.py, i
data (2) (1 page) Q Search data (2) 688997 Karina Gay 998 Vitae St. Atlanta GA 45160 1 1 45884.90 46.92 34 30417353-K 465794-

In Python!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have atttached the code. It has comments for explanation. If you have any queries, feel free to comment.

Program Screenhots for Indentation Reference:

import msort def comparator_by_name (x, y): k = x[1].split() # split to put in tuple 1 y[1].split() return (k[-1], k[0]) < (1#Mergesort: = def mergesort(arr, key=lambda x, y: x < y): if len(arr) >1: # Sort halves ito separate memory mid len (arr) //

Sample Output (On a part of file):

Program code to copy:

msort.py:

def mergesort(arr, key=lambda x, y: x < y):
    if len(arr) >1:
        # Sort halves ito separate memory
        mid = len(arr) // 2
        L = arr[:mid]
        R = arr[mid:]
        mergesort(L, key)
        mergesort(R, key)

        # Merge L and R back into arr
        i = j = k = 0

        ## put using the key
        while i < len(L) and j < len(R):
            if key(L[i], R[j]):
                # put ith item
                arr[k] = L[i]
                i+=1
            else:
                arr[k] = R[j]
                j+=1
            k+=1

# Checking if any elements were left
        while i < len(L):
            arr[k] = L[i]
            i+=1
            k+=1
        while j < len(R):
            arr[k] = R[j]
            j+=1
            k+=1

ex11a.py:

import msort


def comparator_by_name(x, y):
    k = x[1].split() # split to put in tuple
    l = y[1].split()
    return (k[-1], k[0]) < (l[-1], l[0])

## open the file for reading
lines = []
with open("data.csv", "r") as f:
    ### read all the lines
    for line in f:
        ## get a line and split by comma
        l = line.split(',')
        ## append to lines
        lines.append(l)
    ## sort using the id
  
# sort by id
with open("data_by_id.txt", "w") as f:
    sorted_data = lines.copy() ## make a copy
    msort.mergesort(sorted_data, lambda x, y: x[0] < y[0]) ## use id as the key
    for line in sorted_data:
        f.write(str(tuple(line)) + "\n") ## writes the lines

# sort by name
with open("data_by_name.txt", "w") as f:
    sorted_name = lines.copy() ## make a copy
    msort.mergesort(sorted_name, comparator_by_name) ## use tuple as the comparator
    for line in sorted_name:
        f.write(str(tuple(line)) + "\n") ## writes the lines

Add a comment
Know the answer?
Add Answer to:
In Python! Search Exercise 11.A: Mergesort with a Comparator CS 1410 Background The sort algorithms we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT