Inserting while itterating

Peter Abel PeterAbel at gmx.net
Fri Jan 16 13:33:17 CET 2004


"Thomas Guettler" <guettli at thomas-guettler.de> wrote in message news:<pan.2004.01.14.08.43.00.894047 at thomas-guettler.de>...
> Hi,
> 
> Simple excerise:
> 
> You have a sorted list of integers:
> l=[1, 2, 4, 7, 8, 12]
> 
> and you should fill all gaps:
> 
> result == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
> 
> How would you code this?
> 
> Constrain: The original list should be changed,
> don't create a copy.
> 
>   thomas


>>> l = [1, 2, 4, 7, 8, 12]
>>> id(l)
27295112
>>> for i in range(len(l)-1,0,-1):
... 	for j in range(l[i]-l[i-1]-1):
... 		l.insert(i,l[i]-1)
... 
>>> id(l)
27295112
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> 

Tested, but no idea about timing.

Regards
Peter



More information about the Python-list mailing list