In: Computer Science
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following.
Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less than the second parameter’s index.
Define a function named rinsort. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall sort the elements of the list from position 0 to the position corresponding to the second parameter, in ascending order using insertion sort. This function must be recursive.
It is highly recommended that you thoroughly test your module. This will mean writing code that exercises your module’s contents by setting up a suitable list or lists of parameters, importing your p5.py file and then looping through the test cases. You do not need to submit your test code. You should treat a negative second parameter in either rinsert or rinsort as an error, and a second parameter past the end of the list as an error.
def rinsert(nums, index): if (index == 0): return item = nums[index] index -= 1 while index >= 0 and nums[index] > item: nums[index+1] = nums[index] index -= 1 nums[index + 1] = item # Note start is an optional parameter to make the function do the # sorting recursively def rinsort(nums, end, start = 0): # base case if end < start: return nums rinsert(nums, start) rinsort(nums, end, start + 1) return nums print(rinsort([12, 2, 9, 6, 8], 4))
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.