I'm writing a program that, giving a list of integers, identify the minimum value and then remove the minimum with the minor index of the list.
Ex: list = [3, 6, 15, 2, 4, 2] should return [3, 6, 15, 4, 2] Notice the first 2 removed because of the minor index on the list.
This is my entire code:
def remove_smallest(numbers):
mylist = []
if not numbers:
return mylist
minimo = min(numbers)
if numbers.count(minimo) > 1:
numbers = numbers[::-1] #My solution to the minor index is to reverse the list and loop. Then reverse again.
#mylist = [item for item in numbers if item != minimo and item not in mylist] #I tried to use a list comprehension, with awful results.
it = iter(numbers)
for item in it:
if item == minimo and item in mylist:
next(it, None)
continue
mylist.append(item)
print(mylist[::-1])
remove_smallest([2, 4, 5, 1, 2, 1])
First two items will get appended to "mylist" (1, 2). Then, because 1 is on mylist, it will skip it and then continue. So far so good, however when it should select the 5, it does not, instead goes straight to the 4 and then 2, resulting in an array that looks like this at the end of the program: [2, 4, 2, 1] when it should return [2, 4, 5, 2, 1]
Thanks