Efficient way to find missing elements in an integer sequence

前端 未结 16 1195
鱼传尺愫
鱼传尺愫 2020-12-01 04:32

Suppose we have two items missing in a sequence of consecutive integers and the missing elements lie between the first and last elements. I did write a code that does accomp

相关标签:
16条回答
  • 2020-12-01 05:11

    Simply walk the list and look for non-consecutive numbers:

    prev = L[0]
    for this in L[1:]:
        if this > prev+1:
            for item in range(prev+1, this):    # this handles gaps of 1 or more
                print item
        prev = this
    
    0 讨论(0)
  • 2020-12-01 05:12
    
     a=[1,2,3,7,5,11,20]
     b=[]
     def miss(a,b):
         for x in range (a[0],a[-1]):
            if x not in a:
                b.append(x)
         return b
     print (miss(a,b))
    
    

    ANS:[4, 6, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19]

    works for sorted,unsorted , with duplicates too

    0 讨论(0)
  • 2020-12-01 05:13

    Assuming that L is a list of integers with no duplicates, you can infer that the part of the list between start and index is completely consecutive if and only if L[index] == L[start] + (index - start) and similarly with index and end is completely consecutive if and only if L[index] == L[end] - (end - index). This combined with splitting the list into two recursively gives a sublinear solution.

    # python 3.3 and up, in older versions, replace "yield from" with yield loop
    
    def missing_elements(L, start, end):
        if end - start <= 1: 
            if L[end] - L[start] > 1:
                yield from range(L[start] + 1, L[end])
            return
    
        index = start + (end - start) // 2
    
        # is the lower half consecutive?
        consecutive_low =  L[index] == L[start] + (index - start)
        if not consecutive_low:
            yield from missing_elements(L, start, index)
    
        # is the upper part consecutive?
        consecutive_high =  L[index] == L[end] - (end - index)
        if not consecutive_high:
            yield from missing_elements(L, index, end)
    
    def main():
        L = [10,11,13,14,15,16,17,18,20]
        print(list(missing_elements(L,0,len(L)-1)))
        L = range(10, 21)
        print(list(missing_elements(L,0,len(L)-1)))
    
    main()
    
    0 讨论(0)
  • 2020-12-01 05:13
    arr = [1, 2, 5, 6, 10, 12]
    diff = []
    
    """zip will return array of tuples (1, 2) (2, 5) (5, 6) (6, 10) (10, 12) """
    for a, b in zip(arr , arr[1:]):
        if a + 1 != b:
            diff.extend(range(a+1, b))
    
    print(diff)
    

    [3, 4, 7, 8, 9, 11]

    If the list is sorted we can lookup for any gap. Then generate a range object between current (+1) and next value (not inclusive) and extend it to the list of differences.

    0 讨论(0)
  • 2020-12-01 05:13

    First we should sort the list and then we check for each element, except the last one, if the next value is in the list. Be carefull not to have duplicates in the list!

    l.sort()
    
    [l[i]+1 for i in range(len(l)-1) if l[i]+1 not in l]
    
    0 讨论(0)
  • 2020-12-01 05:14
    missingItems = [x for x in complete_list if not x in L]
    
    0 讨论(0)
提交回复
热议问题