Skip to content

Instantly share code, notes, and snippets.

@vbkaisetsu
Created May 31, 2017 12:15
Show Gist options
  • Save vbkaisetsu/6337c6d21a98b3c6194e4c600e758a70 to your computer and use it in GitHub Desktop.
Save vbkaisetsu/6337c6d21a98b3c6194e4c600e758a70 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from itertools import chain
def ranges_or(ranges):
result = []
depth = 0
for p, t in sorted(chain.from_iterable(((x, -1), (y, 1)) for x, y in ranges)):
if depth == 0 and t == -1:
start = p
elif depth == -1 and t == 1:
result.append((start, p))
depth += t
return result
print(ranges_or([(-3, 2), (5, 8), (8, 8), (0, 6), (11, 15), (16, 20), (18, 21), (14, 22), (9, 10)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment