Question

In: Computer Science

(Programming Language: Python) Complete the function remove number such that given a list of integers and...

(Programming Language: Python)

Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of
n from the list. Remember that this function needs to modify the list, not return a new list.

# DO NOT ADD ANY OTHER IMPORTS from typing import List

def remove_number(lst: List[int], number: int) -> None:

"""

Remove every instance of number in lst. Do this *in-place*, i.e. *modify* the list. Do NOT return a new list.

>>> lst = [1, 2, 3]

>>> remove_number(lst, 3)

>>> lst

[1, 2]

"""

pass

Solutions

Expert Solution

Please find the code below with the comments at every line of the code and also screenshot of the execution.

#this program will return the list after removing the element that has been passed to remove_number(lst, element)

#defining the function remove_number
def remove_number(lst, element):
  #iterating through the loop to remove all the occurences of element in the passed list
  for iter in range(len(lst) - 1, -1, -1):
    #checking the condition that if it matches the given given element
    if lst[iter] == element:
      #deleting the element from the list
        del lst[iter]
  #returning the same list(not the new list)
  return lst

#initialinzing the list
lst = [1,2,3,3,3,4,4,2,5,3,2,1,0]

#calling the remove_number method to remove 3 from the list
remove_number(lst,3)

#priniting the list lst
print(lst)

Screenshot of the execution:

Another way of execution :

you can execute by commenting return also to check whether its removing in the same list or new list:
please find the screenshot below for the same after commenting return from line 12


Related Solutions

Task 1: Remove Number Complete the function remove number such that given a list of integers...
Task 1: Remove Number Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. Task 2: Logged List The log2() function is one of an algorithm designer’s favourite functions. You’ll learn more about this later, but briefly – if your input size is 1048576 elements, but you only look at...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
Programming language Python It have to be in Functions with a main function Samuel is a...
Programming language Python It have to be in Functions with a main function Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits. At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
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.
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): A website requests an user to input his account password. Write a program to examize the validity of the password. The valid password must consists of: At least 1 letter in [a-z] At least 1 number in [0-9] At least a letter in [A-Z] (capital) At least one special letter in [$#@] At least...
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers)
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT