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.
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the product of two prime numbers ? and ? and returns the tuple ( ?, ? ) where ?≤? . Example: ????????(22)=(2,11) Example: ????????(3605282209)=(59447,60647) This problem has a time-out limit of 1 second and a memory limit of 1MB. The number ? in all test-cases will satisfy 4≤?≤800000000000000 For example: Test Result print(factorMe(22)) (2, 11) print(factorMe(3605282209)) (59447, 60647)
(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 Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT