I am trying to implement a function that would insert any missing numbers in a list. The list is in ascending order with no repetitive elements.
This is my code so far:
def fill_in_numbers(lst):
i=0
while True:
if lst[-1] - lst[i] !=1:
lst.insert(i+1, lst[i]+1)
i+=1
if lst[-1] - lst[i]== 1:
break
return lst
So, for example, if the input is fill_in_numbers([12,15,19]) the expected output is [12,13,14,15,16,17,18,19], or if the input is fill_in_numbers([-5,0,5]) the expected output is [-5,-4,-3,-2,-1,0,1,2,3,4,5].
However, when I run my code the output I get for the first input is [12,13,14,15,16,17,18,15,19] and for the second one is [-5,-4,-3,-2,-1,0,1,2,3,4,0,5].
What is wrong with my code?