Last active
March 29, 2017 14:16
-
-
Save mc706/a4ce807f2616dc2356c3b6c6d236b96a to your computer and use it in GitHub Desktop.
phillydev#daily_programmer-2017-03-29.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def overlaps(pairs): | |
clusters = [] | |
for pair in sorted(pairs): | |
low, high = pair | |
found = False | |
for i, cluster in enumerate(clusters): | |
cluster_low, cluster_high = cluster | |
if max(low, cluster_low) <= min(high, cluster_high): | |
found = True | |
clusters[i] = [min(low, cluster_low), max(high, cluster_high)] | |
break; | |
if not found: | |
clusters.append(pair) | |
return clusters | |
if __name__ == '__main__': | |
examples = [ | |
([[1,3], [2,6], [8,10], [7,11]], [[1,6], [7,11]]), | |
([[5,12], [8,10]],[[5,12]]), | |
([[1,3], [2,6], [8,10], [7,11], [6,9]], [[1,11]]), | |
] | |
for example in examples: | |
data, result = example | |
assert overlaps(data) == result, "got {0} expected {1}".format(overlaps(data), result) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment