Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First (='ω'=) solution in Creative category for Merge Intervals by Magu
def merge_intervals(intervals):
a = 0; merg = []; (lst := sorted(list(set([n for t in intervals for n in range(t[0], t[1]+1)])))).append(float('inf'))
for b in range(1, len(lst)):
if lst[b] - lst[b-1] == 1: continue
merg.append((lst[a], lst[b-1])); a = b
return merg
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert merge_intervals([(1, 4), (2, 6), (8, 10), (12, 19)]) == [(1, 6), (8, 10), (12, 19)], "First"
assert merge_intervals([(1, 12), (2, 3), (4, 7)]) == [(1, 12)], "Second"
assert merge_intervals([(1, 5), (6, 10), (10, 15), (17, 20)]) == [(1, 15), (17, 20)], "Third"
print('Done! Go ahead and Check IT')
Oct. 30, 2021
Comments: