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...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
(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.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT