Question

Question2: The following function is supposed to receive a list of numbers and a number as...

Question2: The following function is supposed to receive a list of numbers and a number as the input parameters. The function finds all occurrences of number inside the list and removes them from the list and eventually returns the results. Do you think this function will work as expected? If yes, please mention and if no, please fix it.

def function(listOfNumbers, number): for i in range(len(listOfNumbers)): if listOfNumbers[i]==number:

listOfNumbers.remove(number) return listOfNumbers

newList = function([3,4,1,3,4,7,8,1,2,3,4,6,8], 3)
print("After removing the occurrences of the number, the result is %g" %( newList))

Python code

Homework Answers

Answer #1
# Given code does not word
# Below is the fix for the code

######################

def function(listOfNumbers, number):
    while number in listOfNumbers:
        listOfNumbers.remove(number)
    return listOfNumbers

newList = function([3,4,1,3,4,7,8,1,2,3,4,6,8], 3)
print("After removing the occurrences of the number, the result is", newList)

After removing the occurrences of the number, the result is [4, 1, 4, 7, 8, 1, 2, 4, 6, 8]

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
python 3 For this exercise you are to implement the function poly_iter in the array-backed list...
python 3 For this exercise you are to implement the function poly_iter in the array-backed list class, which, when called with positive integer parameters a, b, c, returns an iterator over the values in the underlying list at indexes a*i2+b*i+c, for i=0, 1, 2, ... E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98, 99], the following code: for x in lst.poly_iter(2, 3, 4): print(x) will produce the output: 4 9 18 31...
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order....
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order. But some code is missing as indicated by '?'. def sort_in_place(list_num): for i in range(?): for j in range(?): if ?: temp = list_num[j] list_num[j] = list_num[i] list_num[i] = temp my_list = [23,1,45,20,13,-34] sort_in_place(my_list) print(my_list) Modify the three lines of code in the program below so that the output is [-34, 1, 13, 20, 23, 45]
Python The function cube should receive a list as a parameter and return a new list...
Python The function cube should receive a list as a parameter and return a new list with each number taken in cubic (raised to three). Write the necessary code for the function, as well as an example which uses the function to calculate and print the cubes for the numbers 5, 8 and 9
convert code from python to cpp L = [2,7,4,19,45,16,9,13,8,69,55,11,23,98,14,5,1,3]   #Merging function def merge(M,N): merging_list = []...
convert code from python to cpp L = [2,7,4,19,45,16,9,13,8,69,55,11,23,98,14,5,1,3]   #Merging function def merge(M,N): merging_list = []        //create empty list to merge the lists M and N if not M:                //if M is empty list, m = 0                //set m = 0 else: m = len(M)             //otherwise, set m = len(M) if not N:                //if N is empty list, n = 0       ...
I am working on exercise 5.30 from Introduction to Computing using python (Author: Perkovic). I was...
I am working on exercise 5.30 from Introduction to Computing using python (Author: Perkovic). I was looking at the solution and was able to understand what to do. However, when I implement the temp function as indicated, I keep getting this error "ValueError: the first two maketrans arguments must have equal length". However, it seems my two arguments are equal length, so I'm not sure what I am doing wrong! print('Exercise 5.30') def many(file): infile = open(file) content = infile.read()...
The following code implements this algorithm to sort a list of numbers in ascending order. But...
The following code implements this algorithm to sort a list of numbers in ascending order. But some code is missing as indicated by '?'. def sort_in_place(list_num):     for i in range(?):         for j in range(?):             if ?:                 temp = list_num[j]                 list_num[j] = list_num[i]                 list_num[i] = temp my_list = [23,1,45,20,13,-34] sort_in_place(my_list) print(my_list)
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
Write a program to input 6 numbers. After each number is input, print the biggest of...
Write a program to input 6 numbers. After each number is input, print the biggest of the numbers entered so far. Please use python and include a screenshot that the code is running. Please use simple code as I am only a high school student.
Hello I need a flowchart made for reference I am using flowgorithm Here is the python...
Hello I need a flowchart made for reference I am using flowgorithm Here is the python code that I need a flowchart for def get_user_input_validated(): """ Module Name: get_user_input_validated Parameters: None Description: Defence program validates input as rock, paper or scissors returns input in lowercase making input case insensitive """ choice=["rock","paper","scissors"] while True: user_choice = input("Please enter your choice (rock/paper/scissors):") if user_choice.lower() in choice: break else: print("Sorry - that selection is not valid.",end="") return user_choice.lower()
How do I fix my code to collect ONLY hashtag instead of accumulating all the information...
How do I fix my code to collect ONLY hashtag instead of accumulating all the information about the number of occurrences of all words from the textfile? Please help to fix below codes. tweetcount=0 maxcount=0 count = 0 with open('elon-musk.txt') as book: for tweet in book: count += 1 print("Number of tweets:", count) print() with open('elon-musk.txt') as book: for line in book: s count = len(line.split()) if count>maxcount: maxline = line maxcount = count tweetcount += 1 print("Tweet with max...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT