Question

In: Computer Science

PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...

PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.  
Hint: Use a whlie loop and list methods

lst = [1,3,5,7]

insertInOrder(lst, 2)
print(lst)
# Should print [1, 2, 3, 5, 7]

insertInOrder(lst, 4)
print(lst)
# Should print [1, 2, 3, 4, 5, 7]

insertOnOrder(lst, 8)
print(lst)
# Should print [1, 2, 3, 4, 5, 7, 8]

insertInOrder(lst, 0)
print(lst)
# Should print [0, 1, 2, 3, 4, 5, 7, 8]

Solutions

Expert Solution

Python code:

#defining insertInOrder function
def insertInOrder(lst,number):
    #initializing i
    i=0
    #looping all elements in lst
    while(i<len(lst)):
        #checking if the number is less than the current number in lst
        if(number<lst[i]):
            #inserting the number in the current position
            lst.insert(i,number)
            #exiting the loop
            break
        #incrementing i
        i+=1
    #checking if the entire loop is iterated
    if(i==len(lst)):
        #then add the number in the last position
        lst.append(number)
#initializing lst
lst=[1,3,5,7]
#calling insertInOrder and insrting 2
insertInOrder(lst,2)
#printing lst
print(lst)
#calling insertInOrder and insrting 4
insertInOrder(lst,4)
#printing lst
print(lst)
#calling insertInOrder and insrting 8
insertInOrder(lst,8)
#printing lst
print(lst)
#calling insertInOrder and insrting 0
insertInOrder(lst,0)
#printing lst
print(lst)

Screenshot:


Output:


Related Solutions

Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
All functions are written in python Write a function cube_evens_lc(values) that takes as input a list...
All functions are written in python Write a function cube_evens_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the even numbers in values (i.e., the even numbers raised to the third power). For example: >>> cube_evens_lc([2, 5, 6, 4, 1]) result: [8, 216, 64] This version of the function may not use recursion. Write a function cube_evens_rec(values) that takes as input a...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT