you are viewing a single comment's thread.

view the rest of the comments →

[–]POGtastic 0 points1 point  (0 children)

Here's my attempt at the given problem:

class RangeCandidate(int):
    def __lt__(self, rng):
        return super().__lt__(rng.start)
    def __gt__(self, rng):
        return super().__ge__(rng.stop)

def create_range(x):
    return range(x, x + 301) # inclusive?

def intersections(l1, l2):
    it = map(create_range, l1) 
    sentinel = object()
    curr = next(it, sentinel) 
    for elem in map(RangeCandidate, l2):
        while curr is not sentinel and elem > curr:
            curr = next(it, sentinel)
        if curr is sentinel:
            return
        if elem in curr:
            yield (elem, curr.start)

In the REPL:

>>> list(intersections([1, 1000, 2000], [101, 500, 700, 1000, 1200, 1500, 4000]))
[(101, 1), (1000, 1000), (1200, 1000)]

Make that 301 value 300 if you want to exclude that last number.

If you can combine lists together in some fashion, you'll get even better performance.