Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Removing List Items Suppose you want to write a program that removes the given items from the list. The program should work with two lists,
Removing List Items
Suppose you want to write a program that removes the given items from the list. The program should work with two lists, like these:
targetlist
removelist
The idea is to take the targetlist and delete the elements from positions listed in removelist. So in the first example, we want to remove elements and from targetlist. After this it should be
My first thought about how to do this didn't work:
def removepositionstargetlist removelist:
for elt in removelist:
del targetlistelt
values
positions
removepositionsvalues positions
printvalues
# at the moment, this prints:
# should be
values
positions
removepositionsvalues positions
printvalues
# should be
In your labpy add a comment that describes why the function above doesn't work: it loops over the items in removelist and deletes each one. Why doesn't that remove the correct items?
Then fix the function so it removes the correct items. Please remove the outsidethefunction test code before submitting: include only the corrected function definition and the comment.
Searching, but more
The search algorithms we have seen only return one of the positions of a value in the list. It's possible that a particular value will occur multiple times. In some cases, you want all of the occurrences returned.
In labpy write a function multisearch that takes a list and value as its argument. It should return a list of all positions in the list where the value occurs. If the value doesn't occur, this will be the empty list. For example,
testlist
multisearchtestlist
multisearchtestlist
multisearchtestlist
multisearchabaca
You may find it useful to start with the implementation of linear search from class. You can modify this to build and then return a list of the relevant positions.
Finding the Median
In labpy write a function median that takes a list of numbers as its argument and returns the list's median.
The median is the value in the list that has an equal number of values that are larger and smaller. If the list has an even length, the median is the average of the two middle elements.
median
median
Hint: sort the list first. To test if a number is even, you can look at n
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started