Question

In: Computer Science

In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array...

In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns the count of “valid” (i.e. non-removed) data elements left. This function should do the removal “by hand” and SHOULD NOT use the remove method. Hint: Consider what how you update your current position in an array when you don’t remove an element versus when you do remove an element. Python

Solutions

Expert Solution

def remove_all(l, n, val):
   while(1):
       flag = 0
       for i in range(0,len(l)):
           if(l[i] == val):
               flag = 1
               j = i
               while(j < len(l) - 1):
                   l[j] = l[j+1]
                   j = j + 1
       if(flag == 0):
           break
       else:
           l.pop()
   print(l)
l = [1,4,5,3,6,4,2,4]
remove_all(l,8,4)

If you have any doubts please comment and please don't dislike.


Related Solutions

This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use...
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use of your prior two functions to create a 2D array filled with a default parameter. var twoD = Init2D(<width>, <height>, <fill val>); Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(<min>, <max>); Challenge 5 – Random Int 2D Array Use your prior functions to provide a function...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] let words = ["gin", "zen", "gig", "msg"] longestMorseCodeWords(words) The Morse...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT