Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import traceback def merge_sort(A): print(TODO: Implement this function) #Email class #Each email has two instance variables: # self.sender is a string representing the name of

import traceback
def merge_sort(A):
print("TODO: Implement this function")
#Email class
#Each email has two instance variables:
# self.sender is a string representing the name of the person
# or organization that sent the email
# self.subject is a stirng representing the subject line of
# the Email
#You can create new methods, but don't change the existing ones.
class Email:
def __init__(self,sender,subject):
self.sender = sender
self.subject = subject
def __repr__(self):
return " {:20}:{}".format(self.sender,self.subject)
if __name__ == '__main__':
#Setup test cases
e1 = Email("Inigo Montoya","You killed my father. Prepare to die.")
e2 = Email("IRS", "THIS IS YOUR FINAL WARNING")
e3 = Email("Your Grandson", "Need Money for College")
e4 = Email("Prince of Nigeria", "Help transferring funds")
e5 = Email("Jackpot Lottery", "YOU'RE WINNER!")
e6 = Email("Super Antivirus Pro", "Virus Detected! Download Now!")
e7 = Email("Anonymous", "Forward this to 20 friends and you will get $$$")
e8 = Email("The Bank", "Please confirm your password")
e9 = Email("Ted", "HAHAHAHAHAHAHAHAHAHA")
e10 = Email("Tedd","mine!")
e11 = Email("Teddd","be")
e12 = Email("Tedddd","will")
e13 = Email("Teddddd","Vengeance")
e14 = Email("Tedddddd","day.")
e15 = Email("Teddddddd","this")
e16 = Email("Tedddddddd","for")
e17 = Email("Teddddddddd","waited")
e18 = Email("Tedddddddddd","I")
e19 = Email("Teddddddddddd","have")
e20 = Email("Tedddddddddddd","Long")
inbox1 = []
inbox2 = [e13]
inbox3 = [e2,e6]
inbox4 = [e9,e10,e11,e12,e13,e14,e15,e16,e17,e18,e19,e20]
inbox5 = [e20,e19,e18,e17,e16,e15,e14,e13,e12,e11,e10,e9]
inbox6 = [e1,e2,e3,e4,e5,e6,e7,e8]
tests = [inbox1,inbox2,inbox3,inbox4,inbox5,inbox6]
correct = [[],
[e13],
[e6,e2],
[e20,e19,e18,e17,e16,e15,e14,e13,e12,e11,e10,e9],
[e20,e19,e18,e17,e16,e15,e14,e13,e12,e11,e10,e9],
[e6,e4,e5,e1,e3,e7,e8,e2]]
#Run test cases, check whether sorted list correct
count = 0
try:
for i in range(len(tests)):
print("TEST #"+str(i+1))
print("Running: merge_sort(",tests[i],") ")
merge_sort(tests[i])
print("Expected:",correct[i]," Got:",tests[i])
assert correct[i] == tests[i],"List sorted incorrectly"
count=count+1
print(" --------------------------------------- ")
except AssertionError as e:
print(" FAIL: ",e)
except Exception:
print(" FAIL: ",traceback.format_exc())
print(count,"out of",len(tests),"tests passed.")
image text in transcribed
image text in transcribed
You are placed in charge of the email servers as part of your unpaid internship at Holistic Synergies, Ltd. Your supervisor, Dr. Boss Manager III, has compromised company security by leaking data to hackers over email on multiple occasions. However, he has a solution. After watching a certain sci-fi movie series, Dr. Manager has come to the conclusion that hackers always use short, trendy names like "Neo" or "Cypher". Therefore, rather than displaying emails in order from newest to oldest, he orders you to reprogram the server to display emails from longest sender name to shortest, so that all of the emails from people with short hacker names are pushed to the bottom. Speed is of the utmost importance to Holistic Synergies, Ltd., so you will be implementing this email sorting routine in Merge Sort to guarantee a (nlgn) algorithm. In particular, your boss has heard that recursion tends to be slow in Python, so you need to implement Merge Sort iteratively (no recursive calls). You will likely need at least three nested loops to accomplish this: - One to keep track of how large the sublists you're currently merging are (start at 2, then 4 , then 8 , and so on) - One to cycle through all of the sublists of that size (for example, if you're currently looking at all sublists of size 4 in list A, then you need to look at A[0:4],A[4:8],A[8:12], and so on). - One (or possibly more, depending on how you implement it) to actually do the merge process for the left and right halves of that sublist. Instructions: Download the template pa03.py from the class website. The template includes a Email class, which consists of two instance variables: sender, which is a string representing the name of the person or entity who sent the email, and subject, which is a string representing the subject line of the email. The file also includes some test cases representing sample inboxes for Holistic Synergies, Ltd. employees. You'll need to implement the Merge Sort algorithm which operates on a list of Email objects, and sort them in non-increasing order by the length of the sender string (that is, longest sender name to shortest). This algorithm should alter the list passed in, rather than return a new list. Remember, Merge Sort should be a stable sorting algorithm - that means that if Email A and Email B have the same sender length and Email A appears before Email B in the input list, Email A should appear before Email B in the output list. You might want to consider adding methods that overwrite one or more of the comparison operators for the Email class - this would allow you to use operators such as : def _gt__(self, other) = : def __ge_(self, other) ==: def ____(self, other) 1=: def _____(self, other) While you're not allowed to use recursion, you are permitted to write helper functions - this may be useful in breaking up and testing different parts of the algorithm. I would recommend implementing the recursive version of merge sort, based on the textbook pseudocode, as a starting point. Then figure out how to write loops to replace the recursion

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

3. Identify cultural universals in nonverbal communication.

Answered: 1 week ago