I’m trying to answer this question on a practice test:
Write a function, def eliminate(x, y), that copies all the elements of the list x except the largest value into the list y.
The best thing I could come up with is:
def eliminate(x, y):
print(x)
y = x
big = max(y)
y.remove(big)
print(y)
def main():
x = [1, 3, 5, 6, 7, 9]
y = [0]
eliminate(x, y)
main()
I don’t think that’ll cut it if a question like that comes up on my final, and I’m pretty sure I shouldn’t be writing a main function with it, just the eliminate one. So how would I answer this? (keep in mind this is an introductory course, I shouldn’t be using more advanced coding)
I’d probably do this:
This fills
ywith all the elements inxexcept whichever is largest. For example:This assumes that by “copies” the question is asking for the contents of
yto be replaced. If the non-maximal elements ofxare to be appended toy, you could usey.extendinstead.Note that your version doesn’t handle the case where there are multiple elements with the maximum value (e.g.
[1,2,2]) —.remove()only removes one of the arguments, not all of them.