Inserting while itterating

Francis Avila francisgavila at yahoo.com
Wed Jan 14 04:10:30 EST 2004


Thomas Guettler wrote in message ...
>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
>
This a homework problem?

>>> L = [1, 2, 4, 7, 8, 12]
>>> result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> L.extend([i for i in range(L[0], L[-1]) if i not in L])
>>> L.sort()
>>> L == result
True

-- 
Francis Avila



More information about the Python-list mailing list