Knowee
Questions
Features
Study Tools

Traceback (most recent call last): File "./main.py", line 43, in <module> print(mergeSort(arr,n)) File "./main.py", line 3, in mergeSort return _mergeSort(arr, temp_arr, 0, n-1)NameError: name '_mergeSort' is not defined

Question

Traceback (most recent call last): File "./main.py", line 43, in <module> print(mergeSort(arr,n)) File "./main.py", line 3, in mergeSort return _mergeSort(arr, temp_arr, 0, n-1)NameError: name '_mergeSort' is not defined

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

The error message you're seeing is because the function _mergeSort is not defined in your code. This function is being called in your mergeSort function, but Python doesn't know what _mergeSort is supposed to do because it hasn't been defined anywhere.

Here's a step-by-step guide on how to fix this:

  1. Define the _mergeSort function: You need to write the definition for _mergeSort. This function should take the same parameters as mergeSort (i.e., arr, temp_arr, left, and right).

  2. Implement the merge sort algorithm in _mergeSort: Merge sort is a divide-and-conquer algorithm. It works by dividing the array into two halves, sorting them separately, and then merging them. This process is done recursively.

  3. Call _mergeSort in mergeSort: You're already doing this, but I'm including it for completeness. In your mergeSort function, you should call _mergeSort, passing arr, a temporary array, 0, and n-1 as the parameters.

  4. Handle the base case: In _mergeSort, you should include a base case for when left is greater than or equal to right. In this case, the function should simply return.

  5. Sort and merge: If left is less than right, you should calculate the middle point, recursively sort the two halves of the array, and then merge them.

Remember, the key to implementing merge sort is understanding that it's a recursive algorithm. You divide the problem into smaller subproblems (i.e., sorting the two halves of the array), solve those subproblems, and then combine their solutions to solve the original problem (i.e., sorting the entire array).

This problem has been solved

Solution 2

The error message you're seeing is because the function _mergeSort is not defined in your code. This function is being called in your mergeSort function, but Python doesn't know what _mergeSort is supposed to do because it hasn't been defined anywhere.

Here's a simple example of how you might define _mergeSort:

def _mergeSort(arr, temp_arr, left, right):
    # This is where you'd put the logic for your merge sort
    pass

You'll need to replace the pass statement with the actual logic for your merge sort. If you're not sure how to implement merge sort, there are many resources online that can help you understand the algorithm.

This problem has been solved

Similar Questions

Traceback (most recent call last): File "main.py", line 41, in <module> mergeSort(arr, 0, N - 1) File "main.py", line 33, in mergeSort mergeSort(arr, l, m) File "main.py", line 33, in mergeSort mergeSort(arr, l, m) File "main.py", line 35, in mergeSort merge(arr, l, m, r) File "main.py", line 12, in merge if L[i] <= R[j]:NameError: name 'R' is not defined

Input65 1 3 15 10 4OutputTraceback (most recent call last): File "main.py", line 41, in <module> mergeSort(arr, 0, N - 1) File "main.py", line 33, in mergeSort mergeSort(arr, l, m) File "main.py", line 33, in mergeSort mergeSort(arr, l, m) File "main.py", line 35, in mergeSort merge(arr, l, m, r)NameError: name 'merge' is not defined

InstructionsYou will need to create three files:Apartment.py - file containing a class definition for an Apartment objectlab06.py - file containing mergesort and other functions defined in the lab06.py section of this lab.testFile.py - file containing pytest functions testing the overall correctness of your class definitionsThere will be no starter code for this assignment, but rather the class descriptions and required methods are defined in the specification below.You should organize your lab work in its own directory. This way all files for a lab are located in a single folder. Also, this will be easy to import various files into your code using the import / from technique shown in lecture.Apartment.py classThe Apartment.py file will contain the definition of an Apartment. We will define the Apartment attributes as follows:rent - integer that represents the rent of the ApartmentmetersFromUCSB - integer that represents the Apartment’s distance, in meters, from UCSBcondition - string that represents the condition of the Apartment. This string will be one of three options: "bad", "average", or "excellent".You should write a constructor that allows the user to construct an apartment object by passing in values for all of the fields. You may assume that all attributes of the Apartment object will be defined. Therefore, there should be no default values for rent, metersFromUCSB, or condition.__init__(self, rent, metersFromUCSB, condition)In addition to your constructor, your class definition should also support “getter” methods that can receive the state of the Apartment object:getRent(self)getMetersFromUCSB(self)getCondition(self)You will also implement the methodgetApartmentDetails(self)that returns a str with all of the Apartment attributes. The string should contain all attributes in the following EXACT format (Note: There is no \n character at the end of this string):a0 = Apartment(1204, 200, "bad")print(a0.getApartmentDetails())Output(Apartment) Rent: $1204, Distance From UCSB: 200m, Condition: badLastly, your Apartment class should overload the >,<, and == operators. This will be used when finding the proper position of an Apartment in the list using the specifications in the Introduction section of this lab. In this context for example, the < operator will return True for Apartment1 < Apartment2 if Apartment1 is better than Apartment2. We reviewed operator overloading in class and the textbook does discuss overloading Python operators. You can also refer to this reference on overloading various operators as well:

Explain the significance of merge sort

What is the base case in the Merge Sort algorithm when it is solved recursively?

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.