Question

In: Computer Science

Write a Python module that must satisfy the following- Define a function named rinsert. This function...

Write a Python module that 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.

Solutions

Expert Solution

Answer:

a.) def rinsert(item_list,val):

    item_list.sort()
    # Searching for the position
    for i in range(len(item_list)):
        if item_list[i] > val:
            index = i
            break

    # Inserting n in the list
    item_list = item_list[:i] + [val] + item_list[i:]
    return item_list


item_list=[10,1,23,45,12,3,5,8,24,77,16]
val=int(input("Enter an integer value (0 to len(item_list)-1): "))

if val not in range(0,len(item_list)-1):
    print("Please enter a value within the specified range")
    val = int(input("Enter an integer value (0 to len(item_list)-1: "))


print("Sorted List after insertion: ",rinsert(item_list,val))

Output:

b.) def rinsort(item_list,val):
    # base case
    if val <= 1:
        return

    # Sort first n-1 elements
    rinsort(item_list, val - 1)
    #Insert last element at its correct position in sorted array
    last = item_list[val - 1]
    j = val - 2

    # Move elements of arr[0..i-1], that are
    # greater than key, to one position ahead
    # of their current position
    while (j >= 0 and item_list[j] > last):
        item_list[j + 1] = item_list[j]
        j = j - 1

    item_list[j + 1] = last

    return item_list


item_list=[10,1,23,45,12,3,5,8,24,77,16]


print("List before insertion sort: ",item_list)
print("List after insertion sort: ",rinsort(item_list,len(item_list)))

Output:


Related Solutions

Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one input, a String named amountStr which consists of a dollar-formatted amount, such as "$ 1,256.86". The returned value is an int representing the number of rounded "dollars" in the amount, (1257 in the sample shown here). You will need to scrub, format and parse the input, then use arithmetic to determine how many rounded dollars the amount contains. roundDollars("$ 1,256.86") → 1257 roundDollars("$ 0.42")...
Write code to define a function named mymath. The function has three arguments in the following...
Write code to define a function named mymath. The function has three arguments in the following order: Boolean, Integer, and Integer. It returns an Integer. The function will return a value as follows: 1. If the Boolean variable is True, the function returns the sum of the two integers. 2. If the Boolean is False, then the function returns the value of the first integer - the value of the second Integer.
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
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...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Define a Python function named matches that has two parameters. Both parameters will be lists of...
Define a Python function named matches that has two parameters. Both parameters will be lists of ints. Both lists will have the same length. Your function should use the accumulator pattern to return a newly created list. For each index, check if the lists' entries at that index are equivalent. If the entries are equivalent, append the literal True to your accumulator. Otherwise, append the literal False to your accumulator. Hint: Since you must use the same index with each...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is a palindrome, and boolean False if it is not. The palindromes for this problem are contained in the list below. You must use this list in your solution. Use a for loop to retrieve each palindrome and test it. Your script should test each string in the list of palindromes below. Be aware there's a second list of palindromes embedded in the palindromes list....
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
Write a Python program containing a function named scrabble_sort that will sort a list of strings...
Write a Python program containing a function named scrabble_sort that will sort a list of strings according to the length of the string, so that shortest strings appear at the front of the list. Words that have the same number of letters should be arranged in alphabetical order. Write your own logic for the sort function (you may want to start from some of the existing sorting code we studied). Do NOT use the built-in sort function provided by Python....
Create a Python Module named piphash_rainbow.py that Creates a rainbow table named sixdigithash_rainbow.csv that stores all...
Create a Python Module named piphash_rainbow.py that Creates a rainbow table named sixdigithash_rainbow.csv that stores all possible six-digit pins and their corresponding md5, sha1, sha256, and sha512 hashes.
IN PYTHON 3 LANGUAGE Define a recursive function named immutify; it is passed any data structure...
IN PYTHON 3 LANGUAGE Define a recursive function named immutify; it is passed any data structure that contains int, str, tuple, list, set, frozenset, and dict values (including nested versions of any of these data structures as an argument). It returns an immutable equivalent data structure (one that could be used for values in a set or keys in a dict). The types int, str, and frozenset are already immutable. Convert a set to a frozenset; convert all the values...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT