Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, could I get assistance with another Python 3 question? Below I will provide what I am having difficulty with. I will provide the 3

Hello, could I get assistance with another Python 3 question? Below I will provide what I am having difficulty with.

image text in transcribed

image text in transcribed

I will provide the 3 different py codes

Starting with main.py:

class NaturalMergeSorter: def __init__(self): return

def get_sorted_run_length(self, integer_list, start_index): # Type your code here. return 0 # Modify this line.

def natural_merge_sort(self, integer_list): # Type your code here. return # Comment out this line.

def merge(self, numbers, left_first, left_last, right_last): merged_size = right_last - left_first + 1

merged_numbers = [None] * merged_size merge_pos = 0 left_pos = left_first right_pos = left_last + 1

# Add smallest element from left or right partition to merged numbers while left_pos

merge_pos += 1

# If left partition isn't empty, add remaining elements to merged_numbers while left_pos

# If right partition isn't empty, add remaining elements to merged_numbers while right_pos

# Copy merged numbers back to numbers for merge_pos in range(merged_size): numbers[left_first + merge_pos] = merged_numbers[merge_pos]

RunLengthTestCase.py:

from NaturalMergeSorter import NaturalMergeSorter

class RunLengthTestCase: def __init__(self, lst, start, expected_return): self.lst = lst self.start = start self.expected_return = expected_return

# Executes the test case. If the test case passes, a message that starts # with "PASS" is printed and true is returned. Otherwise a message that # starts with "FAIL" is printed and false is returned. def execute(self, test_feedback): user_sorter = NaturalMergeSorter()

# Call the get_sorted_run_length() function with the test case parameters user_ret_val = user_sorter.get_sorted_run_length(self.lst, self.start)

# The test passed only if the actual return value equals the # expected return value passed = user_ret_val == self.expected_return

if user_ret_val == self.expected_return: print(f"""PASS: get_sorted_run_length() List: {self.lst}""", file=test_feedback) print(f""" Start index: {self.start}""", file=test_feedback) print(f""" Expected return value: {self.expected_return}""", file=test_feedback) print(f""" Actual return value: {user_ret_val}""", file=test_feedback) return True else: print(f"""FAIL: get_sorted_run_length() List: {self.lst}""", file=test_feedback) print(f""" Start index: {self.start}""", file=test_feedback) print(f""" Expected return value: {self.expected_return}""", file=test_feedback) print(f""" Actual return value: {user_ret_val}""", file=test_feedback) return False

And lastly the NaturalMergeSorter.py:

class NaturalMergeSorter: def __init__(self): return

def get_sorted_run_length(self, integer_list, start_index): # Type your code here. return 0 # Modify this line.

def natural_merge_sort(self, integer_list): # Type your code here. return # Comment out this line.

def merge(self, numbers, left_first, left_last, right_last): merged_size = right_last - left_first + 1

merged_numbers = [None] * merged_size merge_pos = 0 left_pos = left_first right_pos = left_last + 1

# Add smallest element from left or right partition to merged numbers while left_pos

merge_pos += 1

# If left partition isn't empty, add remaining elements to merged_numbers while left_pos

# If right partition isn't empty, add remaining elements to merged_numbers while right_pos

# Copy merged numbers back to numbers for merge_pos in range(merged_size): numbers[left_first + merge_pos] = merged_numbers[merge_pos]

The merge sort algorithm recursively divides the list in half until a list with one element is reached. A variant of merge sort, called natural merge sort, instead finds already-sorted runs of elements and merges the runs together. Ex: The unsorted list below has five sorted runs. Natural merge sort starts at index 0 and finds sorted runs A and B. Runs A and B are merged, using the same merging algorithm as merge sort, to make run F. Next, the algorithm starts after the merged part F, again looking for two sequential, sorted runs. Runs C and D are found and merged to make run G. The algorithm then starts after the merged portion G. Only one run exists, run E, until the end of the list is reached. So the algorithm starts back at index 0 , finds runs F and G, and merges to make run H. Again a single run is found after the just-merged part, so the search starts back at index 0. Runs H and E are found and merged. One last search for a sorted run occurs, finding a sorted run length equal to the list's length. So the list is sorted and the algorithm terminates. Step 1: Implement the get_sorted_run_length() method Implement the get_sorted_run_length0 method in NaturalMergeSorter.py. Access NaturalMergeSorter.py by clicking on the orange arrow next to main.py at the top of the coding window. get_sorted_run_length() has two parameters: - integer_list: a list of integers and - start_index: an integer for the run's starting index. The method returns the number of list elements sorted in ascending order, starting at start_index and ending either at the end of the sorted run, or the end of the list, whichever comes first. The method returns 0 if start_index is out of bounds. File main.py has several test cases for get_sorted_run_length0 that can be run by clicking the "Run program" button. One test case also exists for natural_merge_sort(0, but that can be ignored until step two is completed. The program's output does not affect grading. Submit for grading to ensure that the get_sorted_run_length() unit tests pass before proceeding. Step 2: Implement the natural_merge_sort() method Implement the natural_merge_sort() method in NaturalMergeSorter.py. natural_merge_sort() must: 1. Start at index i=0 2. Get the length of the first sorted run, starting at i - Return if the first run's length equals the list's length - If the first run ends at the list's end, reassign i=0 and repeat step 2 3. Get the length of the second sorted run, starting immediately after the first 4. Merge the two runs with the provided merge() method 5. Reassign i with the first index after the second run, or 0 if the second run ends at the list's end 6. Go to step 2 \begin{tabular}{l|l} LAB & 3.16.1: LAB: Natural merge sort \end{tabular} 0/10 Downloadable files , and Current file: NaturalMergeSorter.py Load default template... \( \begin{aligned} 1 & \text { class NaturalMergeSorter: } \\ 2 & \text { def _init_(self): } \\ 3 & \text { return } \\ 4 & \\ 5 & \text { def get_sorted_run_length(self, integer_list, start_index): } \\ 6 & \text { \# Type your code here. } \\ 7 & \text { return } \theta \quad \text { \# Modify this line. } \\ 8 & \text { def natural_merge_sort(self, integer_list): } \\ 9 & \text { \# Type your code here. } \\ 10 & \text { return \# Comment out this line. } \\ 11 & \\ 12 & \text { def merge(self, numbers, left_first, left_last, right_last): } \\ 13 & \text { merged_size = right_last = left_first+1 } \\ 14 & \text { merged_numbers = [None] * merged_size } \\ 15 & \text { merge_pos = } 0 \\ 16 & \\ 17 & \end{aligned} \)

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

Step: 3

blur-text-image

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

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

Students also viewed these Databases questions

Question

Why is job analysis considered to be a basic HR tool?

Answered: 1 week ago

Question

5.1 Define recruitment and describe the recruitment process.

Answered: 1 week ago