I tried with special cases of the presence of (45, 46) and (45, 45)
as well as test cases that are unlikely to occur in your application: presence (11.6), presence (-1, -5), presence (-9, 5), presence (-3, 10). <w> In any case, the results are suitable for all these cases, this is the point.
Algorithm:
def yi(li): gen = (x for a,b in li for x in xrange(a,b+1)) start = p = gen.next() for x in gen: if x>p+2: yield (start,p) start = p = x else: p = x yield (start,x)
If aff in the following code is set to True, the steps will be shown.
def yi(li): aff = 0 gen = (x for a,b in li for x in xrange(a,b+1)) start = p = gen.next() for x in gen: if aff: print ('start %sp %d p+2 %d ' 'x==%s' % (start,p,p+2,x)) if x>p+2: if aff: print 'yield range(%d,%d)' % (start,p+1) yield (start,p) start = p = x else: p = x if aff: print 'yield range(%d,%d)' % (start,x+1) yield (start,x) for li in ([(7,10),(23,39),(11,13),(11,15),(14,20),(45,46)], [(7,10),(23,39),(11,13),(11,15),(14,20),(45,46),(45,45)], [(7,10),(23,39),(11,13),(11,15),(14,20),(45,45)], [(7,10),(23,39),(11,13),(11,6),(14,20),(45,46)],
result
sorted li [(7, 10), (11, 13), (11, 15), (14, 20), (23, 39), (45, 46)] (7,10) [7, 8, 9] (11,13) [11, 12] (11,15) [11, 12, 13, 14] (14,20) [14, 15, 16, 17, 18, 19] (23,39) [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38] (45,46) [45] list(yi(li)) [(7, 20), (23, 39), (45, 46)] sorted li [(7, 10), (11, 13), (11, 15), (14, 20), (23, 39), (45, 45), (45, 46)] (7,10) [7, 8, 9] (11,13) [11, 12] (11,15) [11, 12, 13, 14] (14,20) [14, 15, 16, 17, 18, 19] (23,39) [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38] (45,45) [] (45,46) [45] list(yi(li)) [(7, 20), (23, 39), (45, 46)] sorted li [(7, 10), (11, 13), (11, 15), (14, 20), (23, 39), (45, 45)] (7,10) [7, 8, 9] (11,13) [11, 12] (11,15) [11, 12, 13, 14] (14,20) [14, 15, 16, 17, 18, 19] (23,39) [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38] (45,45) [] list(yi(li)) [(7, 20), (23, 39), (45, 45)] sorted li [(7, 10), (11, 6), (11, 13), (14, 20), (23, 39), (45, 46)] (7,10) [7, 8, 9] (11,6) [] (11,13) [11, 12] (14,20) [14, 15, 16, 17, 18, 19] (23,39) [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38] (45,46) [45] list(yi(li)) [(7, 20), (23, 39), (45, 46)] sorted li [(-1, -5), (7, 10), (11, 13), (14, 20), (23, 39), (45, 45)] (-1,-5) [] (7,10) [7, 8, 9] (11,13) [11, 12] (14,20) [14, 15, 16, 17, 18, 19] (23,39) [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38] (45,45) [] list(yi(li)) [(7, 20), (23, 39), (45, 45)] sorted li [(-9, -5), (7, 10), (11, 13), (14, 20), (23, 39), (45, 45)] (-9,-5) [-9, -8, -7, -6] (7,10) [7, 8, 9] (11,13) [11, 12] (14,20) [14, 15, 16, 17, 18, 19] (23,39) [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38] (45,45) [] list(yi(li)) [(-9, -5), (7, 20), (23, 39), (45, 45)] sorted li [(-3, 10), (7, 10), (11, 13), (14, 20), (23, 39), (45, 45)] (-3,10) [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (7,10) [7, 8, 9] (11,13) [11, 12] (14,20) [14, 15, 16, 17, 18, 19] (23,39) [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38] (45,45) [] list(yi(li)) [(-3, 20), (23, 39), (45, 45)]